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