1
2
3
4
5
6
7 package waffle.mock.http;
8
9 import java.io.IOException;
10 import java.io.PrintWriter;
11
12 import org.junit.jupiter.api.Assertions;
13 import org.junit.jupiter.api.BeforeEach;
14 import org.junit.jupiter.api.Test;
15
16
17
18
19 class SimpleHttpResponseTest {
20
21
22 private SimpleHttpResponse response;
23
24
25
26
27 @BeforeEach
28 void setUp() {
29 this.response = new SimpleHttpResponse();
30 }
31
32
33
34
35 @Test
36 void testInitialStatus() {
37 Assertions.assertEquals(500, this.response.getStatus());
38 }
39
40
41
42
43 @Test
44 void testSendErrorInt() {
45 this.response.sendError(403);
46 Assertions.assertEquals(403, this.response.getStatus());
47 }
48
49
50
51
52 @Test
53 void testSendErrorIntString() {
54 this.response.sendError(404, "Not Found");
55 Assertions.assertEquals(404, this.response.getStatus());
56 }
57
58
59
60
61 @Test
62 void testSetStatus() {
63 this.response.setStatus(200);
64 Assertions.assertEquals(200, this.response.getStatus());
65 }
66
67
68
69
70 @Test
71 void testGetStatusStringUnauthorized() {
72 this.response.setStatus(401);
73 Assertions.assertEquals("Unauthorized", this.response.getStatusString());
74 }
75
76
77
78
79 @Test
80 void testGetStatusStringUnknown() {
81 this.response.setStatus(200);
82 Assertions.assertEquals("Unknown", this.response.getStatusString());
83 }
84
85
86
87
88 @Test
89 void testAddHeaderAndGetHeaderValues() {
90 this.response.addHeader("X-Custom", "value1");
91 this.response.addHeader("X-Custom", "value2");
92 final String[] values = this.response.getHeaderValues("X-Custom");
93 Assertions.assertNotNull(values);
94 Assertions.assertEquals(2, values.length);
95 }
96
97
98
99
100 @Test
101 void testGetHeaderValuesNullForMissing() {
102 Assertions.assertNull(this.response.getHeaderValues("X-Missing"));
103 }
104
105
106
107
108 @Test
109 void testSetHeaderReplacesValue() {
110 this.response.addHeader("X-Single", "old");
111 this.response.setHeader("X-Single", "new");
112 Assertions.assertEquals("new", this.response.getHeader("X-Single"));
113 }
114
115
116
117
118 @Test
119 void testGetHeaderNullForMissing() {
120 Assertions.assertNull(this.response.getHeader("X-NotSet"));
121 }
122
123
124
125
126 @Test
127 void testGetWriter() {
128 final PrintWriter writer = this.response.getWriter();
129 Assertions.assertNotNull(writer);
130 writer.print("hello");
131 final String text = this.response.getOutputText();
132 Assertions.assertEquals("hello", text);
133 }
134
135
136
137
138
139
140
141 @Test
142 void testGetOutputStream() throws IOException {
143 final javax.servlet.ServletOutputStream out = this.response.getOutputStream();
144 Assertions.assertNotNull(out);
145 out.write('A');
146 Assertions.assertTrue(this.response.getOutputText().contains("A"));
147 }
148
149
150
151
152 @Test
153 void testGetOutputTextEmpty() {
154 Assertions.assertEquals("", this.response.getOutputText());
155 }
156
157
158
159
160 @Test
161 void testFlushBufferDoesNotThrow() {
162 this.response.sendError(401);
163 Assertions.assertDoesNotThrow(() -> this.response.flushBuffer());
164 }
165
166
167
168
169 @Test
170 void testGetHeaderNamesSize() {
171 Assertions.assertEquals(0, this.response.getHeaderNamesSize());
172 this.response.addHeader("X-One", "v1");
173 this.response.addHeader("X-Two", "v2");
174 Assertions.assertEquals(2, this.response.getHeaderNamesSize());
175 }
176 }