Skip to content

Commit e4c74eb

Browse files
author
Rob Winch
committed
Add MockMvc tests to rest sample
1 parent 141aba9 commit e4c74eb

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

samples/rest/src/integration-test/java/rest/RestMockMvcTests.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package rest;
1717

18+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
1819
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
1920
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
2021
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@@ -24,6 +25,7 @@
2425
import org.junit.Test;
2526
import org.junit.runner.RunWith;
2627
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.security.test.context.support.WithMockUser;
2729
import org.springframework.session.ExpiringSession;
2830
import org.springframework.session.web.http.SessionRepositoryFilter;
2931
import org.springframework.test.context.ContextConfiguration;
@@ -56,12 +58,29 @@ public void setup() {
5658
mvc = MockMvcBuilders.webAppContextSetup(context)
5759
.alwaysDo(print())
5860
.addFilters(sessionRepositoryFilter)
59-
.apply(springSecurity()).build();
61+
.apply(springSecurity())
62+
.build();
6063
}
6164

6265
@Test
6366
public void noSessionOnNoCredentials() throws Exception {
6467
mvc.perform(get("/"))
65-
.andExpect(header().doesNotExist("x-auth-token"));
68+
.andExpect(header().doesNotExist("x-auth-token"))
69+
.andExpect(status().isUnauthorized());
70+
}
71+
72+
@WithMockUser
73+
@Test
74+
public void autheticatedAnnotation() throws Exception {
75+
mvc.perform(get("/"))
76+
.andExpect(content().string("{\"username\":\"user\"}"));
77+
78+
}
79+
80+
@Test
81+
public void autheticatedRequestPostProcessor() throws Exception {
82+
mvc.perform(get("/").with(user("user")))
83+
.andExpect(content().string("{\"username\":\"user\"}"));
84+
6685
}
6786
}

0 commit comments

Comments
 (0)