blob: 343b27057a1e68ade5c13a4f5bd383308dc7aeb6 [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.Function;
23import com.google.common.base.Splitter;
Dave Borowitzfe8fdab2014-11-04 16:19:33 -080024import com.google.common.collect.Iterables;
25import com.google.common.collect.LinkedListMultimap;
26import com.google.common.collect.ListMultimap;
27import com.google.common.collect.Maps;
28
29import org.eclipse.jgit.http.server.ServletUtils;
30import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
31
Dave Borowitz9de65952012-08-13 16:09:45 -070032import java.io.BufferedReader;
33import java.io.UnsupportedEncodingException;
34import java.net.URLDecoder;
35import java.security.Principal;
36import java.util.Collection;
37import java.util.Collections;
38import java.util.Enumeration;
39import java.util.List;
40import java.util.Locale;
41import java.util.Map;
42
43import javax.servlet.RequestDispatcher;
44import javax.servlet.ServletInputStream;
45import javax.servlet.http.Cookie;
46import javax.servlet.http.HttpServletRequest;
47import javax.servlet.http.HttpSession;
48
49/** Simple fake implementation of {@link HttpServletRequest}. */
50public class FakeHttpServletRequest implements HttpServletRequest {
51 public static final String SERVLET_PATH = "/b";
52
53 public static FakeHttpServletRequest newRequest() {
54 return new FakeHttpServletRequest(
55 URLS.getHostName(null),
56 80,
57 "",
Dave Borowitz33d4fda2013-10-22 16:40:20 -070058 SERVLET_PATH);
Dave Borowitz9de65952012-08-13 16:09:45 -070059 }
60
61 public static FakeHttpServletRequest newRequest(DfsRepository repo) {
62 FakeHttpServletRequest req = newRequest();
63 req.setAttribute(ServletUtils.ATTRIBUTE_REPOSITORY, repo);
64 return req;
65 }
66
67 private final Map<String, Object> attributes;
68 private final ListMultimap<String, String> headers;
69
70 private ListMultimap<String, String> parameters;
71 private String hostName;
72 private int port;
Shawn Pearce10e68e62016-01-02 09:37:58 -080073 private String method;
Dave Borowitz9de65952012-08-13 16:09:45 -070074 private String contextPath;
75 private String servletPath;
76 private String path;
77
Dave Borowitz33d4fda2013-10-22 16:40:20 -070078 private FakeHttpServletRequest(String hostName, int port, String contextPath,
79 String servletPath) {
Dave Borowitz9de65952012-08-13 16:09:45 -070080 this.hostName = checkNotNull(hostName, "hostName");
81 checkArgument(port > 0);
82 this.port = port;
Shawn Pearce10e68e62016-01-02 09:37:58 -080083 this.method = "GET";
Dave Borowitz9de65952012-08-13 16:09:45 -070084 this.contextPath = checkNotNull(contextPath, "contextPath");
85 this.servletPath = checkNotNull(servletPath, "servletPath");
86 attributes = Maps.newConcurrentMap();
87 parameters = LinkedListMultimap.create();
88 headers = LinkedListMultimap.create();
89 }
90
91 @Override
92 public Object getAttribute(String name) {
93 return attributes.get(name);
94 }
95
96 @Override
97 public Enumeration<String> getAttributeNames() {
98 return Collections.enumeration(attributes.keySet());
99 }
100
101 @Override
102 public String getCharacterEncoding() {
103 return UTF_8.name();
104 }
105
106 @Override
107 public int getContentLength() {
108 return -1;
109 }
110
111 @Override
112 public String getContentType() {
113 return null;
114 }
115
116 @Override
117 public ServletInputStream getInputStream() {
118 throw new UnsupportedOperationException();
119 }
120
121 @Override
122 public String getLocalAddr() {
123 return "1.2.3.4";
124 }
125
126 @Override
127 public String getLocalName() {
128 return hostName;
129 }
130
131 @Override
132 public int getLocalPort() {
133 return port;
134 }
135
136 @Override
137 public Locale getLocale() {
138 return Locale.US;
139 }
140
141 @Override
142 public Enumeration<Locale> getLocales() {
143 return Collections.enumeration(Collections.singleton(Locale.US));
144 }
145
146 @Override
147 public String getParameter(String name) {
148 return Iterables.getFirst(parameters.get(name), null);
149 }
150
151 private static final Function<Collection<String>, String[]> STRING_COLLECTION_TO_ARRAY =
152 new Function<Collection<String>, String[]>() {
153 @Override
154 public String[] apply(Collection<String> values) {
155 return values.toArray(new String[0]);
156 }
157 };
158
159 @Override
160 public Map<String, String[]> getParameterMap() {
161 return Collections.unmodifiableMap(
162 Maps.transformValues(parameters.asMap(), STRING_COLLECTION_TO_ARRAY));
163 }
164
165 @Override
166 public Enumeration<String> getParameterNames() {
167 return Collections.enumeration(parameters.keySet());
168 }
169
170 @Override
171 public String[] getParameterValues(String name) {
172 return STRING_COLLECTION_TO_ARRAY.apply(parameters.get(name));
173 }
174
175 public void setQueryString(String qs) {
176 ListMultimap<String, String> params = LinkedListMultimap.create();
177 for (String entry : Splitter.on('&').split(qs)) {
Dave Borowitz27058932014-12-03 15:44:46 -0800178 List<String> kv = Splitter.on('=').limit(2).splitToList(entry);
Dave Borowitz9de65952012-08-13 16:09:45 -0700179 try {
180 params.put(URLDecoder.decode(kv.get(0), UTF_8.name()),
181 kv.size() == 2 ? URLDecoder.decode(kv.get(1), UTF_8.name()) : "");
182 } catch (UnsupportedEncodingException e) {
183 throw new IllegalArgumentException(e);
184 }
185 }
186 parameters = params;
187 }
188
189 @Override
190 public String getProtocol() {
191 return "HTTP/1.1";
192 }
193
194 @Override
195 public BufferedReader getReader() {
196 throw new UnsupportedOperationException();
197 }
198
199 @Override
200 @Deprecated
201 public String getRealPath(String path) {
202 throw new UnsupportedOperationException();
203 }
204
205 @Override
206 public String getRemoteAddr() {
207 return "5.6.7.8";
208 }
209
210 @Override
211 public String getRemoteHost() {
212 return "remotehost";
213 }
214
215 @Override
216 public int getRemotePort() {
217 return 1234;
218 }
219
220 @Override
221 public RequestDispatcher getRequestDispatcher(String path) {
222 throw new UnsupportedOperationException();
223 }
224
225 @Override
226 public String getScheme() {
227 return port == 443 ? "https" : "http";
228 }
229
230 @Override
231 public String getServerName() {
232 return hostName;
233 }
234
235 @Override
236 public int getServerPort() {
237 return port;
238 }
239
240 @Override
241 public boolean isSecure() {
242 return port == 443;
243 }
244
245 @Override
246 public void removeAttribute(String name) {
247 attributes.remove(name);
248 }
249
250 @Override
251 public void setAttribute(String name, Object value) {
252 attributes.put(name, value);
253 }
254
255 @Override
256 public void setCharacterEncoding(String env) throws UnsupportedOperationException {
257 throw new UnsupportedOperationException();
258 }
259
260 @Override
261 public String getAuthType() {
262 return null;
263 }
264
265 @Override
266 public String getContextPath() {
267 return contextPath;
268 }
269
270 @Override
271 public Cookie[] getCookies() {
272 return new Cookie[0];
273 }
274
275 @Override
276 public long getDateHeader(String name) {
277 throw new UnsupportedOperationException();
278 }
279
280 @Override
281 public String getHeader(String name) {
282 return Iterables.getFirst(headers.get(name), null);
283 }
284
285 @Override
286 public Enumeration<String> getHeaderNames() {
287 return Collections.enumeration(headers.keySet());
288 }
289
290 @Override
291 public Enumeration<String> getHeaders(String name) {
292 return Collections.enumeration(headers.get(name));
293 }
294
295 @Override
296 public int getIntHeader(String name) {
297 return Integer.parseInt(getHeader(name));
298 }
299
300 @Override
301 public String getMethod() {
Shawn Pearce10e68e62016-01-02 09:37:58 -0800302 return method;
303 }
304
305 public void setMethod(String m) {
306 method = m;
Dave Borowitz9de65952012-08-13 16:09:45 -0700307 }
308
309 @Override
310 public String getPathInfo() {
311 return path;
312 }
313
314 public void setPathInfo(String path) {
315 this.path = checkNotNull(path);
316 }
317
318 @Override
319 public String getPathTranslated() {
320 return path;
321 }
322
323 @Override
324 public String getQueryString() {
Dave Borowitzd91bdf72013-01-10 20:07:32 -0800325 return GitilesView.paramsToString(parameters);
Dave Borowitz9de65952012-08-13 16:09:45 -0700326 }
327
328 @Override
329 public String getRemoteUser() {
330 return null;
331 }
332
333 @Override
334 public String getRequestURI() {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800335 String uri = contextPath + servletPath + path;
336 if (!parameters.isEmpty()) {
337 uri += "?" + GitilesView.paramsToString(parameters);
338 }
339 return uri;
Dave Borowitz9de65952012-08-13 16:09:45 -0700340 }
341
342 @Override
343 public StringBuffer getRequestURL() {
344 return null;
345 }
346
347 @Override
348 public String getRequestedSessionId() {
349 return null;
350 }
351
352 @Override
353 public String getServletPath() {
354 return servletPath;
355 }
356
357 @Override
358 public HttpSession getSession() {
359 throw new UnsupportedOperationException();
360 }
361
362 @Override
363 public HttpSession getSession(boolean create) {
364 throw new UnsupportedOperationException();
365 }
366
367 @Override
368 public Principal getUserPrincipal() {
369 throw new UnsupportedOperationException();
370 }
371
372 @Override
373 public boolean isRequestedSessionIdFromCookie() {
374 throw new UnsupportedOperationException();
375 }
376
377 @Override
378 public boolean isRequestedSessionIdFromURL() {
379 throw new UnsupportedOperationException();
380 }
381
382 @Override
383 @Deprecated
384 public boolean isRequestedSessionIdFromUrl() {
385 throw new UnsupportedOperationException();
386 }
387
388 @Override
389 public boolean isRequestedSessionIdValid() {
390 throw new UnsupportedOperationException();
391 }
392
393 @Override
394 public boolean isUserInRole(String role) {
395 throw new UnsupportedOperationException();
396 }
397}