Skip to content

Commit 6964c2e

Browse files
author
Rob Winch
committed
SEC-2633: Add MockMvc tests for insecuremvc
1 parent 8f84921 commit 6964c2e

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2002-2014 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+
* 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,
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+
package org.springframework.security.samples.config;
17+
18+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
19+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
20+
21+
import javax.servlet.Filter;
22+
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.security.samples.mvc.config.WebMvcConfiguration;
28+
import org.springframework.test.context.ContextConfiguration;
29+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30+
import org.springframework.test.context.web.WebAppConfiguration;
31+
import org.springframework.test.web.servlet.MockMvc;
32+
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
33+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
34+
import org.springframework.web.context.WebApplicationContext;
35+
36+
/**
37+
* @author Rob Winch
38+
*
39+
*/
40+
@RunWith(SpringJUnit4ClassRunner.class)
41+
@ContextConfiguration(classes={RootConfiguration.class, WebMvcConfiguration.class})
42+
@WebAppConfiguration
43+
public class SecurityConfigTests {
44+
private MockMvc mvc;
45+
46+
@Autowired
47+
private WebApplicationContext context;
48+
49+
@Autowired
50+
private Filter springSecurityFilterChain;
51+
52+
@Before
53+
public void setup() {
54+
mvc = MockMvcBuilders
55+
.webAppContextSetup(context)
56+
.addFilters(springSecurityFilterChain)
57+
.build();
58+
}
59+
60+
@Test
61+
public void composeMessage() throws Exception {
62+
MockHttpServletRequestBuilder composeMessage =
63+
post("/")
64+
.param("summary", "New Message")
65+
.param("text", "This is a new message");
66+
67+
mvc.perform(composeMessage)
68+
.andExpect(redirectedUrlPattern("/*"));
69+
}
70+
}

0 commit comments

Comments
 (0)