Skip to content

fix content length on batched queries #231

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

Merged
merged 1 commit into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import graphql.ExecutionResult;
import graphql.kickstart.execution.GraphQLObjectMapper;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -32,7 +33,7 @@ public void write(HttpServletRequest request, HttpServletResponse response) thro
responseBuilder.append(']');

String responseContent = responseBuilder.toString();
response.setContentLength(responseContent.length());
response.setContentLength(responseContent.getBytes(StandardCharsets.UTF_8).length);
response.getWriter().write(responseContent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.springframework.mock.web.MockHttpServletResponse
import spock.lang.Shared
import spock.lang.Specification

import java.nio.charset.StandardCharsets
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicReference
Expand Down Expand Up @@ -108,10 +109,24 @@ class AbstractGraphQLHttpServletSpec extends Specification {
then:
response.getStatus() == STATUS_OK
response.getContentType() == CONTENT_TYPE_JSON_UTF8
response.getContentLength() == mapper.writeValueAsString(["data": ["echo": "test"]]).length()
response.getContentLength() == mapper.writeValueAsString(["data": ["echo": "test"]]).getBytes(StandardCharsets.UTF_8).length
getResponseContent().data.echo == "test"
}

def "query over HTTP GET returns data with correct contentLength"() {
setup:
request.addParameter('query', 'query { echo(arg:"special char á") }')

when:
servlet.doGet(request, response)

then:
response.getStatus() == STATUS_OK
response.getContentType() == CONTENT_TYPE_JSON_UTF8
response.getContentLength() == mapper.writeValueAsString(["data": ["echo": "special char á"]]).getBytes(StandardCharsets.UTF_8).length
getResponseContent().data.echo == "special char á"
}

def "async query over HTTP GET starts async request"() {
setup:
servlet = TestUtils.createDefaultServlet({ env -> env.arguments.arg }, { env -> env.arguments.arg }, { env ->
Expand Down Expand Up @@ -213,6 +228,21 @@ class AbstractGraphQLHttpServletSpec extends Specification {
getBatchedResponseContent()[1].data.echo == "test"
}

def "batched query over HTTP GET returns data with correct contentLength"() {
setup:
request.addParameter('query', '[{ "query": "query { echo(arg:\\"special char á\\") }" }, { "query": "query { echo(arg:\\"test\\") }" }]')

when:
servlet.doGet(request, response)

then:
response.getStatus() == STATUS_OK
response.getContentType() == CONTENT_TYPE_JSON_UTF8
response.getContentLength() == mapper.writeValueAsString([["data": ["echo": "special char á"]], ["data": ["echo": "test"]]]).getBytes(StandardCharsets.UTF_8).length
getBatchedResponseContent()[0].data.echo == "special char á"
getBatchedResponseContent()[1].data.echo == "test"
}

def "batched query over HTTP GET with variables returns data"() {
setup:
request.addParameter('query', '[{ "query": "query { echo(arg:\\"test\\") }", "variables": { "arg": "test" } }, { "query": "query { echo(arg:\\"test\\") }", "variables": { "arg": "test" } }]')
Expand Down