Skip to content

Commit

Permalink
Merge pull request playframework#1208 from davidcostanzo/lighthouse-2…
Browse files Browse the repository at this point in the history
…140-patch

[#2140] Make FunctionalTest compatible with Response.writeChunk()
  • Loading branch information
xael-fry authored Jan 4, 2018
2 parents d4692be + f0f6545 commit 913a5c6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
26 changes: 26 additions & 0 deletions framework/src/play/test/FunctionalTest.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}

0 comments on commit 913a5c6

Please sign in to comment.