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.http.HttpServletRequest;
27 import jakarta.servlet.http.HttpServletRequestWrapper;
28 import jakarta.servlet.http.HttpSession;
29
30 import java.security.Principal;
31 import java.util.Collections;
32 import java.util.Enumeration;
33 import java.util.HashMap;
34 import java.util.Map;
35
36 import org.mockito.Mockito;
37
38
39
40
41 public class SimpleHttpRequest extends HttpServletRequestWrapper {
42
43
44 private static int remotePortS = 0;
45
46
47 private String requestURI;
48
49
50 private String queryString;
51
52
53 private String remoteUser;
54
55
56 private String method = "GET";
57
58
59 private String remoteHost;
60
61
62 private String remoteAddr;
63
64
65 private int remotePort = -1;
66
67
68 private final Map<String, String> headers = new HashMap<>();
69
70
71 private final Map<String, String> parameters = new HashMap<>();
72
73
74 private byte[] content;
75
76
77 private HttpSession session = new SimpleHttpSession();
78
79
80 private Principal principal;
81
82
83
84
85 public SimpleHttpRequest() {
86 super(Mockito.mock(HttpServletRequest.class));
87 this.remotePort = SimpleHttpRequest.nextRemotePort();
88 }
89
90
91
92
93
94
95 public static synchronized int nextRemotePort() {
96 return ++SimpleHttpRequest.remotePortS;
97 }
98
99
100
101
102 public static synchronized void resetRemotePort() {
103 SimpleHttpRequest.remotePortS = 0;
104 }
105
106
107
108
109
110
111
112
113
114 public void addHeader(final String headerName, final String headerValue) {
115 this.headers.put(headerName, headerValue);
116 }
117
118 @Override
119 public String getHeader(final String headerName) {
120 return this.headers.get(headerName);
121 }
122
123 @Override
124 public Enumeration<String> getHeaderNames() {
125 return Collections.enumeration(this.headers.keySet());
126 }
127
128 @Override
129 public String getMethod() {
130 return this.method;
131 }
132
133 @Override
134 public int getContentLength() {
135 return this.content == null ? -1 : this.content.length;
136 }
137
138 @Override
139 public int getRemotePort() {
140 return this.remotePort;
141 }
142
143
144
145
146
147
148
149 public void setMethod(final String methodName) {
150 this.method = methodName;
151 }
152
153
154
155
156
157
158
159 public void setContentLength(final int length) {
160 this.content = new byte[length];
161 }
162
163
164
165
166
167
168
169 public void setRemoteUser(final String username) {
170 this.remoteUser = username;
171 }
172
173 @Override
174 public String getRemoteUser() {
175 return this.remoteUser;
176 }
177
178 @Override
179 public HttpSession getSession() {
180 return this.session;
181 }
182
183 @Override
184 public HttpSession getSession(final boolean create) {
185 if (this.session == null && create) {
186 this.session = new SimpleHttpSession();
187 }
188 return this.session;
189 }
190
191 @Override
192 public String getQueryString() {
193 return this.queryString;
194 }
195
196
197
198
199
200
201
202 public void setQueryString(final String query) {
203 this.queryString = query;
204 if (this.queryString != null) {
205 for (final String eachParameter : this.queryString.split("[&]", -1)) {
206 final String[] pair = eachParameter.split("=", -1);
207 final String value = pair.length == 2 ? pair[1] : "";
208 this.addParameter(pair[0], value);
209 }
210 }
211 }
212
213
214
215
216
217
218
219 public void setRequestURI(final String uri) {
220 this.requestURI = uri;
221 }
222
223 @Override
224 public String getRequestURI() {
225 return this.requestURI;
226 }
227
228 @Override
229 public String getParameter(final String parameterName) {
230 return this.parameters.get(parameterName);
231 }
232
233
234
235
236
237
238
239
240
241 public void addParameter(final String parameterName, final String parameterValue) {
242 this.parameters.put(parameterName, parameterValue);
243 }
244
245 @Override
246 public String getRemoteHost() {
247 return this.remoteHost;
248 }
249
250
251
252
253
254
255
256 public void setRemoteHost(final String value) {
257 this.remoteHost = value;
258 }
259
260 @Override
261 public String getRemoteAddr() {
262 return this.remoteAddr;
263 }
264
265
266
267
268
269
270
271 public void setRemoteAddr(final String value) {
272 this.remoteAddr = value;
273 }
274
275 @Override
276 public Principal getUserPrincipal() {
277 return this.principal;
278 }
279
280
281
282
283
284
285
286 public void setUserPrincipal(final Principal value) {
287 this.principal = value;
288 }
289 }