@Test public void shouldReturnWithoutIdWhenSingleJwk() throws Exception { UrlJwkProvider provider = new UrlJwkProvider(getClass().getResource("/jwks-single-no-kid.json")); assertThat(provider.get(null), notNullValue()); UrlJwkProvider provider2 = new UrlJwkProvider(getClass().getResource("/jwks-single.json")); assertThat(provider2.get(null), notNullValue()); }
@Test public void shouldFailToLoadByDifferentIdWhenSingleJwk() throws Exception { expectedException.expect(SigningKeyNotFoundException.class); UrlJwkProvider provider = new UrlJwkProvider(getClass().getResource("/jwks-single-no-kid.json")); provider.get("wrong-kid"); }
@Test public void shouldReturnByIdWhenSingleJwk() throws Exception { UrlJwkProvider provider = new UrlJwkProvider(getClass().getResource("/jwks-single.json")); assertThat(provider.get(KID), notNullValue()); }
@Test public void shouldReturnSingleJwkById() throws Exception { UrlJwkProvider provider = new UrlJwkProvider(getClass().getResource("/jwks.json")); assertThat(provider.get(KID), notNullValue()); }
@Test public void shouldFailToLoadSingleWithoutIdWhenMultipleJwk() throws Exception { expectedException.expect(SigningKeyNotFoundException.class); UrlJwkProvider provider = new UrlJwkProvider(getClass().getResource("/jwks.json")); provider.get(null); }
@Test public void shouldFailToLoadSingleWhenJsonIsInvalid() throws Exception { expectedException.expect(SigningKeyNotFoundException.class); UrlJwkProvider provider = new UrlJwkProvider(getClass().getResource("/invalid-jwks.json")); provider.get(KID); }
@Test public void shouldConfigureURLConnectionTimeouts() throws Exception { URLConnection urlConnection = mock(URLConnection.class); // Although somewhat of a hack, this approach gets the job done - this method can // only be called once per virtual machine, but that is sufficient for now. URL.setURLStreamHandlerFactory(new MockURLStreamHandlerFactory(urlConnection)); when(urlConnection.getInputStream()).thenReturn(getClass().getResourceAsStream("/jwks.json")); int connectTimeout = 10000; int readTimeout = 15000; UrlJwkProvider urlJwkProvider = new UrlJwkProvider(new URL("mock://localhost"), connectTimeout, readTimeout); Jwk jwk = urlJwkProvider.get("NkJCQzIyQzRBMEU4NjhGNUU4MzU4RkY0M0ZDQzkwOUQ0Q0VGNUMwQg"); assertNotNull(jwk); ArgumentCaptor<Integer> connectTimeoutCaptor = ArgumentCaptor.forClass(Integer.class); verify(urlConnection).setConnectTimeout(connectTimeoutCaptor.capture()); assertThat(connectTimeoutCaptor.getValue(), is(connectTimeout)); ArgumentCaptor<Integer> readTimeoutCaptor = ArgumentCaptor.forClass(Integer.class); verify(urlConnection).setReadTimeout(readTimeoutCaptor.capture()); assertThat(readTimeoutCaptor.getValue(), is(readTimeout)); } }
@Test public void shouldFailToLoadSingleWhenUrlHasNothing() throws Exception { expectedException.expect(SigningKeyNotFoundException.class); UrlJwkProvider provider = new UrlJwkProvider(new URL("file:///not_found.file")); provider.get(KID); }
@Test public void shouldFailToLoadSingleWhenKeysIsEmpty() throws Exception { expectedException.expect(SigningKeyNotFoundException.class); UrlJwkProvider provider = new UrlJwkProvider(getClass().getResource("/empty-jwks.json")); provider.get(KID); }