diff --git a/src/main/java/org/takes/rs/RsWithBody.java b/src/main/java/org/takes/rs/RsWithBody.java index 5180b6742..e4b186a8b 100644 --- a/src/main/java/org/takes/rs/RsWithBody.java +++ b/src/main/java/org/takes/rs/RsWithBody.java @@ -93,7 +93,7 @@ public RsWithBody(final Response res, final URL url) { new Response() { @Override public Iterable head() throws IOException { - return res.head(); + return append(res, url.openStream().available()); } @Override public InputStream body() throws IOException { @@ -113,7 +113,7 @@ public RsWithBody(final Response res, final byte[] body) { new Response() { @Override public Iterable head() throws IOException { - return res.head(); + return append(res, body.length); } @Override public InputStream body() { @@ -133,7 +133,7 @@ public RsWithBody(final Response res, final InputStream body) { new Response() { @Override public Iterable head() throws IOException { - return res.head(); + return append(res, body.available()); } @Override public InputStream body() { @@ -143,4 +143,18 @@ public InputStream body() { ); } + /** + * Appends content length to header from response. + * @param res Response + * @param length Response body content length + * @return Iterable String of header attributes + * @throws IOException if something goes wrong. + */ + private static Iterable append( + final Response res, final int length) throws IOException { + return new RsWithHeader( + res, "Content-Length", Integer.toString(length) + ).head(); + } + } diff --git a/src/test/java/org/takes/facets/fork/TkForkTest.java b/src/test/java/org/takes/facets/fork/TkForkTest.java index 88b329f69..c87373c88 100644 --- a/src/test/java/org/takes/facets/fork/TkForkTest.java +++ b/src/test/java/org/takes/facets/fork/TkForkTest.java @@ -55,6 +55,7 @@ public void dispatchesByRegularExpression() throws IOException { Matchers.equalTo( Joiner.on("\r\n").join( "HTTP/1.1 200 OK", + String.format("Content-Length: %s", body.length()), "Content-Type: text/plain", "", body diff --git a/src/test/java/org/takes/rs/RsTextTest.java b/src/test/java/org/takes/rs/RsTextTest.java index 72d990e81..d7e03ab25 100644 --- a/src/test/java/org/takes/rs/RsTextTest.java +++ b/src/test/java/org/takes/rs/RsTextTest.java @@ -49,6 +49,7 @@ public void makesPlainTextResponse() throws IOException { Matchers.equalTo( Joiner.on("\r\n").join( "HTTP/1.1 200 OK", + String.format("Content-Length: %s", body.length()), "Content-Type: text/plain", "", body diff --git a/src/test/java/org/takes/tk/TkHTMLTest.java b/src/test/java/org/takes/tk/TkHTMLTest.java index 4773e8ee0..c47b5f0e5 100644 --- a/src/test/java/org/takes/tk/TkHTMLTest.java +++ b/src/test/java/org/takes/tk/TkHTMLTest.java @@ -52,6 +52,7 @@ public void createsTextResponse() throws IOException { Matchers.equalTo( Joiner.on("\r\n").join( "HTTP/1.1 200 OK", + String.format("Content-Length: %s", body.length()), "Content-Type: text/html", "", body diff --git a/src/test/java/org/takes/tk/TkTextTest.java b/src/test/java/org/takes/tk/TkTextTest.java index f22228220..6187e9de1 100644 --- a/src/test/java/org/takes/tk/TkTextTest.java +++ b/src/test/java/org/takes/tk/TkTextTest.java @@ -52,6 +52,7 @@ public void createsTextResponse() throws IOException { Matchers.equalTo( Joiner.on("\r\n").join( "HTTP/1.1 200 OK", + String.format("Content-Length: %s", body.length()), "Content-Type: text/plain", "", body