[java-oidc-common] branch main updated: Add HTTP Provider Configuration fetching strategy tests

Phil Smart philip.smart at jisc.ac.uk
Wed Feb 23 16:34:13 UTC 2022


This is an automated email from the git hooks/post-receive script.

philsmart pushed a commit to branch main
in repository java-oidc-common.

View the commit online:
http://git.shibboleth.net/view/?p=java-oidc-common.git;a=commit;h=04f506ea8215e3043d9248c877ff28d29a8d3336

The following commit(s) were added to refs/heads/main by this push:
     new 04f506e  Add HTTP Provider Configuration fetching strategy tests
04f506e is described below

commit 04f506ea8215e3043d9248c877ff28d29a8d3336
Author: Phil Smart <philip.smart at jisc.ac.uk>
AuthorDate: Wed Feb 23 16:34:07 2022 +0000

    Add HTTP Provider Configuration fetching strategy tests
    
    Fix Javadoc
---
 .../metadata/cache/impl/DynamicMetadataCache.java  |   5 +-
 ...PProviderConfigurationFetchingStrategyTest.java | 169 +++++++++++++++++++++
 2 files changed, 172 insertions(+), 2 deletions(-)

diff --git a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DynamicMetadataCache.java b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DynamicMetadataCache.java
index 35d0570..e4628e9 100644
--- a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DynamicMetadataCache.java
+++ b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DynamicMetadataCache.java
@@ -353,6 +353,7 @@ public class DynamicMetadataCache<IdentifierType, MetadataType>
             ratioGaugeFetchToGet = MetricsSupport.register(
                     MetricRegistry.name(metricsBaseName, METRIC_RATIOGAUGE_FETCH_TO_GET), 
                     new RatioGauge() {
+                        @Override
                         protected Ratio getRatio() {
                             return Ratio.of(timerFetchFromSource.getCount(), 
                                     timerGet.getCount());
@@ -590,8 +591,8 @@ public class DynamicMetadataCache<IdentifierType, MetadataType>
      *  
      * <p>The first read attempt is optimistic and occurs without acquiring a read lock. The optimistic
      * read is validated to ensure another thread has not acquired a write lock in the meantime. If it has,
-     * a write lock is obtained and a further read is attempted - to ensure a consistent state. This
-     * should improve efficiency given that metadata reads will vastly out number metadata fetch/writes.</p>
+     * a read lock is obtained and a further read is attempted - to ensure a consistent state. This
+     * should improve efficiency given that metadata reads will out number metadata fetch/writes.</p>
      * 
      * @param mgmtData the metadata management data.
      * @param identifier the metadata identifier to use as a key to fetch.
diff --git a/oidc-common-metadata-impl/src/test/java/net/shibboleth/oidc/metadata/impl/HTTPProviderConfigurationFetchingStrategyTest.java b/oidc-common-metadata-impl/src/test/java/net/shibboleth/oidc/metadata/impl/HTTPProviderConfigurationFetchingStrategyTest.java
new file mode 100644
index 0000000..371b505
--- /dev/null
+++ b/oidc-common-metadata-impl/src/test/java/net/shibboleth/oidc/metadata/impl/HTTPProviderConfigurationFetchingStrategyTest.java
@@ -0,0 +1,169 @@
+package net.shibboleth.oidc.metadata.impl;
+
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.StatusLine;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.ResponseHandler;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.message.BasicHeader;
+import org.apache.http.protocol.HttpContext;
+import org.mockito.Mockito;
+import org.springframework.core.io.ClassPathResource;
+import org.testng.annotations.Test;
+
+import com.google.common.io.CharStreams;
+import com.nimbusds.oauth2.sdk.id.Issuer;
+import com.nimbusds.openid.connect.sdk.op.OIDCProviderMetadata;
+
+import net.shibboleth.oidc.metadata.criterion.IssuerIDCriterion;
+import net.shibboleth.oidc.metadata.impl.HTTPProviderConfigurationFetchingStrategy.OIDCProviderMetadataResponseHandler;
+import net.shibboleth.utilities.java.support.resolver.CriteriaSet;
+
+/** Tests for HTTPProviderConfigurationFetchingStrategy.*/
+public class HTTPProviderConfigurationFetchingStrategyTest {
+    
+    
+    @Test
+    public void testProviderConfigurationLoads_Success() throws Exception {
+        
+        final HttpClient httpClient = Mockito.mock(HttpClient.class);
+        final HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
+        final StatusLine statusLine = Mockito.mock(StatusLine.class);
+
+        Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
+
+        Mockito.when(statusLine.getStatusCode()).thenReturn(200);
+        
+        final var metadata = 
+                new ClassPathResource("/net/shibboleth/oidc/metadata/impl/openid-configuration.json");
+        final var metadataAsString = CharStreams.toString(new InputStreamReader(
+                metadata.getInputStream(), StandardCharsets.UTF_8));;
+        
+        Mockito.when(httpResponse.getEntity()).thenReturn(new StringEntity(metadataAsString));
+        Mockito.when(httpClient.execute((HttpUriRequest) Mockito.any(), (ResponseHandler) Mockito.any(),
+                (HttpContext) Mockito.any()))
+                .thenReturn(OIDCProviderMetadata.parse(metadataAsString));
+        
+        
+        
+        final HTTPProviderConfigurationFetchingStrategy strategy = 
+                new HTTPProviderConfigurationFetchingStrategy(httpClient, 
+                        new OIDCProviderMetadataResponseHandler());
+        
+        final var fetchedMetadata = 
+                strategy.apply(new CriteriaSet(new IssuerIDCriterion(new Issuer("https://op.example.com"))));
+        
+        assertNotNull(fetchedMetadata);
+        assertEquals(fetchedMetadata.getIssuer().getValue(), "https://op.example.com");
+    }
+    
+    @Test
+    public void testResponseHandler_Success() throws Exception {
+        
+        final OIDCProviderMetadataResponseHandler responseHandler = new OIDCProviderMetadataResponseHandler();
+
+        final HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
+        final StatusLine statusLine = Mockito.mock(StatusLine.class);
+        Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
+        Mockito.when(statusLine.getStatusCode()).thenReturn(200);
+        
+        final var metadata = 
+                new ClassPathResource("/net/shibboleth/oidc/metadata/impl/openid-configuration.json");
+        final var metadataAsString = CharStreams.toString(new InputStreamReader(
+                metadata.getInputStream(), StandardCharsets.UTF_8));
+        
+        final var StringEntity = new StringEntity(metadataAsString);
+        StringEntity.setContentType(new BasicHeader("Content-Type", "application/json"));        
+        Mockito.when(httpResponse.getEntity()).thenReturn(StringEntity);
+
+        final var handledMetadata = responseHandler.handleResponse(httpResponse);
+        
+        assertNotNull(handledMetadata);
+        assertEquals(handledMetadata.getIssuer().getValue(), "https://op.example.com");
+    }
+    
+    
+    @Test
+    public void testResponseHandler_304Response_NoMetadata() throws Exception {
+        
+        final OIDCProviderMetadataResponseHandler responseHandler = new OIDCProviderMetadataResponseHandler();
+
+        final HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
+        final StatusLine statusLine = Mockito.mock(StatusLine.class);
+        Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
+        Mockito.when(statusLine.getStatusCode()).thenReturn(304);        
+
+        final var handledMetadata = responseHandler.handleResponse(httpResponse);        
+        assertNull(handledMetadata);
+
+    }
+    
+    @Test
+    public void testResponseHandler_500Response_NoMetadata() throws Exception {
+        
+        final OIDCProviderMetadataResponseHandler responseHandler = new OIDCProviderMetadataResponseHandler();
+
+        final HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
+        final StatusLine statusLine = Mockito.mock(StatusLine.class);
+        Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
+        Mockito.when(statusLine.getStatusCode()).thenReturn(500);
+
+        final var handledMetadata = responseHandler.handleResponse(httpResponse);        
+        assertNull(handledMetadata);
+    }
+    
+    @Test
+    public void testResponseHandlerWrongContentType_NoMetadata() throws Exception {
+        
+        final OIDCProviderMetadataResponseHandler responseHandler = new OIDCProviderMetadataResponseHandler();
+
+        final HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
+        final StatusLine statusLine = Mockito.mock(StatusLine.class);
+        Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
+        Mockito.when(statusLine.getStatusCode()).thenReturn(200);
+        
+        final var metadata = 
+                new ClassPathResource("/net/shibboleth/oidc/metadata/impl/openid-configuration.json");
+        final var metadataAsString = CharStreams.toString(new InputStreamReader(
+                metadata.getInputStream(), StandardCharsets.UTF_8));
+        
+        final var StringEntity = new StringEntity(metadataAsString);
+        StringEntity.setContentType(new BasicHeader("Content-Type", "application/xml"));        
+        Mockito.when(httpResponse.getEntity()).thenReturn(StringEntity);
+
+        final var handledMetadata = responseHandler.handleResponse(httpResponse);
+        
+        assertNull(handledMetadata);
+
+    }
+ 
+    @Test
+    public void testResponseHandlerEmptyEntity_NoMetadata() throws Exception {
+        
+        final OIDCProviderMetadataResponseHandler responseHandler = new OIDCProviderMetadataResponseHandler();
+
+        final HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
+        final StatusLine statusLine = Mockito.mock(StatusLine.class);
+        Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
+        Mockito.when(statusLine.getStatusCode()).thenReturn(200);
+        
+        final var StringEntity = new StringEntity("");
+        StringEntity.setContentType(new BasicHeader("Content-Type", "application/json"));        
+        Mockito.when(httpResponse.getEntity()).thenReturn(StringEntity);
+
+        final var handledMetadata = responseHandler.handleResponse(httpResponse);
+        
+        assertNull(handledMetadata);
+
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list