Search in sources :

Example 61 with ResultMatcher

use of org.springframework.test.web.servlet.ResultMatcher in project tutorials by eugenp.

the class RequestMapingShortcutsIntegrationTest method giventUrl_whenPostRequest_thenFindPostResponse.

@Test
public void giventUrl_whenPostRequest_thenFindPostResponse() throws Exception {
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/post");
    ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("POST Response");
    this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) Test(org.junit.Test)

Example 62 with ResultMatcher

use of org.springframework.test.web.servlet.ResultMatcher in project tutorials by eugenp.

the class RequestMapingShortcutsIntegrationTest method giventUrl_whenDeleteRequest_thenFindDeleteResponse.

@Test
public void giventUrl_whenDeleteRequest_thenFindDeleteResponse() throws Exception {
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.delete("/delete");
    ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("DELETE Response");
    this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) Test(org.junit.Test)

Example 63 with ResultMatcher

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

the class HeadersConfigurerTests method getWhenContentSecurityPolicyConfiguredThenContentSecurityPolicyHeaderInResponse.

@Test
public void getWhenContentSecurityPolicyConfiguredThenContentSecurityPolicyHeaderInResponse() throws Exception {
    this.spring.register(ContentSecurityPolicyDefaultConfig.class).autowire();
    ResultMatcher csp = header().string(HttpHeaders.CONTENT_SECURITY_POLICY, "default-src 'self'");
    // @formatter:off
    MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(csp).andReturn();
    // @formatter:on
    assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.CONTENT_SECURITY_POLICY);
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test)

Example 64 with ResultMatcher

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

the class HeadersConfigurerTests method configureWhenContentSecurityPolicyNoPolicyDirectivesInLambdaThenDefaultHeaderValue.

@Test
public void configureWhenContentSecurityPolicyNoPolicyDirectivesInLambdaThenDefaultHeaderValue() throws Exception {
    this.spring.register(ContentSecurityPolicyNoDirectivesInLambdaConfig.class).autowire();
    ResultMatcher csp = header().string(HttpHeaders.CONTENT_SECURITY_POLICY, "default-src 'self'");
    // @formatter:off
    MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(csp).andReturn();
    // @formatter:on
    assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.CONTENT_SECURITY_POLICY);
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test)

Example 65 with ResultMatcher

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

the class HeadersConfigurerTests method getWhenReferrerPolicyConfiguredWithCustomValueInLambdaThenCustomValueInResponse.

@Test
public void getWhenReferrerPolicyConfiguredWithCustomValueInLambdaThenCustomValueInResponse() throws Exception {
    this.spring.register(ReferrerPolicyCustomInLambdaConfig.class).autowire();
    ResultMatcher referrerPolicy = header().string("Referrer-Policy", ReferrerPolicy.SAME_ORIGIN.getPolicy());
    // @formatter:off
    MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(referrerPolicy).andReturn();
    // @formatter:on
    assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly("Referrer-Policy");
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test)

Aggregations

ResultMatcher (org.springframework.test.web.servlet.ResultMatcher)74 MvcResult (org.springframework.test.web.servlet.MvcResult)69 Test (org.junit.jupiter.api.Test)22 Cookie (javax.servlet.http.Cookie)15 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)13 ModelAndView (org.springframework.web.servlet.ModelAndView)11 Test (org.junit.Test)5 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)5 BindingResult (org.springframework.validation.BindingResult)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 HandlerMethod (org.springframework.web.method.HandlerMethod)4 Method (java.lang.reflect.Method)2 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HttpStatus (org.springframework.http.HttpStatus)1 MediaType (org.springframework.http.MediaType)1 AssertionErrors (org.springframework.test.util.AssertionErrors)1 StubMvcResult (org.springframework.test.web.servlet.StubMvcResult)1