Skip to content

Commit fe71671

Browse files
committed
Add Content Length header class and update to implement getter on abstract Response class
1 parent 4a18ff2 commit fe71671

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package httpserver;
2+
3+
public class ContentLengthHeader extends Header {
4+
public ContentLengthHeader(byte[] payload) {
5+
super("Content-Length", String.valueOf(payload.length));
6+
}
7+
}

src/main/java/httpserver/ResponseWriter.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public ResponseWriter(OutputStream outputStream) {
2323
public void write(Response response) {
2424
try {
2525
writeStatusCode(response);
26-
writeContentLengthHeader(response);
2726
writeHeaders(response);
2827
writeEmptyLine();
2928
writePayload(response);
@@ -36,16 +35,18 @@ private void writeStatusCode(Response response) throws IOException {
3635
write(statusLine(response.getStatusCode()).getBytes());
3736
}
3837

39-
private void writeContentLengthHeader(Response response) throws IOException {
40-
write(contentLengthHeader(response.getPayload()).getBytes());
41-
}
42-
4338
private void writeHeaders(Response response) throws IOException {
39+
writeContentLengthHeader(response);
4440
for (Header header: response.getHeaders()) {
4541
write((header.toString() + "\r\n").getBytes());
4642
}
4743
}
4844

45+
private void writeContentLengthHeader(Response response) throws IOException {
46+
String header = response.getContentLengthHeader().toString() + "\r\n";
47+
write(header.getBytes());
48+
}
49+
4950
private void writeEmptyLine() throws IOException {
5051
write(("\r\n").getBytes());
5152
}
@@ -58,10 +59,6 @@ private void write(byte[] bytes) throws IOException {
5859
outputStream.write(bytes);
5960
}
6061

61-
private String contentLengthHeader(byte[] payload) {
62-
return "Content-Length: " + payload.length + "\r\n";
63-
}
64-
6562
private String statusLine(int statusCode) {
6663
return ("HTTP/1.1 " + statusCode
6764
+ " " + statuses.get(statusCode)

src/main/java/httpserver/response/Response.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package httpserver.response;
22

3+
import httpserver.ContentLengthHeader;
34
import httpserver.Header;
45

56
import java.util.ArrayList;
@@ -31,4 +32,8 @@ public Header[] getHeaders() {
3132
public void setHeader(Header header) {
3233
headers.add(header);
3334
}
35+
36+
public Header getContentLengthHeader() {
37+
return new ContentLengthHeader(payload);
38+
}
3439
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package httpserver;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class ContentLengthHeaderTest {
8+
@Test
9+
public void setsContentLength() throws Exception {
10+
byte[] payload = "test payload".getBytes();
11+
12+
ContentLengthHeader contentLengthHeader = new ContentLengthHeader(payload);
13+
14+
assertEquals(new Header("Content-Length", "12"), contentLengthHeader);
15+
}
16+
}

src/test/java/httpserver/response/OkResponseTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,25 @@ public void getsStatusCode() throws Exception {
2626
@Test
2727
public void getsPayload() throws Exception {
2828
byte[] payload = okResponse.getPayload();
29+
2930
assertEquals("test payload", new String(payload));
3031
}
3132

3233
@Test
3334
public void getsHeaders() throws Exception {
3435
Header[] expected = new Header[]{new Header("header1", "value"),
3536
new Header("header2", "v")};
36-
OkResponse okResponse = this.okResponse;
37+
3738
assertTrue(Arrays.equals(expected, okResponse.getHeaders()));
3839
}
3940

41+
@Test
42+
public void getsContentLengthHeader() throws Exception {
43+
Header expected = new Header("Content-Length", "12");
44+
45+
assertEquals(expected, okResponse.getContentLengthHeader());
46+
}
47+
4048
@Test
4149
public void hasEmptyHeaderList() throws Exception {
4250
OkResponse okResponse = new OkResponse("test payload".getBytes());

0 commit comments

Comments
 (0)