Java Code Examples for org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder#headers()

The following examples show how to use org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder#headers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HomeControllerTestIT.java    From blackduck-alert with Apache License 2.0 5 votes vote down vote up
@Test
@WithMockUser(roles = AlertIntegrationTest.ROLE_ALERT_ADMIN)
public void testVerifyNoToken() throws Exception {
    final HttpHeaders headers = new HttpHeaders();
    headers.add("X-CSRF-TOKEN", UUID.randomUUID().toString());
    final MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get(HOME_VERIFY_URL)
                                                      .with(SecurityMockMvcRequestPostProcessors.user("admin").roles(AlertIntegrationTest.ROLE_ALERT_ADMIN))
                                                      .with(SecurityMockMvcRequestPostProcessors.csrf());
    request.headers(headers);
    mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isUnauthorized());
}
 
Example 2
Source File: HomeControllerTestIT.java    From blackduck-alert with Apache License 2.0 5 votes vote down vote up
@Test
@WithMockUser(roles = AlertIntegrationTest.ROLE_ALERT_ADMIN)
public void testVerifyNullStringCSRFToken() throws Exception {
    final MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get(HOME_VERIFY_URL)
                                                      .with(SecurityMockMvcRequestPostProcessors.user("admin").roles(AlertIntegrationTest.ROLE_ALERT_ADMIN))
                                                      .with(SecurityMockMvcRequestPostProcessors.csrf());
    final HttpHeaders headers = new HttpHeaders();
    headers.add("X-CSRF-TOKEN", "null");
    request.headers(headers);
    mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isUnauthorized());
}
 
Example 3
Source File: UserControllerTest.java    From controller-advice-exception-handler with MIT License 5 votes vote down vote up
private ResultActions asyncGet(String route, HttpHeaders headers) throws Exception {
    MockHttpServletRequestBuilder builder = get(route);

    if(headers != null) {
        builder.headers(headers);
    }

    MvcResult resultActions = mockMvc.perform(builder)
            .andExpect(request().asyncStarted())
            .andReturn();

    return mockMvc.perform(asyncDispatch(resultActions));
}