1212
Replace Reponse getPayload method with writePayload(outputStream) method · onlyskin/java-httpserver@70de44a · GitHub
Skip to content

Commit 70de44a

Browse files
committed
Replace Reponse getPayload method with writePayload(outputStream) method
1 parent 0a4f742 commit 70de44a

17 files changed

+108
-36
lines changed

src/main/java/httpserver/ResponseWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private void writeEmptyLine() throws IOException {
7171
}
7272

7373
private void writePayload(Response response) throws IOException {
74-
write(response.getPayload());
74+
response.writePayload(outputStream);
7575
}
7676

7777
private void write(byte[] bytes) throws IOException {

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import httpserver.header.ContentLengthHeader;
44
import httpserver.header.Header;
55

6+
import java.io.IOException;
7+
import java.io.OutputStream;
68
import java.util.ArrayList;
79
import java.util.List;
810

@@ -19,13 +21,6 @@ public Response() {
1921

2022
public abstract int getStatusCode();
2123

22-
public byte[] getPayload() {
23-
if (isHead) {
24-
return new byte[0];
25-
}
26-
return payload;
27-
}
28-
2924
public void setPayload(byte[] newPayload) {
3025
payload = newPayload;
3126
}
@@ -45,4 +40,11 @@ public Header getContentLengthHeader() {
4540
public void setHeadTrue() {
4641
isHead = true;
4742
}
43+
44+
public void writePayload(OutputStream outputStream) throws IOException {
45+
if (isHead) {
46+
outputStream.write(new byte[0]);
47+
}
48+
outputStream.write(payload);
49+
}
4850
}

src/test/java/httpserver/ResponseWriterTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.io.IOException;
1212

1313
import static org.junit.Assert.*;
14-
import static org.mockito.Mockito.*;
1514

1615
public class ResponseWriterTest {
1716
private final ResponseWriter responseWriter;

src/test/java/httpserver/responder/FormGetResponderTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.junit.Test;
1010

1111
import java.io.IOException;
12+
import java.io.OutputStream;
1213
import java.nio.file.Path;
1314

1415
import static org.junit.Assert.*;
@@ -39,15 +40,18 @@ public FormGetResponderTest() throws IOException {
3940
}
4041

4142
@Test
42-
public void returnsOkResponse() throws Exception {
43+
public void returnsOkResponseWithPayload() throws Exception {
4344
when(pathExaminerMock.pathExists(any())).thenReturn(true);
4445
Request request = new Request("GET", pathString, new Header[0], "");
4546

4647
Response response = formGetResponder.respond(appConfigMock, request);
4748

4849
verify(pathExaminerMock).pathExists(fullPathMock);
4950
assertEquals(200, response.getStatusCode());
50-
assertEquals("file contents mock", new String(response.getPayload()));
51+
52+
OutputStream outputStreamMock = mock(OutputStream.class);
53+
response.writePayload(outputStreamMock);
54+
verify(outputStreamMock).write(fileContentsMock);
5155
}
5256

5357
@Test

src/test/java/httpserver/responder/GetResponderTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.junit.Test;
1212

1313
import java.io.IOException;
14+
import java.io.OutputStream;
1415
import java.nio.file.Path;
1516

1617
import static org.junit.Assert.*;
@@ -49,7 +50,9 @@ public void returns404ForBadPath() throws Exception {
4950
Response response = getResponder.respond(appConfigMock, request);
5051

5152
assertEquals(404, response.getStatusCode());
52-
assertEquals("", new String(response.getPayload()));
53+
OutputStream outputStreamMock = mock(OutputStream.class);
54+
response.writePayload(outputStreamMock);
55+
verify(outputStreamMock).write("".getBytes());
5356
}
5457

5558
@Test
@@ -83,8 +86,11 @@ public void getsFileContentsForPath() throws Exception {
8386
verify(pathExaminerMock).pathExists(fullPathMock);
8487
verify(pathExaminerMock).isFile(fullPathMock);
8588
assertEquals(200, response.getStatusCode());
86-
assertEquals(payloadMock, response.getPayload());
8789
assertEquals("Content-Type", response.getHeaders()[0].getKey());
90+
91+
OutputStream outputStreamMock = mock(OutputStream.class);
92+
response.writePayload(outputStreamMock);
93+
verify(outputStreamMock).write(payloadMock);
8894
}
8995

9096
@Test

src/test/java/httpserver/responder/HeadResponderTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import httpserver.response.Response;
1010
import org.junit.Test;
1111

12+
import java.io.OutputStream;
1213
import java.nio.file.Path;
1314

1415
import static org.junit.Assert.*;
@@ -25,7 +26,7 @@ public HeadResponderTest() {
2526
}
2627

2728
@Test
28-
public void getPayloadReturnsEmptyByteArrayButContentLengthHeaderIsFull() throws Exception {
29+
public void writePayloadWritesEmptyByteArrayButContentLengthHeaderIsFull() throws Exception {
2930
Path fullPathMock = mock(Path.class);
3031
when(pathExaminerMock.getFullPath(rootMock, "/filename")).thenReturn(fullPathMock);
3132
when(pathExaminerMock.pathExists(fullPathMock)).thenReturn(true);
@@ -46,7 +47,9 @@ public void getPayloadReturnsEmptyByteArrayButContentLengthHeaderIsFull() throws
4647

4748
Response response = headResponder.respond(appConfigMock, request);
4849

49-
assertEquals("", new String(response.getPayload()));
50+
OutputStream outputStreamMock = mock(OutputStream.class);
51+
response.writePayload(outputStreamMock);
52+
verify(outputStreamMock).write("".getBytes());
5053
assertEquals("21", response.getContentLengthHeader().getValue());
5154
}
5255
}

src/test/java/httpserver/responder/PostResponderTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
import httpserver.response.Response;
99
import org.junit.Test;
1010

11+
import java.io.OutputStream;
1112
import java.nio.file.Path;
1213

13-
import static junit.framework.TestCase.*;
14+
import static org.junit.Assert.*;
1415
import static org.mockito.Mockito.*;
1516

1617
public class PostResponderTest {
@@ -47,7 +48,9 @@ public void overwritesFileWithDataIfHandlesAndExists() throws Exception {
4748
assertEquals(200, response.getStatusCode());
4849
verify(fileOperatorMock).replaceContents(fullPathMock, "data=example".getBytes());
4950
verify(fileOperatorMock).readContents(fullPathMock);
50-
assertEquals(fileContentsMock, response.getPayload());
51+
OutputStream outputStreamMock = mock(OutputStream.class);
52+
response.writePayload(outputStreamMock);
53+
verify(outputStreamMock).write(fileContentsMock);
5154
}
5255

5356
@Test
@@ -64,7 +67,9 @@ public void createsFileWithDataIfHandlesButDoesntExist() throws Exception {
6467
verify(fileOperatorMock).createFileAtPath(fullPathMock);
6568
verify(fileOperatorMock).replaceContents(fullPathMock, "data=example".getBytes());
6669
verify(fileOperatorMock).readContents(fullPathMock);
67-
assertEquals(fileContentsMock, response.getPayload());
70+
OutputStream outputStreamMock = mock(OutputStream.class);
71+
response.writePayload(outputStreamMock);
72+
verify(outputStreamMock).write(fileContentsMock);
6873
}
6974

7075
@Test

src/test/java/httpserver/responder/PutResponderTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import httpserver.response.Response;
99
import org.junit.Test;
1010

11+
import java.io.OutputStream;
1112
import java.nio.file.Path;
1213

1314
import static org.junit.Assert.*;
@@ -49,7 +50,9 @@ public void overwritesFileWithDataIfHandlesAndExists() throws Exception {
4950
assertEquals(200, response.getStatusCode());
5051
verify(fileOperatorMock).replaceContents(fullPathMock, "data=example".getBytes());
5152
verify(fileOperatorMock).readContents(fullPathMock);
52-
assertEquals(fileContentsMock, response.getPayload());
53+
OutputStream outputStreamMock = mock(OutputStream.class);
54+
response.writePayload(outputStreamMock);
55+
verify(outputStreamMock).write(fileContentsMock);
5356
}
5457

5558
@Test

src/test/java/httpserver/responder/special/CookieResponderTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import httpserver.response.Response;
77
import org.junit.Test;
88

9+
import java.io.OutputStream;
910
import java.util.Arrays;
1011

1112
import static org.junit.Assert.*;
@@ -25,9 +26,11 @@ public void hasCorrectPayloadAndSetsCookie() throws Exception {
2526
mock(Request.class));
2627

2728
assertEquals(200, response.getStatusCode());
28-
assertEquals("Eat", new String(response.getPayload()));
2929
Header[] expected = new Header[]{new Header("Set-Cookie", "key=value")};
3030
assertTrue(Arrays.equals(expected, response.getHeaders()));
31+
OutputStream outputStreamMock = mock(OutputStream.class);
32+
response.writePayload(outputStreamMock);
33+
verify(outputStreamMock).write("Eat".getBytes());
3134
}
3235

3336
@Test

src/test/java/httpserver/responder/special/EatCookieResponderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import httpserver.response.Response;
66
import org.junit.Test;
77

8+
import java.io.OutputStream;
9+
810
import static org.junit.Assert.*;
911
import static org.mockito.Mockito.*;
1012

@@ -22,7 +24,9 @@ public void hasCorrectPayloadAndSetsCookie() throws Exception {
2224
Response response = eatCookieResponder.respond(mock(AppConfig.class),
2325
mock(Request.class));
2426
assertEquals(200, response.getStatusCode());
25-
assertEquals("mmmm chocolate", new String(response.getPayload()));
27+
OutputStream outputStreamMock = mock(OutputStream.class);
28+
response.writePayload(outputStreamMock);
29+
verify(outputStreamMock).write("mmmm chocolate".getBytes());
2630
}
2731

2832
@Test

0 commit comments

Comments
 (0)