Polish "Add Basic Authorization for UrlResource"

See gh-1822
This commit is contained in:
Stephane Nicoll
2023-08-22 16:38:00 +02:00
parent ac9ca412c8
commit f95a1f49df
2 changed files with 27 additions and 7 deletions

View File

@@ -33,6 +33,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.stream.Stream;
import okhttp3.mockwebserver.Dispatcher;
@@ -377,6 +378,19 @@ class ResourceTests {
assertThat(request.getHeader("Framework-Name")).isEqualTo("Spring");
}
@Test
void useUserInfoToSetBasicAuth() throws Exception {
startServer();
UrlResource resource = new UrlResource("http://alice:secret@localhost:"
+ this.server.getPort() + "/resource");
assertThat(resource.getInputStream()).hasContent("Spring");
RecordedRequest request = this.server.takeRequest();
String authorization = request.getHeader("Authorization");
assertThat(authorization).isNotNull().startsWith("Basic ");
assertThat(new String(Base64.getDecoder().decode(
authorization.substring(6)), StandardCharsets.ISO_8859_1)).isEqualTo("alice:secret");
}
@AfterEach
void shutdown() throws Exception {
this.server.shutdown();