1
+ /*
2
+ * Copyright 2002-2015 the original author or authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
+ * use this file except in compliance with the License. You may obtain a copy of
6
+ * the License at
7
+ *
8
+ * http://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, WITHOUT
12
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ * License for the specific language governing permissions and limitations under
14
+ * the License.
15
+ */
16
+ package sample .mvc ;
17
+
18
+ import static org .springframework .security .test .web .servlet .setup .SecurityMockMvcConfigurers .springSecurity ;
19
+ import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .*;
20
+
21
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
22
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
23
+
24
+ import org .junit .Before ;
25
+ import org .junit .Test ;
26
+ import org .junit .runner .RunWith ;
27
+ import org .springframework .beans .factory .annotation .Autowired ;
28
+ import org .springframework .security .test .context .support .WithMockUser ;
29
+ import org .springframework .session .ExpiringSession ;
30
+ import org .springframework .session .web .http .SessionRepositoryFilter ;
31
+ import org .springframework .test .context .ContextConfiguration ;
32
+ import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
33
+ import org .springframework .test .context .web .WebAppConfiguration ;
34
+ import org .springframework .test .web .servlet .MockMvc ;
35
+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
36
+ import org .springframework .web .context .WebApplicationContext ;
37
+
38
+ import sample .HttpSessionConfig ;
39
+ import sample .SecurityConfig ;
40
+
41
+ @ RunWith (SpringJUnit4ClassRunner .class )
42
+ @ ContextConfiguration (classes = {SecurityConfig .class , MvcConfig .class , HttpSessionConfig .class })
43
+ @ WebAppConfiguration
44
+ public class RestMockMvcTests {
45
+
46
+ @ Autowired
47
+ WebApplicationContext context ;
48
+ @ Autowired
49
+ SessionRepositoryFilter <? extends ExpiringSession > sessionFilter ;
50
+
51
+ MockMvc mockMvc ;
52
+
53
+ @ Before
54
+ public void setup () {
55
+ mockMvc = MockMvcBuilders
56
+ .webAppContextSetup (context )
57
+ .addFilters (sessionFilter )
58
+ .apply (springSecurity ())
59
+ .build ();
60
+ }
61
+
62
+ @ WithMockUser
63
+ @ Test
64
+ public void autheticatedAnnotation () throws Exception {
65
+ mockMvc .perform (get ("/" ))
66
+ .andExpect (content ().string ("{\" username\" :\" user\" }" ));
67
+
68
+ }
69
+
70
+ @ Test
71
+ public void autheticatedRequestPostProcessor () throws Exception {
72
+ mockMvc .perform (get ("/" ).with (user ("user" )))
73
+ .andExpect (content ().string ("{\" username\" :\" user\" }" ));
74
+
75
+ }
76
+
77
+ @ Test
78
+ public void unauthenticated () throws Exception {
79
+ mockMvc .perform (get ("/" ))
80
+ .andExpect (status ().isUnauthorized ());
81
+
82
+ }
83
+ }
0 commit comments