|
|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2024 the original author or authors. |
|
|
|
|
* Copyright 2002-2025 the original author or authors. |
|
|
|
|
* |
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
|
@ -306,7 +306,9 @@ class ResourceTests {
@@ -306,7 +306,9 @@ class ResourceTests {
|
|
|
|
|
@Nested |
|
|
|
|
class UrlResourceTests { |
|
|
|
|
|
|
|
|
|
private MockWebServer server = new MockWebServer(); |
|
|
|
|
private static final String LAST_MODIFIED = "Wed, 09 Apr 2014 09:57:42 GMT"; |
|
|
|
|
|
|
|
|
|
private final MockWebServer server = new MockWebServer(); |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void sameResourceWithRelativePathIsEqual() throws Exception { |
|
|
|
|
@ -385,22 +387,44 @@ class ResourceTests {
@@ -385,22 +387,44 @@ class ResourceTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void missingRemoteResourceDoesNotExist() throws Exception { |
|
|
|
|
String baseUrl = startServer(); |
|
|
|
|
String baseUrl = startServer(true); |
|
|
|
|
UrlResource resource = new UrlResource(baseUrl + "/missing"); |
|
|
|
|
assertThat(resource.exists()).isFalse(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void remoteResourceExists() throws Exception { |
|
|
|
|
String baseUrl = startServer(); |
|
|
|
|
String baseUrl = startServer(true); |
|
|
|
|
UrlResource resource = new UrlResource(baseUrl + "/resource"); |
|
|
|
|
assertThat(resource.exists()).isTrue(); |
|
|
|
|
assertThat(resource.isReadable()).isTrue(); |
|
|
|
|
assertThat(resource.contentLength()).isEqualTo(6); |
|
|
|
|
assertThat(resource.lastModified()).isGreaterThan(0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void remoteResourceExistsFallback() throws Exception { |
|
|
|
|
String baseUrl = startServer(false); |
|
|
|
|
UrlResource resource = new UrlResource(baseUrl + "/resource"); |
|
|
|
|
assertThat(resource.exists()).isTrue(); |
|
|
|
|
assertThat(resource.isReadable()).isTrue(); |
|
|
|
|
assertThat(resource.contentLength()).isEqualTo(6); |
|
|
|
|
assertThat(resource.lastModified()).isGreaterThan(0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void canCustomizeHttpUrlConnectionForExists() throws Exception { |
|
|
|
|
String baseUrl = startServer(); |
|
|
|
|
String baseUrl = startServer(true); |
|
|
|
|
CustomResource resource = new CustomResource(baseUrl + "/resource"); |
|
|
|
|
assertThat(resource.exists()).isTrue(); |
|
|
|
|
RecordedRequest request = this.server.takeRequest(); |
|
|
|
|
assertThat(request.getMethod()).isEqualTo("HEAD"); |
|
|
|
|
assertThat(request.getHeader("Framework-Name")).isEqualTo("Spring"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void canCustomizeHttpUrlConnectionForExistsFallback() throws Exception { |
|
|
|
|
String baseUrl = startServer(false); |
|
|
|
|
CustomResource resource = new CustomResource(baseUrl + "/resource"); |
|
|
|
|
assertThat(resource.exists()).isTrue(); |
|
|
|
|
RecordedRequest request = this.server.takeRequest(); |
|
|
|
|
@ -410,7 +434,7 @@ class ResourceTests {
@@ -410,7 +434,7 @@ class ResourceTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void canCustomizeHttpUrlConnectionForRead() throws Exception { |
|
|
|
|
String baseUrl = startServer(); |
|
|
|
|
String baseUrl = startServer(true); |
|
|
|
|
CustomResource resource = new CustomResource(baseUrl + "/resource"); |
|
|
|
|
assertThat(resource.getInputStream()).hasContent("Spring"); |
|
|
|
|
RecordedRequest request = this.server.takeRequest(); |
|
|
|
|
@ -420,7 +444,7 @@ class ResourceTests {
@@ -420,7 +444,7 @@ class ResourceTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void useUserInfoToSetBasicAuth() throws Exception { |
|
|
|
|
startServer(); |
|
|
|
|
startServer(true); |
|
|
|
|
UrlResource resource = new UrlResource( |
|
|
|
|
"http://alice:secret@localhost:" + this.server.getPort() + "/resource"); |
|
|
|
|
assertThat(resource.getInputStream()).hasContent("Spring"); |
|
|
|
|
@ -436,8 +460,8 @@ class ResourceTests {
@@ -436,8 +460,8 @@ class ResourceTests {
|
|
|
|
|
this.server.shutdown(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private String startServer() throws Exception { |
|
|
|
|
this.server.setDispatcher(new ResourceDispatcher()); |
|
|
|
|
private String startServer(boolean withHeadSupport) throws Exception { |
|
|
|
|
this.server.setDispatcher(new ResourceDispatcher(withHeadSupport)); |
|
|
|
|
this.server.start(); |
|
|
|
|
return "http://localhost:" + this.server.getPort(); |
|
|
|
|
} |
|
|
|
|
@ -456,15 +480,26 @@ class ResourceTests {
@@ -456,15 +480,26 @@ class ResourceTests {
|
|
|
|
|
|
|
|
|
|
class ResourceDispatcher extends Dispatcher { |
|
|
|
|
|
|
|
|
|
boolean withHeadSupport; |
|
|
|
|
|
|
|
|
|
public ResourceDispatcher(boolean withHeadSupport) { |
|
|
|
|
this.withHeadSupport = withHeadSupport; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public MockResponse dispatch(RecordedRequest request) { |
|
|
|
|
if (request.getPath().equals("/resource")) { |
|
|
|
|
return switch (request.getMethod()) { |
|
|
|
|
case "HEAD" -> new MockResponse() |
|
|
|
|
.addHeader("Content-Length", "6"); |
|
|
|
|
case "HEAD" -> (this.withHeadSupport ? |
|
|
|
|
new MockResponse() |
|
|
|
|
.addHeader("Content-Type", "text/plain") |
|
|
|
|
.addHeader("Content-Length", "6") |
|
|
|
|
.addHeader("Last-Modified", LAST_MODIFIED) : |
|
|
|
|
new MockResponse().setResponseCode(405)); |
|
|
|
|
case "GET" -> new MockResponse() |
|
|
|
|
.addHeader("Content-Length", "6") |
|
|
|
|
.addHeader("Content-Type", "text/plain") |
|
|
|
|
.addHeader("Content-Length", "6") |
|
|
|
|
.addHeader("Last-Modified", LAST_MODIFIED) |
|
|
|
|
.setBody("Spring"); |
|
|
|
|
default -> new MockResponse().setResponseCode(404); |
|
|
|
|
}; |
|
|
|
|
|