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

[#2140] Make FunctionalTest compatible with Response.writeChunk() #1208

Merged
merged 2 commits into from Jan 4, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions framework/src/play/test/FunctionalTest.java
Expand Up @@ -31,6 +31,7 @@
import play.Invoker;
import play.Invoker.InvocationContext;
import play.classloading.enhancers.ControllersEnhancer.ControllerInstrumentation;
import play.libs.F.Action;
import play.mvc.ActionInvoker;
import play.mvc.Controller;
import play.mvc.Http;
Expand Down Expand Up @@ -399,6 +400,31 @@ public static Response makeRequest(Request request) {
public static Response newResponse() {
Response response = new Response();
response.out = new ByteArrayOutputStream();

// Register an onWriteChunk action so that Response.writeChunk() won't throw
// an unhandled exception if the controller action calls it.
response.onWriteChunk(
new Action<Object>() {
@Override
public void invoke(Object chunk) {
// Mimic the behavior of PlayHandler$LazyChunkedInput.writeChunk()
if (chunk != null) {
try {
byte[] bytes;
if (chunk instanceof byte[]) {
bytes = (byte[]) chunk;
} else {
bytes = chunk.toString().getBytes(response.encoding);
}
response.out.write(bytes);
} catch (Exception exception) {
// Something is wrong with the chunk.
throw new RuntimeException(exception);
}
}
}
});

return response;
}

Expand Down
13 changes: 13 additions & 0 deletions samples-and-tests/just-test-cases/test/FunctionalTestTest.java
Expand Up @@ -126,5 +126,18 @@ public void testGettingStaticFile() {
Response response = GET("http://localhost:9003/public/session.test?req=1");
assertIsOk(response);
}

/**
* This is a regression test for [#2140], which is a bug in FunctionalTest that prevented it from
* testing a controller action that uses {@link Response#writeChunk(Object)}.
*/
@Test
public void testWriteChunks() {
Response response = GET("/application/writeChunks");
assertTrue(response.chunked);
assertIsOk(response);
assertContentType("text/plain", response);
assertContentEquals("abcæøåæøå", response);
}
}