blob: 14ddd5903a0d6b4df09cfa0b7ea7551f946dede9 [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// Copyright 2012 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.gitiles;
16
Dave Borowitz9de65952012-08-13 16:09:45 -070017import static com.google.common.base.Preconditions.checkArgument;
18import static com.google.common.base.Preconditions.checkNotNull;
19import static com.google.gitiles.TestGitilesUrls.URLS;
David Pletcherd7bdaf32014-08-27 14:50:32 -070020import static java.nio.charset.StandardCharsets.UTF_8;
Dave Borowitz9de65952012-08-13 16:09:45 -070021
Dave Borowitzfe8fdab2014-11-04 16:19:33 -080022import com.google.common.base.Splitter;
Dave Borowitzfe8fdab2014-11-04 16:19:33 -080023import com.google.common.collect.Iterables;
24import com.google.common.collect.LinkedListMultimap;
25import com.google.common.collect.ListMultimap;
26import com.google.common.collect.Maps;
Dave Borowitz9de65952012-08-13 16:09:45 -070027import java.io.BufferedReader;
Gerrit Code Review5302fc02021-12-22 13:35:06 -080028import java.io.IOException;
Dave Borowitz9de65952012-08-13 16:09:45 -070029import java.io.UnsupportedEncodingException;
30import java.net.URLDecoder;
31import java.security.Principal;
32import java.util.Collection;
33import java.util.Collections;
34import java.util.Enumeration;
35import java.util.List;
36import java.util.Locale;
37import java.util.Map;
Gerrit Code Review5302fc02021-12-22 13:35:06 -080038import javax.servlet.AsyncContext;
39import javax.servlet.DispatcherType;
Dave Borowitz9de65952012-08-13 16:09:45 -070040import javax.servlet.RequestDispatcher;
Gerrit Code Review5302fc02021-12-22 13:35:06 -080041import javax.servlet.ServletContext;
42import javax.servlet.ServletException;
Dave Borowitz9de65952012-08-13 16:09:45 -070043import javax.servlet.ServletInputStream;
Gerrit Code Review5302fc02021-12-22 13:35:06 -080044import javax.servlet.ServletRequest;
45import javax.servlet.ServletResponse;
Dave Borowitz9de65952012-08-13 16:09:45 -070046import javax.servlet.http.Cookie;
47import javax.servlet.http.HttpServletRequest;
Gerrit Code Review5302fc02021-12-22 13:35:06 -080048import javax.servlet.http.HttpServletResponse;
Dave Borowitz9de65952012-08-13 16:09:45 -070049import javax.servlet.http.HttpSession;
Gerrit Code Review5302fc02021-12-22 13:35:06 -080050import javax.servlet.http.HttpUpgradeHandler;
51import javax.servlet.http.Part;
Dave Borowitz3b744b12016-08-19 16:11:10 -040052import org.eclipse.jgit.http.server.ServletUtils;
53import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
Dave Borowitz9de65952012-08-13 16:09:45 -070054
55/** Simple fake implementation of {@link HttpServletRequest}. */
56public class FakeHttpServletRequest implements HttpServletRequest {
57 public static final String SERVLET_PATH = "/b";
58
59 public static FakeHttpServletRequest newRequest() {
Dave Borowitzcf38c032016-05-02 11:06:23 -040060 return new FakeHttpServletRequest(URLS.getHostName(null), 80, "", SERVLET_PATH);
Dave Borowitz9de65952012-08-13 16:09:45 -070061 }
62
63 public static FakeHttpServletRequest newRequest(DfsRepository repo) {
64 FakeHttpServletRequest req = newRequest();
65 req.setAttribute(ServletUtils.ATTRIBUTE_REPOSITORY, repo);
66 return req;
67 }
68
69 private final Map<String, Object> attributes;
70 private final ListMultimap<String, String> headers;
71
72 private ListMultimap<String, String> parameters;
73 private String hostName;
74 private int port;
Shawn Pearce10e68e62016-01-02 09:37:58 -080075 private String method;
Dave Borowitz9de65952012-08-13 16:09:45 -070076 private String contextPath;
77 private String servletPath;
78 private String path;
79
Dave Borowitzcf38c032016-05-02 11:06:23 -040080 private FakeHttpServletRequest(
81 String hostName, int port, String contextPath, String servletPath) {
Dave Borowitz9de65952012-08-13 16:09:45 -070082 this.hostName = checkNotNull(hostName, "hostName");
83 checkArgument(port > 0);
84 this.port = port;
Shawn Pearce10e68e62016-01-02 09:37:58 -080085 this.method = "GET";
Dave Borowitz9de65952012-08-13 16:09:45 -070086 this.contextPath = checkNotNull(contextPath, "contextPath");
87 this.servletPath = checkNotNull(servletPath, "servletPath");
88 attributes = Maps.newConcurrentMap();
89 parameters = LinkedListMultimap.create();
90 headers = LinkedListMultimap.create();
91 }
92
93 @Override
94 public Object getAttribute(String name) {
95 return attributes.get(name);
96 }
97
98 @Override
99 public Enumeration<String> getAttributeNames() {
100 return Collections.enumeration(attributes.keySet());
101 }
102
103 @Override
104 public String getCharacterEncoding() {
105 return UTF_8.name();
106 }
107
108 @Override
109 public int getContentLength() {
110 return -1;
111 }
112
113 @Override
Gerrit Code Review5302fc02021-12-22 13:35:06 -0800114 public long getContentLengthLong() {
115 throw new UnsupportedOperationException();
116 }
117
118 @Override
Dave Borowitz9de65952012-08-13 16:09:45 -0700119 public String getContentType() {
120 return null;
121 }
122
123 @Override
124 public ServletInputStream getInputStream() {
125 throw new UnsupportedOperationException();
126 }
127
128 @Override
129 public String getLocalAddr() {
130 return "1.2.3.4";
131 }
132
133 @Override
134 public String getLocalName() {
135 return hostName;
136 }
137
138 @Override
139 public int getLocalPort() {
140 return port;
141 }
142
143 @Override
Gerrit Code Review5302fc02021-12-22 13:35:06 -0800144 public ServletContext getServletContext() {
145 throw new UnsupportedOperationException();
146 }
147
148 @Override
149 public AsyncContext startAsync() {
150 throw new UnsupportedOperationException();
151 }
152
153 @Override
154 public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) {
155 throw new UnsupportedOperationException();
156 }
157
158 @Override
159 public boolean isAsyncStarted() {
160 throw new UnsupportedOperationException();
161 }
162
163 @Override
164 public boolean isAsyncSupported() {
165 throw new UnsupportedOperationException();
166 }
167
168 @Override
169 public AsyncContext getAsyncContext() {
170 throw new UnsupportedOperationException();
171 }
172
173 @Override
174 public DispatcherType getDispatcherType() {
175 throw new UnsupportedOperationException();
176 }
177
178 @Override
Dave Borowitz9de65952012-08-13 16:09:45 -0700179 public Locale getLocale() {
180 return Locale.US;
181 }
182
183 @Override
184 public Enumeration<Locale> getLocales() {
185 return Collections.enumeration(Collections.singleton(Locale.US));
186 }
187
188 @Override
189 public String getParameter(String name) {
190 return Iterables.getFirst(parameters.get(name), null);
191 }
192
David Pursehousedcde0af2016-10-04 15:24:59 +0900193 private static final String[] stringCollectionToArray(Collection<String> values) {
194 return values.toArray(new String[0]);
195 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700196
197 @Override
198 public Map<String, String[]> getParameterMap() {
199 return Collections.unmodifiableMap(
David Pursehousedcde0af2016-10-04 15:24:59 +0900200 Maps.transformValues(parameters.asMap(), c -> stringCollectionToArray(c)));
Dave Borowitz9de65952012-08-13 16:09:45 -0700201 }
202
203 @Override
204 public Enumeration<String> getParameterNames() {
205 return Collections.enumeration(parameters.keySet());
206 }
207
208 @Override
209 public String[] getParameterValues(String name) {
David Pursehousedcde0af2016-10-04 15:24:59 +0900210 return stringCollectionToArray(parameters.get(name));
Dave Borowitz9de65952012-08-13 16:09:45 -0700211 }
212
213 public void setQueryString(String qs) {
214 ListMultimap<String, String> params = LinkedListMultimap.create();
215 for (String entry : Splitter.on('&').split(qs)) {
Dave Borowitz27058932014-12-03 15:44:46 -0800216 List<String> kv = Splitter.on('=').limit(2).splitToList(entry);
Dave Borowitz9de65952012-08-13 16:09:45 -0700217 try {
Dave Borowitzcf38c032016-05-02 11:06:23 -0400218 params.put(
219 URLDecoder.decode(kv.get(0), UTF_8.name()),
Dave Borowitz9de65952012-08-13 16:09:45 -0700220 kv.size() == 2 ? URLDecoder.decode(kv.get(1), UTF_8.name()) : "");
221 } catch (UnsupportedEncodingException e) {
222 throw new IllegalArgumentException(e);
223 }
224 }
225 parameters = params;
226 }
227
228 @Override
229 public String getProtocol() {
230 return "HTTP/1.1";
231 }
232
233 @Override
234 public BufferedReader getReader() {
235 throw new UnsupportedOperationException();
236 }
237
238 @Override
239 @Deprecated
240 public String getRealPath(String path) {
241 throw new UnsupportedOperationException();
242 }
243
244 @Override
245 public String getRemoteAddr() {
246 return "5.6.7.8";
247 }
248
249 @Override
250 public String getRemoteHost() {
251 return "remotehost";
252 }
253
254 @Override
255 public int getRemotePort() {
256 return 1234;
257 }
258
259 @Override
260 public RequestDispatcher getRequestDispatcher(String path) {
261 throw new UnsupportedOperationException();
262 }
263
264 @Override
265 public String getScheme() {
266 return port == 443 ? "https" : "http";
267 }
268
269 @Override
270 public String getServerName() {
271 return hostName;
272 }
273
274 @Override
275 public int getServerPort() {
276 return port;
277 }
278
279 @Override
280 public boolean isSecure() {
281 return port == 443;
282 }
283
284 @Override
285 public void removeAttribute(String name) {
286 attributes.remove(name);
287 }
288
289 @Override
290 public void setAttribute(String name, Object value) {
291 attributes.put(name, value);
292 }
293
294 @Override
295 public void setCharacterEncoding(String env) throws UnsupportedOperationException {
296 throw new UnsupportedOperationException();
297 }
298
299 @Override
300 public String getAuthType() {
301 return null;
302 }
303
304 @Override
305 public String getContextPath() {
306 return contextPath;
307 }
308
309 @Override
310 public Cookie[] getCookies() {
311 return new Cookie[0];
312 }
313
314 @Override
315 public long getDateHeader(String name) {
316 throw new UnsupportedOperationException();
317 }
318
319 @Override
320 public String getHeader(String name) {
321 return Iterables.getFirst(headers.get(name), null);
322 }
323
AJ Ross001ea9b2016-08-23 13:40:04 -0700324 public boolean setHeader(String name, String value) {
325 return headers.put(name, value);
326 }
327
Dave Borowitz9de65952012-08-13 16:09:45 -0700328 @Override
329 public Enumeration<String> getHeaderNames() {
330 return Collections.enumeration(headers.keySet());
331 }
332
333 @Override
334 public Enumeration<String> getHeaders(String name) {
335 return Collections.enumeration(headers.get(name));
336 }
337
338 @Override
339 public int getIntHeader(String name) {
340 return Integer.parseInt(getHeader(name));
341 }
342
343 @Override
344 public String getMethod() {
Shawn Pearce10e68e62016-01-02 09:37:58 -0800345 return method;
346 }
347
348 public void setMethod(String m) {
349 method = m;
Dave Borowitz9de65952012-08-13 16:09:45 -0700350 }
351
352 @Override
353 public String getPathInfo() {
354 return path;
355 }
356
357 public void setPathInfo(String path) {
358 this.path = checkNotNull(path);
359 }
360
361 @Override
362 public String getPathTranslated() {
363 return path;
364 }
365
366 @Override
367 public String getQueryString() {
Dave Borowitzd91bdf72013-01-10 20:07:32 -0800368 return GitilesView.paramsToString(parameters);
Dave Borowitz9de65952012-08-13 16:09:45 -0700369 }
370
371 @Override
372 public String getRemoteUser() {
373 return null;
374 }
375
376 @Override
377 public String getRequestURI() {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800378 String uri = contextPath + servletPath + path;
379 if (!parameters.isEmpty()) {
380 uri += "?" + GitilesView.paramsToString(parameters);
381 }
382 return uri;
Dave Borowitz9de65952012-08-13 16:09:45 -0700383 }
384
385 @Override
386 public StringBuffer getRequestURL() {
387 return null;
388 }
389
390 @Override
391 public String getRequestedSessionId() {
392 return null;
393 }
394
395 @Override
396 public String getServletPath() {
397 return servletPath;
398 }
399
400 @Override
401 public HttpSession getSession() {
402 throw new UnsupportedOperationException();
403 }
404
405 @Override
Gerrit Code Review5302fc02021-12-22 13:35:06 -0800406 public String changeSessionId() {
407 throw new UnsupportedOperationException();
408 }
409
410 @Override
Dave Borowitz9de65952012-08-13 16:09:45 -0700411 public HttpSession getSession(boolean create) {
412 throw new UnsupportedOperationException();
413 }
414
415 @Override
416 public Principal getUserPrincipal() {
417 throw new UnsupportedOperationException();
418 }
419
420 @Override
421 public boolean isRequestedSessionIdFromCookie() {
422 throw new UnsupportedOperationException();
423 }
424
425 @Override
426 public boolean isRequestedSessionIdFromURL() {
427 throw new UnsupportedOperationException();
428 }
429
430 @Override
431 @Deprecated
432 public boolean isRequestedSessionIdFromUrl() {
433 throw new UnsupportedOperationException();
434 }
435
436 @Override
Gerrit Code Review5302fc02021-12-22 13:35:06 -0800437 public boolean authenticate(HttpServletResponse response) throws IOException, ServletException {
438 throw new UnsupportedOperationException();
439 }
440
441 @Override
442 public void login(String username, String password) throws ServletException {
443 throw new UnsupportedOperationException();
444 }
445
446 @Override
447 public void logout() throws ServletException {
448 throw new UnsupportedOperationException();
449 }
450
451 @Override
452 public Collection<Part> getParts() throws IOException, ServletException {
453 throw new UnsupportedOperationException();
454 }
455
456 @Override
457 public Part getPart(String name) throws IOException, ServletException {
458 throw new UnsupportedOperationException();
459 }
460
461 @Override
462 public <T extends HttpUpgradeHandler> T upgrade(Class<T> httpUpgradeHandlerClass)
463 throws IOException, ServletException {
464 throw new UnsupportedOperationException();
465 }
466
467 @Override
Dave Borowitz9de65952012-08-13 16:09:45 -0700468 public boolean isRequestedSessionIdValid() {
469 throw new UnsupportedOperationException();
470 }
471
472 @Override
473 public boolean isUserInRole(String role) {
474 throw new UnsupportedOperationException();
475 }
476}