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