|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package sample; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | +import static org.springframework.http.HttpMethod.GET; |
| 22 | +import static org.springframework.http.HttpMethod.POST; |
| 23 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 24 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 25 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 26 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 27 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 28 | +import static sample.JwkSetEndpointFilter.WELL_KNOWN_JWK_URIS; |
| 29 | + |
| 30 | +import org.hamcrest.Matchers; |
| 31 | +import org.junit.jupiter.api.BeforeAll; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.api.TestInstance; |
| 34 | +import org.junit.jupiter.api.TestInstance.Lifecycle; |
| 35 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 36 | +import org.springframework.test.web.servlet.MockMvc; |
| 37 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 38 | + |
| 39 | +import com.nimbusds.jose.JOSEException; |
| 40 | +import com.nimbusds.jose.jwk.JWK; |
| 41 | +import com.nimbusds.jose.jwk.JWKSet; |
| 42 | +import com.nimbusds.jose.jwk.KeyUse; |
| 43 | +import com.nimbusds.jose.jwk.gen.RSAKeyGenerator; |
| 44 | + |
| 45 | +@TestInstance(Lifecycle.PER_CLASS) |
| 46 | +public class JwkSetEndpointFilterTest { |
| 47 | + |
| 48 | + private MockMvc mvc; |
| 49 | + private JWKSet jwkSet; |
| 50 | + private JWK jwk; |
| 51 | + private JwkSetEndpointFilter filter; |
| 52 | + |
| 53 | + @BeforeAll |
| 54 | + void setup() throws JOSEException { |
| 55 | + this.jwk = new RSAKeyGenerator(2048).keyID("endpoint-test").keyUse(KeyUse.ENCRYPTION).generate(); |
| 56 | + this.jwkSet = new JWKSet(jwk); |
| 57 | + this.filter = new JwkSetEndpointFilter(jwkSet); |
| 58 | + this.mvc = MockMvcBuilders.standaloneSetup(new FakeController()).addFilters(filter) |
| 59 | + .alwaysDo(print()).build(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testRequestMatcher() { |
| 64 | + assertTrue(this.filter.ifRequestMatches(new MockHttpServletRequest(GET.name(), WELL_KNOWN_JWK_URIS))); |
| 65 | + |
| 66 | + assertFalse(this.filter.ifRequestMatches(new MockHttpServletRequest(POST.name(), WELL_KNOWN_JWK_URIS))); |
| 67 | + assertFalse(this.filter.ifRequestMatches(new MockHttpServletRequest(GET.name(), "/stuff" + WELL_KNOWN_JWK_URIS))); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void testResponseIfRequestMatches() throws Exception { |
| 72 | + mvc.perform(get(WELL_KNOWN_JWK_URIS)).andDo(print()).andExpect(status().isOk()) |
| 73 | + .andExpect(jsonPath("$.keys").isArray()).andExpect(jsonPath("$.keys").isNotEmpty()) |
| 74 | + .andExpect(jsonPath("$.keys[0].kid").value(jwk.getKeyID())) |
| 75 | + .andExpect(jsonPath("$.keys[0].kty").value(jwk.getKeyType().toString())); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void testResponseIfNotRequestMatches() throws Exception { |
| 80 | + mvc.perform(get("/fake")).andDo(print()).andExpect(status().isOk()) |
| 81 | + .andExpect(content().string(Matchers.is("fake"))); |
| 82 | + } |
| 83 | +} |
0 commit comments