29
29
* @run junit/othervm HttpURLConnectionExpectContinueTest
30
30
*/
31
31
32
- import org .junit .jupiter .api .AfterAll ;
33
- import org .junit .jupiter .api .BeforeAll ;
34
- import org .junit .jupiter .api .Test ;
35
- import org .junit .jupiter .api .TestInstance ;
32
+ import org .junit .AfterClass ;
33
+ import org .junit .BeforeClass ;
34
+ import org .junit .Test ;
36
35
37
36
import java .io .IOException ;
38
37
import java .io .InputStream ;
44
43
import java .util .logging .Level ;
45
44
import java .util .logging .Logger ;
46
45
47
- import static org .junit .jupiter . api . Assertions .assertTrue ;
46
+ import static org .junit .Assert .assertTrue ;
48
47
49
- @ TestInstance (TestInstance .Lifecycle .PER_CLASS )
50
48
public class HttpURLConnectionExpectContinueTest {
51
49
52
- class Control {
50
+ private static class Control {
53
51
volatile ServerSocket serverSocket = null ;
54
52
volatile boolean stop = false ;
55
53
volatile boolean respondWith100Continue = false ;
56
54
volatile boolean write100ContinueTwice = false ;
57
55
volatile String response = null ;
58
56
}
59
57
60
- private Thread serverThread = null ;
61
- private volatile Control control ;
58
+ private static Thread serverThread = null ;
59
+ private static volatile Control control ;
62
60
static final Logger logger ;
63
61
64
62
static {
63
+ control = new Control ();
65
64
logger = Logger .getLogger ("sun.net.www.protocol.http.HttpURLConnection" );
66
65
logger .setLevel (Level .ALL );
67
66
Logger .getLogger ("" ).getHandlers ()[0 ].setLevel (Level .ALL );
68
67
}
69
68
70
- @ BeforeAll
71
- public void startServerSocket () throws Exception {
72
- Control control = this .control = new Control ();
69
+ @ BeforeClass
70
+ public static void startServerSocket () throws Exception {
73
71
74
72
control .serverSocket = new ServerSocket ();
75
73
control .serverSocket .setReuseAddress (true );
@@ -170,9 +168,8 @@ public void startServerSocket() throws Exception {
170
168
serverThread .start ();
171
169
}
172
170
173
- @ AfterAll
174
- public void stopServerSocket () throws Exception {
175
- Control control = this .control ;
171
+ @ AfterClass
172
+ public static void stopServerSocket () throws Exception {
176
173
control .stop = true ;
177
174
control .serverSocket .close ();
178
175
serverThread .join ();
@@ -181,7 +178,6 @@ public void stopServerSocket() throws Exception {
181
178
@ Test
182
179
public void testNonChunkedRequestAndNoExpect100ContinueResponse () throws Exception {
183
180
String body = "testNonChunkedRequestAndNoExpect100ContinueResponse" ;
184
- Control control = this .control ;
185
181
control .response = "HTTP/1.1 200 OK\r \n " +
186
182
"Connection: close\r \n " +
187
183
"Content-Length: " + body .length () + "\r \n " +
@@ -198,16 +194,15 @@ public void testNonChunkedRequestAndNoExpect100ContinueResponse() throws Excepti
198
194
int responseCode = connection .getResponseCode ();
199
195
String responseBody = new String (connection .getInputStream ().readAllBytes (), StandardCharsets .UTF_8 ).strip ();
200
196
System .err .println ("response body: " + responseBody );
201
- assertTrue (responseCode == 200 ,
202
- String . format ( "Expected 200 response, instead received %s" , responseCode ) );
203
- assertTrue (body . equals ( responseBody ),
204
- String . format ( "Expected response %s, instead received %s" , body , responseBody ));
197
+ assertTrue (String . format ( "Expected 200 response, instead received %s" , responseCode ) ,
198
+ responseCode == 200 );
199
+ assertTrue (String . format ( "Expected response %s, instead received %s" , body , responseBody ),
200
+ body . equals ( responseBody ));
205
201
}
206
202
207
203
@ Test
208
204
public void testNonChunkedRequestWithExpect100ContinueResponse () throws Exception {
209
205
String body = "testNonChunkedRequestWithExpect100ContinueResponse" ;
210
- Control control = this .control ;
211
206
control .response = "HTTP/1.1 200 OK\r \n " +
212
207
"Connection: close\r \n " +
213
208
"Content-Length: " + body .length () + "\r \n " +
@@ -224,16 +219,15 @@ public void testNonChunkedRequestWithExpect100ContinueResponse() throws Exceptio
224
219
int responseCode = connection .getResponseCode ();
225
220
String responseBody = new String (connection .getInputStream ().readAllBytes (), StandardCharsets .UTF_8 ).strip ();
226
221
System .err .println ("response body: " + responseBody );
227
- assertTrue (responseCode == 200 ,
228
- String . format ( "Expected 200 response, instead received %s" , responseCode ) );
229
- assertTrue (body . equals ( responseBody ),
230
- String . format ( "Expected response %s, instead received %s" , body , responseBody ));
222
+ assertTrue (String . format ( "Expected 200 response, instead received %s" , responseCode ) ,
223
+ responseCode == 200 );
224
+ assertTrue (String . format ( "Expected response %s, instead received %s" , body , responseBody ),
225
+ body . equals ( responseBody ));
231
226
}
232
227
233
228
@ Test
234
229
public void testNonChunkedRequestWithDoubleExpect100ContinueResponse () throws Exception {
235
230
String body = "testNonChunkedRequestWithDoubleExpect100ContinueResponse" ;
236
- Control control = this .control ;
237
231
control .response = "HTTP/1.1 200 OK\r \n " +
238
232
"Connection: close\r \n " +
239
233
"Content-Length: " + body .length () + "\r \n " +
@@ -250,16 +244,15 @@ public void testNonChunkedRequestWithDoubleExpect100ContinueResponse() throws Ex
250
244
int responseCode = connection .getResponseCode ();
251
245
String responseBody = new String (connection .getInputStream ().readAllBytes (), StandardCharsets .UTF_8 ).strip ();
252
246
System .err .println ("response body: " + responseBody );
253
- assertTrue (responseCode == 200 ,
254
- String . format ( "Expected 200 response, instead received %s" , responseCode ) );
255
- assertTrue (body . equals ( responseBody ),
256
- String . format ( "Expected response %s, instead received %s" , body , responseBody ));
247
+ assertTrue (String . format ( "Expected 200 response, instead received %s" , responseCode ) ,
248
+ responseCode == 200 );
249
+ assertTrue (String . format ( "Expected response %s, instead received %s" , body , responseBody ),
250
+ body . equals ( responseBody ));
257
251
}
258
252
259
253
@ Test
260
254
public void testChunkedRequestAndNoExpect100ContinueResponse () throws Exception {
261
255
String body = "testChunkedRequestAndNoExpect100ContinueResponse" ;
262
- Control control = this .control ;
263
256
control .response = "HTTP/1.1 200 OK\r \n " +
264
257
"Connection: close\r \n " +
265
258
"Content-Length: " + body .length () + "\r \n " +
@@ -277,16 +270,15 @@ public void testChunkedRequestAndNoExpect100ContinueResponse() throws Exception
277
270
int responseCode = connection .getResponseCode ();
278
271
String responseBody = new String (connection .getInputStream ().readAllBytes (), StandardCharsets .UTF_8 ).strip ();
279
272
System .err .println ("response body: " + responseBody );
280
- assertTrue (responseCode == 200 ,
281
- String . format ( "Expected 200 response, instead received %s" , responseCode ) );
282
- assertTrue (body . equals ( responseBody ),
283
- String . format ( "Expected response %s, instead received %s" , body , responseBody ));
273
+ assertTrue (String . format ( "Expected 200 response, instead received %s" , responseCode ) ,
274
+ responseCode == 200 );
275
+ assertTrue (String . format ( "Expected response %s, instead received %s" , body , responseBody ),
276
+ body . equals ( responseBody ));
284
277
}
285
278
286
279
@ Test
287
280
public void testChunkedRequestWithExpect100ContinueResponse () throws Exception {
288
281
String body = "testChunkedRequestWithExpect100ContinueResponse" ;
289
- Control control = this .control ;
290
282
control .response = "HTTP/1.1 200 OK\r \n " +
291
283
"Connection: close\r \n " +
292
284
"Content-Length: " + body .length () + "\r \n " +
@@ -304,16 +296,15 @@ public void testChunkedRequestWithExpect100ContinueResponse() throws Exception {
304
296
int responseCode = connection .getResponseCode ();
305
297
String responseBody = new String (connection .getInputStream ().readAllBytes (), StandardCharsets .UTF_8 ).strip ();
306
298
System .err .println ("response body: " + responseBody );
307
- assertTrue (responseCode == 200 ,
308
- String . format ( "Expected 200 response, instead received %s" , responseCode ) );
309
- assertTrue (body . equals ( responseBody ),
310
- String . format ( "Expected response %s, instead received %s" , body , responseBody ));
299
+ assertTrue (String . format ( "Expected 200 response, instead received %s" , responseCode ) ,
300
+ responseCode == 200 );
301
+ assertTrue (String . format ( "Expected response %s, instead received %s" , body , responseBody ),
302
+ body . equals ( responseBody ));
311
303
}
312
304
313
305
@ Test
314
306
public void testChunkedRequestWithDoubleExpect100ContinueResponse () throws Exception {
315
307
String body = "testChunkedRequestWithDoubleExpect100ContinueResponse" ;
316
- Control control = this .control ;
317
308
control .response = "HTTP/1.1 200 OK\r \n " +
318
309
"Connection: close\r \n " +
319
310
"Content-Length: " + body .length () + "\r \n " +
@@ -331,16 +322,15 @@ public void testChunkedRequestWithDoubleExpect100ContinueResponse() throws Excep
331
322
int responseCode = connection .getResponseCode ();
332
323
String responseBody = new String (connection .getInputStream ().readAllBytes (), StandardCharsets .UTF_8 ).strip ();
333
324
System .err .println ("response body: " + responseBody );
334
- assertTrue (responseCode == 200 ,
335
- String . format ( "Expected 200 response, instead received %s" , responseCode ) );
336
- assertTrue (body . equals ( responseBody ),
337
- String . format ( "Expected response %s, instead received %s" , body , responseBody ));
325
+ assertTrue (String . format ( "Expected 200 response, instead received %s" , responseCode ) ,
326
+ responseCode == 200 );
327
+ assertTrue (String . format ( "Expected response %s, instead received %s" , body , responseBody ),
328
+ body . equals ( responseBody ));
338
329
}
339
330
340
331
@ Test
341
332
public void testFixedLengthRequestAndNoExpect100ContinueResponse () throws Exception {
342
333
String body = "testFixedLengthRequestAndNoExpect100ContinueResponse" ;
343
- Control control = this .control ;
344
334
control .response = "HTTP/1.1 200 OK\r \n " +
345
335
"Connection: close\r \n " +
346
336
"Content-Length: " + body .length () + "\r \n " +
@@ -358,16 +348,15 @@ public void testFixedLengthRequestAndNoExpect100ContinueResponse() throws Except
358
348
int responseCode = connection .getResponseCode ();
359
349
String responseBody = new String (connection .getInputStream ().readAllBytes (), StandardCharsets .UTF_8 ).strip ();
360
350
System .err .println ("response body: " + responseBody );
361
- assertTrue (responseCode == 200 ,
362
- String . format ( "Expected 200 response, instead received %s" , responseCode ) );
363
- assertTrue (body . equals ( responseBody ),
364
- String . format ( "Expected response %s, instead received %s" , body , responseBody ));
351
+ assertTrue (String . format ( "Expected 200 response, instead received %s" , responseCode ) ,
352
+ responseCode == 200 );
353
+ assertTrue (String . format ( "Expected response %s, instead received %s" , body , responseBody ),
354
+ body . equals ( responseBody ));
365
355
}
366
356
367
357
@ Test
368
358
public void testFixedLengthRequestWithExpect100ContinueResponse () throws Exception {
369
359
String body = "testFixedLengthRequestWithExpect100ContinueResponse" ;
370
- Control control = this .control ;
371
360
control .response = "HTTP/1.1 200 OK\r \n " +
372
361
"Connection: close\r \n " +
373
362
"Content-Length: " + body .length () + "\r \n " +
@@ -385,16 +374,15 @@ public void testFixedLengthRequestWithExpect100ContinueResponse() throws Excepti
385
374
int responseCode = connection .getResponseCode ();
386
375
String responseBody = new String (connection .getInputStream ().readAllBytes (), StandardCharsets .UTF_8 ).strip ();
387
376
System .err .println ("response body: " + responseBody );
388
- assertTrue (responseCode == 200 ,
389
- String . format ( "Expected 200 response, instead received %s" , responseCode ) );
390
- assertTrue (body . equals ( responseBody ),
391
- String . format ( "Expected response %s, instead received %s" , body , responseBody ));
377
+ assertTrue (String . format ( "Expected 200 response, instead received %s" , responseCode ) ,
378
+ responseCode == 200 );
379
+ assertTrue (String . format ( "Expected response %s, instead received %s" , body , responseBody ),
380
+ body . equals ( responseBody ));
392
381
}
393
382
394
383
@ Test
395
384
public void testFixedLengthRequestWithDoubleExpect100ContinueResponse () throws Exception {
396
385
String body = "testFixedLengthRequestWithDoubleExpect100ContinueResponse" ;
397
- Control control = this .control ;
398
386
control .response = "HTTP/1.1 200 OK\r \n " +
399
387
"Connection: close\r \n " +
400
388
"Content-Length: " + body .length () + "\r \n " +
@@ -412,10 +400,10 @@ public void testFixedLengthRequestWithDoubleExpect100ContinueResponse() throws E
412
400
int responseCode = connection .getResponseCode ();
413
401
String responseBody = new String (connection .getInputStream ().readAllBytes (), StandardCharsets .UTF_8 ).strip ();
414
402
System .err .println ("response body: " + responseBody );
415
- assertTrue (responseCode == 200 ,
416
- String . format ( "Expected 200 response, instead received %s" , responseCode ) );
417
- assertTrue (body . equals ( responseBody ),
418
- String . format ( "Expected response %s, instead received %s" , body , responseBody ));
403
+ assertTrue (String . format ( "Expected 200 response, instead received %s" , responseCode ) ,
404
+ responseCode == 200 );
405
+ assertTrue (String . format ( "Expected response %s, instead received %s" , body , responseBody ),
406
+ body . equals ( responseBody ));
419
407
}
420
408
421
409
// Creates a connection with all the common settings used in each test
0 commit comments