Search in sources :

Example 66 with ResultMatcher

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

the class HeadersConfigurerTests method getWhenPermissionsPolicyConfiguredThenPermissionsPolicyHeaderInResponse.

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

Example 67 with ResultMatcher

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

the class HeadersConfigurerTests method getWhenHpkpWithCustomAgeThenPublicKeyPinsReportOnlyHeaderWithCustomAgeInResponse.

@Test
public void getWhenHpkpWithCustomAgeThenPublicKeyPinsReportOnlyHeaderWithCustomAgeInResponse() throws Exception {
    this.spring.register(HpkpConfigCustomAge.class).autowire();
    ResultMatcher pinsReportOnly = header().string(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY, "max-age=604800 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\"");
    // @formatter:off
    MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(pinsReportOnly).andReturn();
    // @formatter:on
    assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test)

Example 68 with ResultMatcher

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

the class HeadersConfigurerTests method getWhenHpkpWithReportUriThenPublicKeyPinsReportOnlyHeaderWithReportUriInResponse.

@Test
public void getWhenHpkpWithReportUriThenPublicKeyPinsReportOnlyHeaderWithReportUriInResponse() throws Exception {
    this.spring.register(HpkpConfigWithReportURI.class).autowire();
    ResultMatcher pinsReportOnly = header().string(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY, "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\" ; report-uri=\"https://example.net/pkp-report\"");
    // @formatter:off
    MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(pinsReportOnly).andReturn();
    // @formatter:on
    assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test)

Example 69 with ResultMatcher

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

the class HeadersConfigurerTests method getWhenHpkpWithMultiplePinsThenPublicKeyPinsReportOnlyHeaderWithMultiplePinsInResponse.

@Test
public void getWhenHpkpWithMultiplePinsThenPublicKeyPinsReportOnlyHeaderWithMultiplePinsInResponse() throws Exception {
    this.spring.register(HpkpConfigWithPins.class).autowire();
    ResultMatcher pinsReportOnly = header().string(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY, "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\" ; pin-sha256=\"E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g=\"");
    // @formatter:off
    MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(pinsReportOnly).andReturn();
    // @formatter:on
    assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test)

Example 70 with ResultMatcher

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

the class HeadersConfigurerTests method getWhenContentSecurityPolicyWithReportOnlyThenContentSecurityPolicyReportOnlyHeaderInResponse.

@Test
public void getWhenContentSecurityPolicyWithReportOnlyThenContentSecurityPolicyReportOnlyHeaderInResponse() throws Exception {
    this.spring.register(ContentSecurityPolicyReportOnlyConfig.class).autowire();
    ResultMatcher cspReportOnly = header().string(HttpHeaders.CONTENT_SECURITY_POLICY_REPORT_ONLY, "default-src 'self'; script-src trustedscripts.example.com");
    // @formatter:off
    MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(cspReportOnly).andReturn();
    // @formatter:on
    assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.CONTENT_SECURITY_POLICY_REPORT_ONLY);
}
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