1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
33
33
import org .apache .commons .fileupload .FileUploadException ;
34
34
import org .apache .commons .fileupload .disk .DiskFileItemFactory ;
35
35
import org .apache .commons .fileupload .servlet .ServletFileUpload ;
36
-
37
36
import org .eclipse .jetty .server .Connector ;
38
37
import org .eclipse .jetty .server .NetworkConnector ;
39
38
import org .eclipse .jetty .server .Server ;
40
39
import org .eclipse .jetty .servlet .ServletContextHandler ;
41
40
import org .eclipse .jetty .servlet .ServletHolder ;
42
-
43
41
import org .junit .AfterClass ;
44
42
import org .junit .BeforeClass ;
45
43
@@ -56,11 +54,12 @@ public class AbstractJettyServerTestCase {
56
54
57
55
protected static final String helloWorld = "H\u00e9 llo W\u00f6 rld" ;
58
56
59
- protected static final MediaType textContentType = new MediaType ("text" , "plain" ,
60
- Collections .singletonMap ("charset" , "UTF-8" ));
57
+ protected static final MediaType textContentType =
58
+ new MediaType ("text" , "plain" , Collections .singletonMap ("charset" , "UTF-8" ));
59
+
60
+ protected static final MediaType jsonContentType =
61
+ new MediaType ("application" , "json" , Collections .singletonMap ("charset" , "UTF-8" ));
61
62
62
- protected static final MediaType jsonContentType = new MediaType ("application" ,
63
- "json" , Collections .singletonMap ("charset" , "utf-8" ));
64
63
65
64
private static Server jettyServer ;
66
65
@@ -71,7 +70,6 @@ public class AbstractJettyServerTestCase {
71
70
72
71
@ BeforeClass
73
72
public static void startJettyServer () throws Exception {
74
-
75
73
// Let server pick its own random, available port.
76
74
jettyServer = new Server (0 );
77
75
@@ -114,54 +112,55 @@ public static void stopJettyServer() throws Exception {
114
112
}
115
113
}
116
114
115
+
117
116
/** Servlet that sets the given status code. */
118
117
@ SuppressWarnings ("serial" )
119
118
private static class StatusCodeServlet extends GenericServlet {
120
119
121
120
private final int sc ;
122
121
123
- private StatusCodeServlet (int sc ) {
122
+ public StatusCodeServlet (int sc ) {
124
123
this .sc = sc ;
125
124
}
126
125
127
126
@ Override
128
- public void service (ServletRequest request , ServletResponse response ) throws
129
- ServletException , IOException {
127
+ public void service (ServletRequest request , ServletResponse response ) throws IOException {
130
128
((HttpServletResponse ) response ).setStatus (sc );
131
129
}
132
130
}
133
131
132
+
134
133
/** Servlet that returns an error message for a given status code. */
135
134
@ SuppressWarnings ("serial" )
136
135
private static class ErrorServlet extends GenericServlet {
137
136
138
137
private final int sc ;
139
138
140
- private ErrorServlet (int sc ) {
139
+ public ErrorServlet (int sc ) {
141
140
this .sc = sc ;
142
141
}
143
142
144
143
@ Override
145
- public void service (ServletRequest request , ServletResponse response ) throws ServletException , IOException {
144
+ public void service (ServletRequest request , ServletResponse response ) throws IOException {
146
145
((HttpServletResponse ) response ).sendError (sc );
147
146
}
148
147
}
149
148
149
+
150
150
@ SuppressWarnings ("serial" )
151
151
private static class GetServlet extends HttpServlet {
152
152
153
153
private final byte [] buf ;
154
154
155
155
private final MediaType contentType ;
156
156
157
- private GetServlet (byte [] buf , MediaType contentType ) {
157
+ public GetServlet (byte [] buf , MediaType contentType ) {
158
158
this .buf = buf ;
159
159
this .contentType = contentType ;
160
160
}
161
161
162
162
@ Override
163
- protected void doGet (HttpServletRequest request , HttpServletResponse response )
164
- throws ServletException , IOException {
163
+ protected void doGet (HttpServletRequest request , HttpServletResponse response ) throws IOException {
165
164
if (contentType != null ) {
166
165
response .setContentType (contentType .toString ());
167
166
}
@@ -170,31 +169,31 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
170
169
}
171
170
}
172
171
172
+
173
173
@ SuppressWarnings ("serial" )
174
174
private static class PostServlet extends HttpServlet {
175
175
176
- private final String s ;
176
+ private final String content ;
177
177
178
178
private final String location ;
179
179
180
180
private final byte [] buf ;
181
181
182
182
private final MediaType contentType ;
183
183
184
- private PostServlet (String s , String location , byte [] buf , MediaType contentType ) {
185
- this .s = s ;
184
+ public PostServlet (String content , String location , byte [] buf , MediaType contentType ) {
185
+ this .content = content ;
186
186
this .location = location ;
187
187
this .buf = buf ;
188
188
this .contentType = contentType ;
189
189
}
190
190
191
191
@ Override
192
- protected void doPost (HttpServletRequest request , HttpServletResponse response )
193
- throws ServletException , IOException {
192
+ protected void doPost (HttpServletRequest request , HttpServletResponse response ) throws IOException {
194
193
assertTrue ("Invalid request content-length" , request .getContentLength () > 0 );
195
194
assertNotNull ("No content-type" , request .getContentType ());
196
195
String body = FileCopyUtils .copyToString (request .getReader ());
197
- assertEquals ("Invalid request body" , s , body );
196
+ assertEquals ("Invalid request body" , content , body );
198
197
response .setStatus (HttpServletResponse .SC_CREATED );
199
198
response .setHeader ("Location" , baseUrl + location );
200
199
response .setContentLength (buf .length );
@@ -203,21 +202,21 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
203
202
}
204
203
}
205
204
205
+
206
206
@ SuppressWarnings ("serial" )
207
207
private static class JsonPostServlet extends HttpServlet {
208
208
209
209
private final String location ;
210
210
211
211
private final MediaType contentType ;
212
212
213
- private JsonPostServlet (String location , MediaType contentType ) {
213
+ public JsonPostServlet (String location , MediaType contentType ) {
214
214
this .location = location ;
215
215
this .contentType = contentType ;
216
216
}
217
217
218
218
@ Override
219
- protected void doPost (HttpServletRequest request , HttpServletResponse response )
220
- throws ServletException , IOException {
219
+ protected void doPost (HttpServletRequest request , HttpServletResponse response ) throws IOException {
221
220
assertTrue ("Invalid request content-length" , request .getContentLength () > 0 );
222
221
assertNotNull ("No content-type" , request .getContentType ());
223
222
String body = FileCopyUtils .copyToString (request .getReader ());
@@ -230,18 +229,18 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
230
229
}
231
230
}
232
231
232
+
233
233
@ SuppressWarnings ("serial" )
234
234
private static class PutServlet extends HttpServlet {
235
235
236
236
private final String s ;
237
237
238
- private PutServlet (String s , byte [] buf , MediaType contentType ) {
238
+ public PutServlet (String s , byte [] buf , MediaType contentType ) {
239
239
this .s = s ;
240
240
}
241
241
242
242
@ Override
243
- protected void doPut (HttpServletRequest request , HttpServletResponse response )
244
- throws ServletException , IOException {
243
+ protected void doPut (HttpServletRequest request , HttpServletResponse response ) throws IOException {
245
244
assertTrue ("Invalid request content-length" , request .getContentLength () > 0 );
246
245
assertNotNull ("No content-type" , request .getContentType ());
247
246
String body = FileCopyUtils .copyToString (request .getReader ());
@@ -250,17 +249,19 @@ protected void doPut(HttpServletRequest request, HttpServletResponse response)
250
249
}
251
250
}
252
251
252
+
253
253
@ SuppressWarnings ("serial" )
254
254
private static class UriServlet extends HttpServlet {
255
255
256
256
@ Override
257
- protected void doGet (HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
257
+ protected void doGet (HttpServletRequest req , HttpServletResponse resp ) throws IOException {
258
258
resp .setContentType ("text/plain" );
259
259
resp .setCharacterEncoding ("utf-8" );
260
260
resp .getWriter ().write (req .getRequestURI ());
261
261
}
262
262
}
263
263
264
+
264
265
@ SuppressWarnings ("serial" )
265
266
private static class MultipartServlet extends HttpServlet {
266
267
@@ -300,13 +301,13 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
300
301
}
301
302
}
302
303
304
+
303
305
@ SuppressWarnings ("serial" )
304
306
private static class FormServlet extends HttpServlet {
305
307
306
308
@ Override
307
- protected void doPost (HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
308
- assertEquals (MediaType .APPLICATION_FORM_URLENCODED_VALUE ,
309
- req .getContentType ());
309
+ protected void doPost (HttpServletRequest req , HttpServletResponse resp ) throws IOException {
310
+ assertEquals (MediaType .APPLICATION_FORM_URLENCODED_VALUE , req .getContentType ());
310
311
311
312
Map <String , String []> parameters = req .getParameterMap ();
312
313
assertEquals (2 , parameters .size ());
@@ -322,15 +323,14 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
322
323
}
323
324
}
324
325
326
+
325
327
@ SuppressWarnings ("serial" )
326
328
private static class DeleteServlet extends HttpServlet {
327
329
328
330
@ Override
329
- protected void doDelete (HttpServletRequest req , HttpServletResponse resp )
330
- throws ServletException , IOException {
331
+ protected void doDelete (HttpServletRequest req , HttpServletResponse resp ) throws IOException {
331
332
resp .setStatus (200 );
332
333
}
333
-
334
334
}
335
335
336
336
}
0 commit comments