Search in sources :

Example 56 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-security-oauth by spring-projects.

the class ClientConfigurationTests method testAuthCodeRedirect.

@Test
public void testAuthCodeRedirect() throws Exception {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(ClientContext.class);
    context.refresh();
    MockMvc mvc = MockMvcBuilders.webAppContextSetup(context).addFilters(new OAuth2ClientContextFilter()).build();
    mvc.perform(MockMvcRequestBuilders.get("/photos")).andExpect(MockMvcResultMatchers.status().isFound()).andExpect(MockMvcResultMatchers.header().string("Location", CoreMatchers.startsWith("http://example.com/authorize")));
    context.close();
}
Also used : OAuth2ClientContextFilter(org.springframework.security.oauth2.client.filter.OAuth2ClientContextFilter) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 57 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-security-oauth by spring-projects.

the class ResourceServerConfigurationTests method testWithAuthServerCustomPath.

@Test
public void testWithAuthServerCustomPath() throws Exception {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(ResourceServerAndAuthorizationServerCustomPathContext.class);
    context.refresh();
    MockMvc mvc = buildMockMvc(context);
    mvc.perform(MockMvcRequestBuilders.get("/")).andExpect(MockMvcResultMatchers.header().string("WWW-Authenticate", containsString("Bearer")));
    mvc.perform(MockMvcRequestBuilders.post("/token")).andExpect(MockMvcResultMatchers.header().string("WWW-Authenticate", containsString("Basic")));
    mvc.perform(MockMvcRequestBuilders.get("/authorize")).andExpect(MockMvcResultMatchers.redirectedUrl("http://localhost/login"));
    mvc.perform(MockMvcRequestBuilders.post("/token").header("Authorization", "Basic " + new String(Base64.encode("client:secret".getBytes())))).andExpect(MockMvcResultMatchers.content().string(containsString("Missing grant type")));
    context.close();
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 58 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-security-oauth by spring-projects.

the class ResourceServerConfigurationTests method testCustomTokenServices.

@Test
public void testCustomTokenServices() throws Exception {
    tokenStore.storeAccessToken(token, authentication);
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(TokenServicesContext.class);
    context.refresh();
    MockMvc mvc = buildMockMvc(context);
    mvc.perform(MockMvcRequestBuilders.get("/")).andExpect(MockMvcResultMatchers.status().isUnauthorized());
    mvc.perform(MockMvcRequestBuilders.get("/").header("Authorization", "Bearer FOO")).andExpect(MockMvcResultMatchers.status().isNotFound());
    context.close();
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 59 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-security-oauth by spring-projects.

the class ResourceServerConfigurationTests method testCustomAuthenticationEntryPoint.

@Test
public void testCustomAuthenticationEntryPoint() throws Exception {
    tokenStore.storeAccessToken(token, authentication);
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(AuthenticationEntryPointContext.class);
    context.refresh();
    MockMvc mvc = buildMockMvc(context);
    mvc.perform(MockMvcRequestBuilders.get("/").header("Authorization", "Bearer FOO")).andExpect(MockMvcResultMatchers.status().isFound());
    context.close();
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 60 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-security-oauth by spring-projects.

the class ResourceServerConfigurationTests method testDefaults.

@Test
public void testDefaults() throws Exception {
    tokenStore.storeAccessToken(token, authentication);
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(ResourceServerContext.class);
    context.refresh();
    MockMvc mvc = buildMockMvc(context);
    mvc.perform(MockMvcRequestBuilders.get("/")).andExpect(MockMvcResultMatchers.status().isUnauthorized());
    mvc.perform(MockMvcRequestBuilders.get("/").header("Authorization", "Bearer FOO")).andExpect(MockMvcResultMatchers.status().isNotFound());
    context.close();
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Aggregations

MockMvc (org.springframework.test.web.servlet.MockMvc)97 Test (org.junit.Test)91 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)25 Todo (org.springframework.sync.Todo)16 List (java.util.List)15 TodoRepository (org.springframework.sync.TodoRepository)15 Filter (javax.servlet.Filter)13 MockServletContext (org.springframework.mock.web.MockServletContext)13 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)12 OncePerRequestFilter (org.springframework.web.filter.OncePerRequestFilter)9 MvcResult (org.springframework.test.web.servlet.MvcResult)6 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)5 Ignore (org.junit.Ignore)3 FilterChainProxy (org.springframework.security.web.FilterChainProxy)3 WebConnection (com.gargoylesoftware.htmlunit.WebConnection)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 HttpSession (javax.servlet.http.HttpSession)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2