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