Skip to content

Commit

Permalink
Merge remote-tracking branch 'fork/58'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Apr 29, 2015
2 parents 0a4b5f2 + d4fb402 commit ffba83c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/main/java/org/takes/rs/RsWithBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public RsWithBody(final Response res, final URL url) {
new Response() {
@Override
public Iterable<String> head() throws IOException {
return res.head();
return append(res, url.openStream().available());
}
@Override
public InputStream body() throws IOException {
Expand All @@ -113,7 +113,7 @@ public RsWithBody(final Response res, final byte[] body) {
new Response() {
@Override
public Iterable<String> head() throws IOException {
return res.head();
return append(res, body.length);
}
@Override
public InputStream body() {
Expand All @@ -133,7 +133,7 @@ public RsWithBody(final Response res, final InputStream body) {
new Response() {
@Override
public Iterable<String> head() throws IOException {
return res.head();
return append(res, body.available());
}
@Override
public InputStream body() {
Expand All @@ -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<String> append(
final Response res, final int length) throws IOException {
return new RsWithHeader(
res, "Content-Length", Integer.toString(length)
).head();
}

}
1 change: 1 addition & 0 deletions src/test/java/org/takes/facets/fork/TkForkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/takes/rs/RsTextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/takes/tk/TkHTMLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/takes/tk/TkTextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ffba83c

Please sign in to comment.