Skip to content

Commit

Permalink
yegor256#58: Content-length header attribute added
Browse files Browse the repository at this point in the history
  • Loading branch information
popprem committed Apr 18, 2015
1 parent 080ae2e commit 7061428
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
54 changes: 51 additions & 3 deletions src/main/java/org/takes/rs/RsWithBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import lombok.EqualsAndHashCode;
import org.takes.Response;

Expand All @@ -42,6 +45,11 @@
@EqualsAndHashCode(callSuper = true)
public final class RsWithBody extends RsWrap {

/**
* String for content lenght key:value.
*/
private static final String CONT_LGTH = "Content-Length:%s";

/**
* Ctor.
* @param body Body
Expand Down Expand Up @@ -93,7 +101,16 @@ public RsWithBody(final Response res, final URL url) {
new Response() {
@Override
public Iterable<String> head() throws IOException {
return res.head();
final List<String> headerAttr = getHeadAttributes(
res.head()
);
headerAttr.add(
String.format(
CONT_LGTH,
url.openStream().available()
)
);
return headerAttr;
}
@Override
public InputStream body() throws IOException {
Expand All @@ -113,7 +130,15 @@ public RsWithBody(final Response res, final byte[] body) {
new Response() {
@Override
public Iterable<String> head() throws IOException {
return res.head();
final List<String> headerAttr = getHeadAttributes(
res.head()
);
headerAttr.add(
String.format(
CONT_LGTH, body.length
)
);
return headerAttr;
}
@Override
public InputStream body() {
Expand All @@ -133,7 +158,15 @@ public RsWithBody(final Response res, final InputStream body) {
new Response() {
@Override
public Iterable<String> head() throws IOException {
return res.head();
final List<String> headerAttr = getHeadAttributes(
res.head()
);
headerAttr.add(
String.format(
CONT_LGTH, body.available()
)
);
return headerAttr;
}
@Override
public InputStream body() {
Expand All @@ -143,4 +176,19 @@ public InputStream body() {
);
}

/**
* Forms a list with header attributes from response.
* @param head Response head
* @return List of header attributes
*/
private static List<String> getHeadAttributes(final Iterable<String> head) {
final Iterator<String> itr = head.iterator();
// @checkstyle ConditionalRegexpMultilineCheck (1 line)
final List<String> headerAttrs = new ArrayList<String>();
while (itr.hasNext()) {
headerAttrs.add(itr.next());
}
return headerAttrs;
}

}
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 7061428

Please sign in to comment.