Skip to content

writing content as bytes to avoid contentLength calculation problems #242

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
Apr 9, 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 @@ -33,8 +33,9 @@ public void write(HttpServletRequest request, HttpServletResponse response) thro
responseBuilder.append(']');

String responseContent = responseBuilder.toString();
response.setContentLength(responseContent.getBytes(StandardCharsets.UTF_8).length);
response.getWriter().write(responseContent);
byte[] contentBytes = responseContent.getBytes(StandardCharsets.UTF_8);
response.setContentLength(contentBytes.length);
response.getOutputStream().write(contentBytes);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public void write(HttpServletRequest request, HttpServletResponse response) thro
response.setStatus(HttpRequestHandler.STATUS_OK);
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
String responseContent = graphQLObjectMapper.serializeResultAsJson(result);
response.setContentLength(responseContent.getBytes(StandardCharsets.UTF_8).length);
response.getWriter().write(responseContent);
byte[] contentBytes = responseContent.getBytes(StandardCharsets.UTF_8);
response.setContentLength(contentBytes.length);
response.getOutputStream().write(contentBytes);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import org.codehaus.groovy.runtime.StringBufferWriter
import spock.lang.Specification
import spock.lang.Unroll

import javax.servlet.ServletOutputStream
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.nio.charset.StandardCharsets

class SingleQueryResponseWriterTest extends Specification {

Expand All @@ -20,18 +22,16 @@ class SingleQueryResponseWriterTest extends Specification {

def requestMock = Mock(HttpServletRequest)
def responseMock = Mock(HttpServletResponse)
responseMock.getOutputStream() >> Mock(ServletOutputStream)

def responseContentBuffer = new StringBuffer()
responseMock.getWriter() >> new PrintWriter(new StringBufferWriter(responseContentBuffer))
1 * responseMock.setContentLength(expectedContentLenght)
1 * responseMock.setCharacterEncoding("UTF-8")
1 * responseMock.setCharacterEncoding(StandardCharsets.UTF_8.name())
1 * responseMock.getOutputStream().write(expectedResponseContent.getBytes(StandardCharsets.UTF_8))

expect:
def writer = new SingleQueryResponseWriter(new ExecutionResultImpl(result, []), graphQLObjectMapperMock)
writer.write(requestMock, responseMock)

responseContentBuffer.toString() == expectedResponseContent

where:
result || expectedContentLenght | expectedResponseContent
[testValue: "abcde"] || 30 | """{"data":{"testValue":"abcde"}}"""
Expand Down