Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller unit test failure when the request body is empty and is read directly #82

Open
aentwist opened this issue Jul 16, 2021 · 0 comments

Comments

@aentwist
Copy link

Implementing complete GraphQL support involves reading the request body and considering it as the query for one special case. In a controller, this means using request.reader.text.

def index() {
    def body = request.reader.text
    respond body
}

In a test class the controller method is called multiple times as there are multiple tests.

def "test"() {
    when:
    controller.index()

    then:
    true
}

def "test2"() {
    when:
    controller.index()

    then:
    true
}

This is a problem when the body is empty. When executing the second test, it fails with java.io.IOException: Stream closed. The tests are not independent.

Please note that when the body is not empty, i.e.
request.json = [hello: "world"]
the tests pass.

Please also note that replacing request.reader.text with request.JSON in the controller causes the tests to pass. This is specifically for reading the body text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant