1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.util.http.testutil;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.collect.Iterables;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.net.HttpHeaders;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Locale;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jgit.util.RawParseUtils;
/** Simple fake implementation of {@link HttpServletResponse}. */
public class FakeHttpServletResponse implements HttpServletResponse {
private final ByteArrayOutputStream actualBody = new ByteArrayOutputStream();
private final ListMultimap<String, String> headers = LinkedListMultimap.create();
private int status = SC_OK;
private boolean committed;
private ServletOutputStream outputStream;
private PrintWriter writer;
public FakeHttpServletResponse() {}
@Override
public synchronized void flushBuffer() throws IOException {
if (outputStream != null) {
outputStream.flush();
}
if (writer != null) {
writer.flush();
}
}
@Override
public int getBufferSize() {
throw new UnsupportedOperationException();
}
@Override
public String getCharacterEncoding() {
return UTF_8.name();
}
@Override
public String getContentType() {
return null;
}
@Override
public Locale getLocale() {
return Locale.US;
}
@Override
public synchronized ServletOutputStream getOutputStream() {
checkState(writer == null, "getWriter() already called");
if (outputStream == null) {
outputStream =
new ServletOutputStream() {
@Override
public void write(int c) throws IOException {
actualBody.write(c);
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener listener) {
throw new UnsupportedOperationException();
}
};
}
return outputStream;
}
@Override
public synchronized PrintWriter getWriter() {
checkState(outputStream == null, "getOutputStream() already called");
if (writer == null) {
writer = new PrintWriter(new OutputStreamWriter(actualBody, UTF_8));
}
return writer;
}
@Override
public synchronized boolean isCommitted() {
return committed;
}
@Override
public void reset() {
throw new UnsupportedOperationException();
}
@Override
public void resetBuffer() {
throw new UnsupportedOperationException();
}
@Override
public void setBufferSize(int sz) {
throw new UnsupportedOperationException();
}
@Override
public void setCharacterEncoding(String name) {
checkArgument(UTF_8.equals(Charset.forName(name)), "unsupported charset: %s", name);
}
@Override
public void setContentLength(int length) {
setContentLengthLong(length);
}
@Override
public void setContentLengthLong(long length) {
headers.removeAll(HttpHeaders.CONTENT_LENGTH);
addHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(length));
}
@Override
public void setContentType(String type) {
headers.removeAll(HttpHeaders.CONTENT_TYPE);
addHeader(HttpHeaders.CONTENT_TYPE, type);
}
@Override
public void setLocale(Locale locale) {
throw new UnsupportedOperationException();
}
@Override
public void addCookie(Cookie cookie) {
throw new UnsupportedOperationException();
}
@Override
public void addDateHeader(String name, long value) {
throw new UnsupportedOperationException();
}
@Override
public void addHeader(String name, String value) {
headers.put(name.toLowerCase(), value);
}
@Override
public void addIntHeader(String name, int value) {
addHeader(name, Integer.toString(value));
}
@Override
public boolean containsHeader(String name) {
return headers.containsKey(name.toLowerCase());
}
@Override
public String encodeRedirectURL(String url) {
throw new UnsupportedOperationException();
}
@Override
@Deprecated
public String encodeRedirectUrl(String url) {
throw new UnsupportedOperationException();
}
@Override
public String encodeURL(String url) {
throw new UnsupportedOperationException();
}
@Override
@Deprecated
public String encodeUrl(String url) {
throw new UnsupportedOperationException();
}
@Override
public synchronized void sendError(int sc) {
status = sc;
committed = true;
}
@Override
public synchronized void sendError(int sc, String msg) {
status = sc;
committed = true;
}
@Override
public synchronized void sendRedirect(String loc) {
status = SC_FOUND;
setHeader(HttpHeaders.LOCATION, loc);
committed = true;
}
@Override
public void setDateHeader(String name, long value) {
setHeader(name, Long.toString(value));
}
@Override
public void setHeader(String name, String value) {
headers.removeAll(name.toLowerCase());
addHeader(name, value);
}
@Override
public void setIntHeader(String name, int value) {
headers.removeAll(name.toLowerCase());
addIntHeader(name, value);
}
@Override
public synchronized void setStatus(int sc) {
status = sc;
committed = true;
}
@Override
@Deprecated
public synchronized void setStatus(int sc, String msg) {
status = sc;
committed = true;
}
@Override
public synchronized int getStatus() {
return status;
}
@Override
public String getHeader(String name) {
return Iterables.getFirst(headers.get(checkNotNull(name.toLowerCase())), null);
}
@Override
public Collection<String> getHeaderNames() {
return headers.keySet();
}
@Override
public Collection<String> getHeaders(String name) {
return headers.get(checkNotNull(name.toLowerCase()));
}
public byte[] getActualBody() {
return actualBody.toByteArray();
}
public String getActualBodyString() {
return RawParseUtils.decode(getActualBody());
}
}
|