1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
package org.apache.tapestry.test.mock;
|
16
|
|
|
17
|
|
import java.io.ByteArrayOutputStream;
|
18
|
|
import java.io.IOException;
|
19
|
|
import java.io.PrintWriter;
|
20
|
|
import java.io.UnsupportedEncodingException;
|
21
|
|
import java.util.*;
|
22
|
|
|
23
|
|
import javax.servlet.ServletOutputStream;
|
24
|
|
import javax.servlet.http.Cookie;
|
25
|
|
import javax.servlet.http.HttpServletResponse;
|
26
|
|
|
27
|
|
|
28
|
|
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
public class MockResponse implements HttpServletResponse
|
36
|
|
{
|
37
|
|
private MockRequest _request;
|
38
|
|
private boolean _commited = false;
|
39
|
|
private ByteArrayOutputStream _outputByteStream;
|
40
|
|
private ServletOutputStream _outputStream;
|
41
|
|
private String _outputString;
|
42
|
|
private List _cookies = new ArrayList();
|
43
|
|
|
44
|
|
private String _redirectLocation;
|
45
|
|
private String _contentType;
|
46
|
|
|
47
|
|
private class ServletOutputStreamImpl extends ServletOutputStream
|
48
|
|
{
|
49
|
195
|
private ServletOutputStreamImpl()
|
50
|
|
{
|
51
|
|
}
|
52
|
|
|
53
|
197
|
public void close() throws IOException
|
54
|
|
{
|
55
|
197
|
super.close();
|
56
|
|
|
57
|
197
|
if (_outputByteStream != null)
|
58
|
197
|
_outputByteStream.close();
|
59
|
|
}
|
60
|
|
|
61
|
233
|
public void write(byte[] b, int off, int len) throws IOException
|
62
|
|
{
|
63
|
233
|
commit();
|
64
|
|
|
65
|
233
|
_outputByteStream.write(b, off, len);
|
66
|
|
}
|
67
|
|
|
68
|
0
|
public void write(byte[] b) throws IOException
|
69
|
|
{
|
70
|
0
|
commit();
|
71
|
|
|
72
|
0
|
_outputByteStream.write(b);
|
73
|
|
}
|
74
|
|
|
75
|
0
|
public void write(int b) throws IOException
|
76
|
|
{
|
77
|
0
|
commit();
|
78
|
|
|
79
|
0
|
_outputByteStream.write(b);
|
80
|
|
}
|
81
|
|
|
82
|
233
|
private void commit()
|
83
|
|
{
|
84
|
233
|
if (!_commited)
|
85
|
|
{
|
86
|
195
|
_commited = true;
|
87
|
195
|
_outputByteStream = new ByteArrayOutputStream();
|
88
|
|
}
|
89
|
|
}
|
90
|
|
}
|
91
|
|
|
92
|
196
|
public MockResponse(MockRequest request)
|
93
|
|
{
|
94
|
196
|
_request = request;
|
95
|
|
}
|
96
|
|
|
97
|
12
|
public void addCookie(Cookie cookie)
|
98
|
|
{
|
99
|
12
|
_cookies.add(cookie);
|
100
|
|
}
|
101
|
|
|
102
|
0
|
public boolean containsHeader(String arg0)
|
103
|
|
{
|
104
|
0
|
return false;
|
105
|
|
}
|
106
|
|
|
107
|
185
|
public String encodeURL(String path)
|
108
|
|
{
|
109
|
185
|
return path;
|
110
|
|
}
|
111
|
|
|
112
|
2
|
public String encodeRedirectURL(String path)
|
113
|
|
{
|
114
|
2
|
return path;
|
115
|
|
}
|
116
|
|
|
117
|
0
|
public String encodeUrl(String path)
|
118
|
|
{
|
119
|
0
|
return encodeURL(path);
|
120
|
|
}
|
121
|
|
|
122
|
0
|
public String encodeRedirectUrl(String path)
|
123
|
|
{
|
124
|
0
|
return encodeRedirectURL(path);
|
125
|
|
}
|
126
|
|
|
127
|
0
|
public void sendError(int code, String message) throws IOException
|
128
|
|
{
|
129
|
0
|
if (_commited)
|
130
|
0
|
throw new IllegalStateException("sendError() when committed.");
|
131
|
|
}
|
132
|
|
|
133
|
0
|
public void sendError(int code) throws IOException
|
134
|
|
{
|
135
|
0
|
sendError(code, null);
|
136
|
|
}
|
137
|
|
|
138
|
2
|
public void sendRedirect(String location) throws IOException
|
139
|
|
{
|
140
|
2
|
if (_commited)
|
141
|
0
|
throw new IllegalStateException("sendRedirect() when committed.");
|
142
|
|
|
143
|
2
|
if (location.endsWith("/FAIL_IO"))
|
144
|
1
|
throw new IOException("Forced IOException in MockResponse.sendRedirect().");
|
145
|
|
|
146
|
1
|
_redirectLocation = location;
|
147
|
|
|
148
|
1
|
_commited = true;
|
149
|
|
|
150
|
|
}
|
151
|
|
|
152
|
1
|
public String getRedirectLocation()
|
153
|
|
{
|
154
|
1
|
return _redirectLocation;
|
155
|
|
}
|
156
|
|
|
157
|
0
|
public void setDateHeader(String name, long value)
|
158
|
|
{
|
159
|
|
}
|
160
|
|
|
161
|
0
|
public void addDateHeader(String name, long value)
|
162
|
|
{
|
163
|
|
}
|
164
|
|
|
165
|
0
|
public void setHeader(String name, String value)
|
166
|
|
{
|
167
|
|
}
|
168
|
|
|
169
|
0
|
public void addHeader(String name, String value)
|
170
|
|
{
|
171
|
|
}
|
172
|
|
|
173
|
0
|
public void setIntHeader(String name, int value)
|
174
|
|
{
|
175
|
|
}
|
176
|
|
|
177
|
0
|
public void addIntHeader(String name, int value)
|
178
|
|
{
|
179
|
|
}
|
180
|
|
|
181
|
0
|
public void setStatus(int name)
|
182
|
|
{
|
183
|
|
}
|
184
|
|
|
185
|
0
|
public void setStatus(int name, String arg1)
|
186
|
|
{
|
187
|
|
}
|
188
|
|
|
189
|
0
|
public String getCharacterEncoding()
|
190
|
|
{
|
191
|
0
|
return null;
|
192
|
|
}
|
193
|
|
|
194
|
195
|
public ServletOutputStream getOutputStream() throws IOException
|
195
|
|
{
|
196
|
195
|
if (_outputStream != null)
|
197
|
0
|
throw new IllegalStateException("getOutputStream() invoked more than once.");
|
198
|
|
|
199
|
195
|
_outputStream = new ServletOutputStreamImpl();
|
200
|
|
|
201
|
195
|
return _outputStream;
|
202
|
|
}
|
203
|
|
|
204
|
11
|
public PrintWriter getWriter() throws IOException
|
205
|
|
{
|
206
|
11
|
return new PrintWriter(getOutputStream());
|
207
|
|
}
|
208
|
|
|
209
|
2
|
public void setContentLength(int arg0)
|
210
|
|
{
|
211
|
|
}
|
212
|
|
|
213
|
184
|
public void setContentType(String contentType)
|
214
|
|
{
|
215
|
184
|
_contentType = contentType;
|
216
|
|
}
|
217
|
|
|
218
|
0
|
public void setBufferSize(int arg0)
|
219
|
|
{
|
220
|
|
}
|
221
|
|
|
222
|
0
|
public int getBufferSize()
|
223
|
|
{
|
224
|
0
|
return 0;
|
225
|
|
}
|
226
|
|
|
227
|
0
|
public void flushBuffer() throws IOException
|
228
|
|
{
|
229
|
|
}
|
230
|
|
|
231
|
0
|
public void resetBuffer()
|
232
|
|
{
|
233
|
|
}
|
234
|
|
|
235
|
0
|
public boolean isCommitted()
|
236
|
|
{
|
237
|
0
|
return _commited;
|
238
|
|
}
|
239
|
|
|
240
|
0
|
public void reset()
|
241
|
|
{
|
242
|
|
}
|
243
|
|
|
244
|
0
|
public void setLocale(Locale arg0)
|
245
|
|
{
|
246
|
|
}
|
247
|
|
|
248
|
0
|
public Locale getLocale()
|
249
|
|
{
|
250
|
0
|
return null;
|
251
|
|
}
|
252
|
|
|
253
|
|
|
254
|
|
|
255
|
|
|
256
|
|
|
257
|
|
|
258
|
|
|
259
|
185
|
public void end() throws IOException
|
260
|
|
{
|
261
|
|
|
262
|
|
|
263
|
185
|
if (_outputStream != null)
|
264
|
184
|
_outputStream.close();
|
265
|
|
}
|
266
|
|
|
267
|
|
|
268
|
|
|
269
|
|
|
270
|
|
|
271
|
|
|
272
|
424
|
public String getOutputString()
|
273
|
|
{
|
274
|
424
|
if (_outputString != null)
|
275
|
228
|
return _outputString;
|
276
|
|
|
277
|
196
|
if (_outputByteStream == null)
|
278
|
1
|
return null;
|
279
|
|
|
280
|
195
|
try
|
281
|
|
{
|
282
|
195
|
String encoding = _request.getCharacterEncoding();
|
283
|
|
|
284
|
195
|
if (encoding != null)
|
285
|
184
|
_outputString = new String(_outputByteStream.toByteArray(), encoding);
|
286
|
|
}
|
287
|
|
catch (UnsupportedEncodingException e)
|
288
|
|
{
|
289
|
|
}
|
290
|
|
|
291
|
195
|
if (_outputString == null)
|
292
|
11
|
_outputString = _outputByteStream.toString();
|
293
|
|
|
294
|
195
|
return _outputString;
|
295
|
|
}
|
296
|
|
|
297
|
3
|
public byte[] getResponseBytes()
|
298
|
|
{
|
299
|
3
|
return _outputByteStream.toByteArray();
|
300
|
|
}
|
301
|
|
|
302
|
134
|
public Cookie[] getCookies()
|
303
|
|
{
|
304
|
134
|
return (Cookie[]) _cookies.toArray(new Cookie[_cookies.size()]);
|
305
|
|
}
|
306
|
|
|
307
|
3
|
public String getContentType()
|
308
|
|
{
|
309
|
3
|
return _contentType;
|
310
|
|
}
|
311
|
|
|
312
|
0
|
public void setCharacterEncoding(String enc)
|
313
|
|
{
|
314
|
|
}
|
315
|
|
|
316
|
|
}
|
317
|
|
|