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