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

#113 RsWithHeaders must reuse RsWithHeader #210

Merged
merged 6 commits into from
May 5, 2015
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/main/java/org/takes/rs/RsWithHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import lombok.EqualsAndHashCode;
import org.takes.Response;

Expand Down Expand Up @@ -62,15 +60,8 @@ public RsWithHeaders(final Response res,
super(
new Response() {
@Override
public List<String> head() throws IOException {
final List<String> head = new LinkedList<String>();
for (final String hdr : res.head()) {
head.add(hdr);
}
for (final CharSequence header : headers) {
head.add(header.toString().trim());
}
return head;
public Iterable<String> head() throws IOException {
return RsWithHeaders.extend(res, headers);
}
@Override
public InputStream body() throws IOException {
Expand All @@ -80,4 +71,20 @@ public InputStream body() throws IOException {
);
}

/**
* Add to head additional headers.
* @param res Original response
* @param headers Values witch will be added to head
* @return Head with additional headers
* @throws IOException If fails
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static Iterable<String> extend(final Response res,
final Iterable<? extends CharSequence> headers) throws IOException {
Response resp = res;
for (final CharSequence hdr: headers) {
resp = new RsWithHeader(resp, hdr);
}
return resp.head();
}
}
10 changes: 6 additions & 4 deletions src/test/java/org/takes/rs/RsWithHeadersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,21 @@ public final class RsWithHeadersTest {
*/
@Test
public void addsHeadersToResponse() throws IOException {
final String host = "Host: www.example.com";
final String type = "Content-Type: text/xml";
MatcherAssert.assertThat(
new RsPrint(
new RsWithHeaders(
new RsEmpty(),
"Host: www.example.com ",
"Content-Type: text/xml "
host,
type
)
).print(),
Matchers.equalTo(
Joiner.on("\r\n").join(
"HTTP/1.1 200 OK",
"Host: www.example.com",
"Content-Type: text/xml",
host,
type,
"",
""
)
Expand Down