1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package waffle.mock.http;
25
26 import jakarta.servlet.ServletOutputStream;
27 import jakarta.servlet.WriteListener;
28 import jakarta.servlet.http.HttpServletResponse;
29 import jakarta.servlet.http.HttpServletResponseWrapper;
30
31 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.OutputStreamWriter;
34 import java.io.PrintWriter;
35 import java.nio.charset.StandardCharsets;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40
41 import org.mockito.Mockito;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45
46
47
48 public class SimpleHttpResponse extends HttpServletResponseWrapper {
49
50
51 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleHttpResponse.class);
52
53
54 private int status = 500;
55
56
57 private final Map<String, List<String>> headers = new HashMap<>();
58
59
60 private final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
61
62
63 private final ServletOutputStream out = new ServletOutputStream() {
64 @Override
65 public void write(final int b) {
66 SimpleHttpResponse.this.bytes.write(b);
67 }
68
69 @Override
70 public boolean isReady() {
71 return false;
72 }
73
74 @Override
75 public void setWriteListener(final WriteListener writeListener) {
76
77 }
78 };
79
80
81 private final PrintWriter writer = new PrintWriter(new OutputStreamWriter(this.bytes, StandardCharsets.UTF_8),
82 true);
83
84
85
86
87 public SimpleHttpResponse() {
88 super(Mockito.mock(HttpServletResponse.class));
89 }
90
91
92
93
94
95
96 @Override
97 public int getStatus() {
98 return this.status;
99 }
100
101 @Override
102 public void addHeader(final String headerName, final String headerValue) {
103 List<String> current = this.headers.get(headerName);
104 if (current == null) {
105 current = new ArrayList<>();
106 }
107 current.add(headerValue);
108 this.headers.put(headerName, current);
109 }
110
111 @Override
112 public void setHeader(final String headerName, final String headerValue) {
113 List<String> current = this.headers.get(headerName);
114 if (current == null) {
115 current = new ArrayList<>();
116 } else {
117 current.clear();
118 }
119 current.add(headerValue);
120 this.headers.put(headerName, current);
121 }
122
123 @Override
124 public void setStatus(final int value) {
125 this.status = value;
126 }
127
128
129
130
131
132
133 public String getStatusString() {
134 if (this.status == 401) {
135 return "Unauthorized";
136 }
137 return "Unknown";
138 }
139
140 @Override
141 public void flushBuffer() {
142 SimpleHttpResponse.LOGGER.info("{}: {}", Integer.valueOf(this.status), this.getStatusString());
143 for (final Map.Entry<String, List<String>> header : this.headers.entrySet()) {
144 for (final String headerValue : header.getValue()) {
145 SimpleHttpResponse.LOGGER.info("{}: {}", header, headerValue);
146 }
147 }
148 }
149
150
151
152
153
154
155 public int getHeaderNamesSize() {
156 return this.headers.size();
157 }
158
159
160
161
162
163
164
165
166
167 public String[] getHeaderValues(final String headerName) {
168 final List<String> headerValues = this.headers.get(headerName);
169 return headerValues == null ? null : headerValues.toArray(new String[0]);
170 }
171
172
173
174
175
176
177
178
179
180 @Override
181 public String getHeader(final String headerName) {
182 final List<String> headerValues = this.headers.get(headerName);
183 return headerValues == null ? null : String.join(", ", headerValues);
184 }
185
186 @Override
187 public void sendError(final int rc, final String message) {
188 this.status = rc;
189 }
190
191 @Override
192 public void sendError(final int rc) {
193 this.status = rc;
194 }
195
196 @Override
197 public PrintWriter getWriter() {
198 return this.writer;
199 }
200
201 @Override
202 public ServletOutputStream getOutputStream() throws IOException {
203 return this.out;
204 }
205
206
207
208
209
210
211 public String getOutputText() {
212 this.writer.flush();
213 return this.bytes.toString(StandardCharsets.UTF_8);
214 }
215 }