1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 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.
@@ -306,7 +306,9 @@ private void assertUrlAndUriBehavior(Resource resource) throws IOException {
306
306
@ Nested
307
307
class UrlResourceTests {
308
308
309
- private MockWebServer server = new MockWebServer ();
309
+ private static final String LAST_MODIFIED = "Wed, 09 Apr 2014 09:57:42 GMT" ;
310
+
311
+ private final MockWebServer server = new MockWebServer ();
310
312
311
313
@ Test
312
314
void sameResourceWithRelativePathIsEqual () throws Exception {
@@ -385,22 +387,44 @@ void unusualRelativeResourcesAreEqual() throws Exception {
385
387
386
388
@ Test
387
389
void missingRemoteResourceDoesNotExist () throws Exception {
388
- String baseUrl = startServer ();
390
+ String baseUrl = startServer (true );
389
391
UrlResource resource = new UrlResource (baseUrl + "/missing" );
390
392
assertThat (resource .exists ()).isFalse ();
391
393
}
392
394
393
395
@ Test
394
396
void remoteResourceExists () throws Exception {
395
- String baseUrl = startServer ();
397
+ String baseUrl = startServer (true );
398
+ UrlResource resource = new UrlResource (baseUrl + "/resource" );
399
+ assertThat (resource .exists ()).isTrue ();
400
+ assertThat (resource .isReadable ()).isTrue ();
401
+ assertThat (resource .contentLength ()).isEqualTo (6 );
402
+ assertThat (resource .lastModified ()).isGreaterThan (0 );
403
+ }
404
+
405
+ @ Test
406
+ void remoteResourceExistsFallback () throws Exception {
407
+ String baseUrl = startServer (false );
396
408
UrlResource resource = new UrlResource (baseUrl + "/resource" );
397
409
assertThat (resource .exists ()).isTrue ();
410
+ assertThat (resource .isReadable ()).isTrue ();
398
411
assertThat (resource .contentLength ()).isEqualTo (6 );
412
+ assertThat (resource .lastModified ()).isGreaterThan (0 );
399
413
}
400
414
401
415
@ Test
402
416
void canCustomizeHttpUrlConnectionForExists () throws Exception {
403
- String baseUrl = startServer ();
417
+ String baseUrl = startServer (true );
418
+ CustomResource resource = new CustomResource (baseUrl + "/resource" );
419
+ assertThat (resource .exists ()).isTrue ();
420
+ RecordedRequest request = this .server .takeRequest ();
421
+ assertThat (request .getMethod ()).isEqualTo ("HEAD" );
422
+ assertThat (request .getHeader ("Framework-Name" )).isEqualTo ("Spring" );
423
+ }
424
+
425
+ @ Test
426
+ void canCustomizeHttpUrlConnectionForExistsFallback () throws Exception {
427
+ String baseUrl = startServer (false );
404
428
CustomResource resource = new CustomResource (baseUrl + "/resource" );
405
429
assertThat (resource .exists ()).isTrue ();
406
430
RecordedRequest request = this .server .takeRequest ();
@@ -410,7 +434,7 @@ void canCustomizeHttpUrlConnectionForExists() throws Exception {
410
434
411
435
@ Test
412
436
void canCustomizeHttpUrlConnectionForRead () throws Exception {
413
- String baseUrl = startServer ();
437
+ String baseUrl = startServer (true );
414
438
CustomResource resource = new CustomResource (baseUrl + "/resource" );
415
439
assertThat (resource .getInputStream ()).hasContent ("Spring" );
416
440
RecordedRequest request = this .server .takeRequest ();
@@ -420,7 +444,7 @@ void canCustomizeHttpUrlConnectionForRead() throws Exception {
420
444
421
445
@ Test
422
446
void useUserInfoToSetBasicAuth () throws Exception {
423
- startServer ();
447
+ startServer (true );
424
448
UrlResource resource = new UrlResource (
425
449
"http://alice:secret@localhost:" + this .server .getPort () + "/resource" );
426
450
assertThat (resource .getInputStream ()).hasContent ("Spring" );
@@ -436,8 +460,8 @@ void shutdown() throws Exception {
436
460
this .server .shutdown ();
437
461
}
438
462
439
- private String startServer () throws Exception {
440
- this .server .setDispatcher (new ResourceDispatcher ());
463
+ private String startServer (boolean withHeadSupport ) throws Exception {
464
+ this .server .setDispatcher (new ResourceDispatcher (withHeadSupport ));
441
465
this .server .start ();
442
466
return "http://localhost:" + this .server .getPort ();
443
467
}
@@ -456,15 +480,26 @@ protected void customizeConnection(HttpURLConnection con) {
456
480
457
481
class ResourceDispatcher extends Dispatcher {
458
482
483
+ boolean withHeadSupport ;
484
+
485
+ public ResourceDispatcher (boolean withHeadSupport ) {
486
+ this .withHeadSupport = withHeadSupport ;
487
+ }
488
+
459
489
@ Override
460
490
public MockResponse dispatch (RecordedRequest request ) {
461
491
if (request .getPath ().equals ("/resource" )) {
462
492
return switch (request .getMethod ()) {
463
- case "HEAD" -> new MockResponse ()
464
- .addHeader ("Content-Length" , "6" );
493
+ case "HEAD" -> (this .withHeadSupport ?
494
+ new MockResponse ()
495
+ .addHeader ("Content-Type" , "text/plain" )
496
+ .addHeader ("Content-Length" , "6" )
497
+ .addHeader ("Last-Modified" , LAST_MODIFIED ) :
498
+ new MockResponse ().setResponseCode (405 ));
465
499
case "GET" -> new MockResponse ()
466
- .addHeader ("Content-Length" , "6" )
467
500
.addHeader ("Content-Type" , "text/plain" )
501
+ .addHeader ("Content-Length" , "6" )
502
+ .addHeader ("Last-Modified" , LAST_MODIFIED )
468
503
.setBody ("Spring" );
469
504
default -> new MockResponse ().setResponseCode (404 );
470
505
};
0 commit comments