|
| 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.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | +import static org.hamcrest.Matchers.is; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.never; |
| 23 | +import static org.mockito.Mockito.only; |
| 24 | +import static org.mockito.Mockito.verify; |
| 25 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 26 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 27 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 28 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 29 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 30 | +import static sample.JwkSetEndpointFilter.WELL_KNOWN_JWK_URIS; |
| 31 | + |
| 32 | +import javax.servlet.FilterChain; |
| 33 | +import javax.servlet.http.HttpServletRequest; |
| 34 | +import javax.servlet.http.HttpServletResponse; |
| 35 | + |
| 36 | +import org.junit.jupiter.api.BeforeAll; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import org.junit.jupiter.api.TestInstance; |
| 39 | +import org.junit.jupiter.api.TestInstance.Lifecycle; |
| 40 | +import org.mockito.Mockito; |
| 41 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 42 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 43 | +import org.springframework.test.web.servlet.MockMvc; |
| 44 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 45 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 46 | +import org.springframework.web.bind.annotation.RestController; |
| 47 | + |
| 48 | +import com.nimbusds.jose.JOSEException; |
| 49 | +import com.nimbusds.jose.jwk.JWK; |
| 50 | +import com.nimbusds.jose.jwk.JWKSet; |
| 51 | +import com.nimbusds.jose.jwk.KeyUse; |
| 52 | +import com.nimbusds.jose.jwk.gen.RSAKeyGenerator; |
| 53 | + |
| 54 | +@TestInstance(Lifecycle.PER_CLASS) |
| 55 | +public class JwkSetEndpointFilterTest { |
| 56 | + |
| 57 | + private MockMvc mvc; |
| 58 | + private JWKSet jwkSet; |
| 59 | + private JWK jwk; |
| 60 | + private JwkSetEndpointFilter filter; |
| 61 | + |
| 62 | + @BeforeAll |
| 63 | + void setup() throws JOSEException { |
| 64 | + this.jwk = new RSAKeyGenerator(2048).keyID("endpoint-test").keyUse(KeyUse.SIGNATURE).generate(); |
| 65 | + this.jwkSet = new JWKSet(jwk); |
| 66 | + this.filter = new JwkSetEndpointFilter(jwkSet); |
| 67 | + this.mvc = MockMvcBuilders.standaloneSetup(new FakeController()).addFilters(filter).alwaysDo(print()).build(); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void constructorWhenJsonWebKeySetIsNullThrowIllegalArgumentException() { |
| 72 | + assertThatThrownBy(() -> new JwkSetEndpointFilter(null)).isInstanceOf(IllegalArgumentException.class); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void doFilterWhenPathMatches() throws Exception { |
| 77 | + String requestUri = WELL_KNOWN_JWK_URIS; |
| 78 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri); |
| 79 | + request.setServletPath(requestUri); |
| 80 | + |
| 81 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 82 | + FilterChain filterChain = mock(FilterChain.class); |
| 83 | + |
| 84 | + this.filter.doFilter(request, response, filterChain); |
| 85 | + |
| 86 | + verify(filterChain, never()).doFilter(Mockito.any(HttpServletRequest.class), |
| 87 | + Mockito.any(HttpServletResponse.class)); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void doFilterWhenPathDoesNotMatch() throws Exception { |
| 92 | + String requestUri = "/stuff/" + WELL_KNOWN_JWK_URIS; |
| 93 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri); |
| 94 | + request.setServletPath(requestUri); |
| 95 | + |
| 96 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 97 | + FilterChain filterChain = mock(FilterChain.class); |
| 98 | + |
| 99 | + this.filter.doFilter(request, response, filterChain); |
| 100 | + |
| 101 | + verify(filterChain, only()).doFilter(Mockito.any(HttpServletRequest.class), |
| 102 | + Mockito.any(HttpServletResponse.class)); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testResponseIfRequestMatches() throws Exception { |
| 107 | + mvc.perform(get(WELL_KNOWN_JWK_URIS)).andDo(print()).andExpect(status().isOk()) |
| 108 | + .andExpect(jsonPath("$.keys").isArray()).andExpect(jsonPath("$.keys").isNotEmpty()) |
| 109 | + .andExpect(jsonPath("$.keys[0].kid").value(jwk.getKeyID())) |
| 110 | + .andExpect(jsonPath("$.keys[0].kty").value(jwk.getKeyType().toString())); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void testResponseIfNotRequestMatches() throws Exception { |
| 115 | + mvc.perform(get("/fake")).andDo(print()).andExpect(status().isOk()) |
| 116 | + .andExpect(content().string(is("fake"))); |
| 117 | + } |
| 118 | + |
| 119 | + @RestController |
| 120 | + class FakeController { |
| 121 | + |
| 122 | + @RequestMapping("/fake") |
| 123 | + public String hello() { |
| 124 | + return "fake"; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments