blob: 2cf316df2422560e000579d794789e536bbe0b76 [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 Borowitze8a5e362013-01-14 16:07:26 -080017import static com.google.common.base.Preconditions.checkArgument;
Dave Borowitzd91bdf72013-01-10 20:07:32 -080018import static com.google.common.base.Preconditions.checkNotNull;
Dave Borowitze8a5e362013-01-14 16:07:26 -080019import static com.google.common.base.Preconditions.checkState;
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.collect.Iterables;
23import com.google.common.collect.LinkedListMultimap;
24import com.google.common.collect.ListMultimap;
25import com.google.common.net.HttpHeaders;
Dave Borowitze8a5e362013-01-14 16:07:26 -080026import java.io.ByteArrayOutputStream;
27import java.io.IOException;
David Pursehouse784216f2018-11-08 11:24:18 +090028import java.io.OutputStreamWriter;
Dave Borowitz9de65952012-08-13 16:09:45 -070029import java.io.PrintWriter;
Dave Borowitze8a5e362013-01-14 16:07:26 -080030import java.nio.charset.Charset;
Dave Borowitz9de65952012-08-13 16:09:45 -070031import java.util.Locale;
Dave Borowitz9de65952012-08-13 16:09:45 -070032import javax.servlet.ServletOutputStream;
33import javax.servlet.http.Cookie;
34import javax.servlet.http.HttpServletResponse;
Dave Borowitz3b744b12016-08-19 16:11:10 -040035import org.eclipse.jgit.util.RawParseUtils;
Dave Borowitz9de65952012-08-13 16:09:45 -070036
37/** Simple fake implementation of {@link HttpServletResponse}. */
38public class FakeHttpServletResponse implements HttpServletResponse {
Dave Borowitze8a5e362013-01-14 16:07:26 -080039 private final ByteArrayOutputStream actualBody = new ByteArrayOutputStream();
40 private final ListMultimap<String, String> headers = LinkedListMultimap.create();
Dave Borowitz9de65952012-08-13 16:09:45 -070041
Dave Borowitze8a5e362013-01-14 16:07:26 -080042 private int status = 200;
43 private boolean committed;
44 private ServletOutputStream outputStream;
45 private PrintWriter writer;
Dave Borowitz9de65952012-08-13 16:09:45 -070046
Dave Borowitzcf38c032016-05-02 11:06:23 -040047 public FakeHttpServletResponse() {}
Dave Borowitz9de65952012-08-13 16:09:45 -070048
49 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -080050 public synchronized void flushBuffer() throws IOException {
51 if (outputStream != null) {
52 outputStream.flush();
53 }
54 if (writer != null) {
55 writer.flush();
56 }
Dave Borowitz9de65952012-08-13 16:09:45 -070057 }
58
59 @Override
60 public int getBufferSize() {
61 throw new UnsupportedOperationException();
62 }
63
64 @Override
65 public String getCharacterEncoding() {
66 return UTF_8.name();
67 }
68
69 @Override
70 public String getContentType() {
71 return null;
72 }
73
74 @Override
75 public Locale getLocale() {
76 return Locale.US;
77 }
78
79 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -080080 public synchronized ServletOutputStream getOutputStream() {
81 checkState(writer == null, "getWriter() already called");
82 if (outputStream == null) {
David Pursehouse784216f2018-11-08 11:24:18 +090083 PrintWriter osWriter = newPrintWriter();
Dave Borowitzcf38c032016-05-02 11:06:23 -040084 outputStream =
85 new ServletOutputStream() {
86 @Override
87 public void write(int c) throws IOException {
88 osWriter.write(c);
89 osWriter.flush();
90 }
91 };
Dave Borowitze8a5e362013-01-14 16:07:26 -080092 }
93 return outputStream;
Dave Borowitz9de65952012-08-13 16:09:45 -070094 }
95
96 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -080097 public synchronized PrintWriter getWriter() {
98 checkState(outputStream == null, "getOutputStream() already called");
99 if (writer == null) {
David Pursehouse784216f2018-11-08 11:24:18 +0900100 writer = newPrintWriter();
Dave Borowitze8a5e362013-01-14 16:07:26 -0800101 }
102 return writer;
Dave Borowitz9de65952012-08-13 16:09:45 -0700103 }
104
105 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800106 public synchronized boolean isCommitted() {
107 return committed;
Dave Borowitz9de65952012-08-13 16:09:45 -0700108 }
109
110 @Override
111 public void reset() {
112 throw new UnsupportedOperationException();
113 }
114
115 @Override
116 public void resetBuffer() {
117 throw new UnsupportedOperationException();
118 }
119
120 @Override
121 public void setBufferSize(int sz) {
122 throw new UnsupportedOperationException();
123 }
124
125 @Override
126 public void setCharacterEncoding(String name) {
Dave Borowitzcf38c032016-05-02 11:06:23 -0400127 checkArgument(UTF_8.equals(Charset.forName(name)), "unsupported charset: %s", name);
Dave Borowitz9de65952012-08-13 16:09:45 -0700128 }
129
130 @Override
131 public void setContentLength(int length) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800132 headers.removeAll(HttpHeaders.CONTENT_LENGTH);
133 headers.put(HttpHeaders.CONTENT_LENGTH, Integer.toString(length));
Dave Borowitz9de65952012-08-13 16:09:45 -0700134 }
135
136 @Override
137 public void setContentType(String type) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800138 headers.removeAll(HttpHeaders.CONTENT_TYPE);
139 headers.put(HttpHeaders.CONTENT_TYPE, type);
Dave Borowitz9de65952012-08-13 16:09:45 -0700140 }
141
142 @Override
143 public void setLocale(Locale locale) {
144 throw new UnsupportedOperationException();
145 }
146
147 @Override
148 public void addCookie(Cookie cookie) {
149 throw new UnsupportedOperationException();
150 }
151
152 @Override
153 public void addDateHeader(String name, long value) {
154 throw new UnsupportedOperationException();
155 }
156
157 @Override
158 public void addHeader(String name, String value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800159 headers.put(name, value);
Dave Borowitz9de65952012-08-13 16:09:45 -0700160 }
161
162 @Override
163 public void addIntHeader(String name, int value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800164 headers.put(name, Integer.toString(value));
Dave Borowitz9de65952012-08-13 16:09:45 -0700165 }
166
167 @Override
168 public boolean containsHeader(String name) {
Dave Borowitz27058932014-12-03 15:44:46 -0800169 return headers.containsKey(name);
Dave Borowitz9de65952012-08-13 16:09:45 -0700170 }
171
172 @Override
173 public String encodeRedirectURL(String url) {
174 throw new UnsupportedOperationException();
175 }
176
177 @Override
178 @Deprecated
179 public String encodeRedirectUrl(String url) {
180 throw new UnsupportedOperationException();
181 }
182
183 @Override
184 public String encodeURL(String url) {
185 throw new UnsupportedOperationException();
186 }
187
188 @Override
189 @Deprecated
190 public String encodeUrl(String url) {
191 throw new UnsupportedOperationException();
192 }
193
194 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800195 public synchronized void sendError(int sc) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700196 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800197 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700198 }
199
200 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800201 public synchronized void sendError(int sc, String msg) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700202 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800203 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700204 }
205
206 @Override
Dave Borowitz5d5619d2014-04-18 17:01:45 -0700207 public synchronized void sendRedirect(String loc) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700208 status = SC_FOUND;
Dave Borowitz5d5619d2014-04-18 17:01:45 -0700209 setHeader(HttpHeaders.LOCATION, loc);
Dave Borowitze8a5e362013-01-14 16:07:26 -0800210 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700211 }
212
213 @Override
214 public void setDateHeader(String name, long value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800215 setHeader(name, Long.toString(value));
Dave Borowitz9de65952012-08-13 16:09:45 -0700216 }
217
218 @Override
219 public void setHeader(String name, String value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800220 headers.removeAll(name);
221 addHeader(name, value);
Dave Borowitz9de65952012-08-13 16:09:45 -0700222 }
223
224 @Override
225 public void setIntHeader(String name, int value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800226 headers.removeAll(name);
227 addIntHeader(name, value);
Dave Borowitz9de65952012-08-13 16:09:45 -0700228 }
229
230 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800231 public synchronized void setStatus(int sc) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700232 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800233 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700234 }
235
236 @Override
237 @Deprecated
Dave Borowitze8a5e362013-01-14 16:07:26 -0800238 public synchronized void setStatus(int sc, String msg) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700239 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800240 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700241 }
242
Dave Borowitze8a5e362013-01-14 16:07:26 -0800243 public synchronized int getStatus() {
Dave Borowitz9de65952012-08-13 16:09:45 -0700244 return status;
245 }
Dave Borowitze8a5e362013-01-14 16:07:26 -0800246
247 public byte[] getActualBody() {
248 return actualBody.toByteArray();
249 }
250
251 public String getActualBodyString() {
252 return RawParseUtils.decode(getActualBody());
253 }
Dave Borowitzd91bdf72013-01-10 20:07:32 -0800254
255 public String getHeader(String name) {
256 return Iterables.getFirst(headers.get(checkNotNull(name)), null);
257 }
David Pursehouse784216f2018-11-08 11:24:18 +0900258
259 private PrintWriter newPrintWriter() {
260 return new PrintWriter(new OutputStreamWriter(actualBody, UTF_8));
261 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700262}