From 58a47b186a35cb2b143a3d5f00644b189d24c584 Mon Sep 17 00:00:00 2001 From: Yoshikazu Nojima Date: Wed, 1 Jan 2025 17:43:31 +0900 Subject: [PATCH] Address WebAuthn4J HttpClient interface design change --- .../RestTemplateAdaptorHttpClient.java | 18 ++++++- .../RestTemplateAdaptorHttpClientTest.java | 47 ++++++++++++++++--- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/webauthn4j-spring-security-metadata/src/main/java/com/webauthn4j/springframework/security/metadata/RestTemplateAdaptorHttpClient.java b/webauthn4j-spring-security-metadata/src/main/java/com/webauthn4j/springframework/security/metadata/RestTemplateAdaptorHttpClient.java index 39f0b0e5d..42cd5ca36 100644 --- a/webauthn4j-spring-security-metadata/src/main/java/com/webauthn4j/springframework/security/metadata/RestTemplateAdaptorHttpClient.java +++ b/webauthn4j-spring-security-metadata/src/main/java/com/webauthn4j/springframework/security/metadata/RestTemplateAdaptorHttpClient.java @@ -17,9 +17,17 @@ package com.webauthn4j.springframework.security.metadata; import com.webauthn4j.metadata.HttpClient; +import com.webauthn4j.metadata.exception.MDSException; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; import org.springframework.util.Assert; import org.springframework.web.client.RestTemplate; +import java.io.IOException; +import java.util.Objects; + /** * An {@link HttpClient} implementation with Spring {@link RestTemplate} */ @@ -36,7 +44,13 @@ public RestTemplateAdaptorHttpClient(RestTemplate restTemplate) { * {@inheritDoc} */ @Override - public String fetch(String url) { - return restTemplate.getForObject(url, String.class); + public Response fetch(String url) { + ResponseEntity entity = restTemplate.getForEntity(url, Resource.class); + Resource resource = Objects.requireNonNull(entity.getBody()); + try { + return new Response(entity.getStatusCode().value(), resource.getInputStream()); + } catch (IOException e) { + throw new MDSException("Failed to fetch " + url, e); + } } } diff --git a/webauthn4j-spring-security-metadata/src/test/java/com/webauthn4j/springframework/security/metadata/RestTemplateAdaptorHttpClientTest.java b/webauthn4j-spring-security-metadata/src/test/java/com/webauthn4j/springframework/security/metadata/RestTemplateAdaptorHttpClientTest.java index 171d88a1b..14f050aaf 100644 --- a/webauthn4j-spring-security-metadata/src/test/java/com/webauthn4j/springframework/security/metadata/RestTemplateAdaptorHttpClientTest.java +++ b/webauthn4j-spring-security-metadata/src/test/java/com/webauthn4j/springframework/security/metadata/RestTemplateAdaptorHttpClientTest.java @@ -18,20 +18,55 @@ import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.client.RestClientTest; +import org.springframework.boot.test.context.TestComponent; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.web.client.RestTemplate; +import java.io.IOException; + +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; +import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; +@RunWith(MockitoJUnitRunner.class) public class RestTemplateAdaptorHttpClientTest { + @Mock + private RestTemplate restTemplate; + + @Mock + ResponseEntity entity; + + @Mock + Resource resource; + @Test - public void fetch_test() { + public void fetch_test() throws IOException { String url = "https://example.com/webauthn/options"; - RestTemplate restTemplate = mock(RestTemplate.class); - when(restTemplate.getForObject(eq(url), eq(String.class))).thenReturn("response"); + when(restTemplate.getForEntity(eq(url), eq(Resource.class))).thenReturn(entity); + when(entity.getBody()).thenReturn(resource); + when(entity.getStatusCode()).thenReturn(HttpStatusCode.valueOf(200)); + when(resource.getInputStream()).thenReturn(null); RestTemplateAdaptorHttpClient httpClient = new RestTemplateAdaptorHttpClient(restTemplate); httpClient.fetch(url); - verify(restTemplate).getForObject(eq(url), eq(String.class)); + verify(restTemplate).getForEntity(eq(url), eq(Resource.class)); } - -} \ No newline at end of file +}