Search in sources :

Example 1 with MockHttpServletRequestBuilder

use of org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder in project spring-framework by spring-projects.

the class MockMvcClientHttpRequestFactory method createRequest.

@Override
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
    return new MockClientHttpRequest(httpMethod, uri) {

        @Override
        public ClientHttpResponse executeInternal() throws IOException {
            try {
                MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri);
                requestBuilder.content(getBodyAsBytes());
                requestBuilder.headers(getHeaders());
                MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn();
                MockHttpServletResponse servletResponse = mvcResult.getResponse();
                HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
                byte[] body = servletResponse.getContentAsByteArray();
                HttpHeaders headers = getResponseHeaders(servletResponse);
                MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
                clientResponse.getHeaders().putAll(headers);
                return clientResponse;
            } catch (Exception ex) {
                byte[] body = ex.toString().getBytes(StandardCharsets.UTF_8);
                return new MockClientHttpResponse(body, HttpStatus.INTERNAL_SERVER_ERROR);
            }
        }
    };
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) IOException(java.io.IOException) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse)

Example 2 with MockHttpServletRequestBuilder

use of org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder in project spring-framework by spring-projects.

the class HtmlUnitRequestBuilder method merge.

@Override
public Object merge(Object parent) {
    if (parent instanceof RequestBuilder) {
        if (parent instanceof MockHttpServletRequestBuilder) {
            MockHttpServletRequestBuilder copiedParent = MockMvcRequestBuilders.get("/");
            copiedParent.merge(parent);
            this.parentBuilder = copiedParent;
        } else {
            this.parentBuilder = (RequestBuilder) parent;
        }
        if (parent instanceof SmartRequestBuilder) {
            this.parentPostProcessor = (SmartRequestBuilder) parent;
        }
    }
    return this;
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) SmartRequestBuilder(org.springframework.test.web.servlet.SmartRequestBuilder) SmartRequestBuilder(org.springframework.test.web.servlet.SmartRequestBuilder) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)

Example 3 with MockHttpServletRequestBuilder

use of org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder in project spring-boot by spring-projects.

the class HalBrowserMvcEndpointDisabledIntegrationTests method endpointsDoNotHaveLinks.

@Test
public void endpointsDoNotHaveLinks() throws Exception {
    for (MvcEndpoint endpoint : this.mvcEndpoints.getEndpoints()) {
        String path = endpoint.getPath();
        if ("/actuator".equals(path) || endpoint instanceof HeapdumpMvcEndpoint) {
            continue;
        }
        path = path.length() > 0 ? path : "/";
        MockHttpServletRequestBuilder requestBuilder = get(path);
        if (endpoint instanceof AuditEventsMvcEndpoint) {
            requestBuilder.param("after", "2016-01-01T12:00:00+00:00");
        }
        this.mockMvc.perform(requestBuilder.accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(jsonPath("$._links").doesNotExist());
    }
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with MockHttpServletRequestBuilder

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

the class SecurityConfigTests method composeMessageRequiresCsrfToken.

@Test
@WithMockUser
public void composeMessageRequiresCsrfToken() throws Exception {
    MockHttpServletRequestBuilder composeMessage = post("/").param("summary", "New Message").param("text", "This is a new message");
    mvc.perform(composeMessage).andExpect(status().isForbidden());
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Example 5 with MockHttpServletRequestBuilder

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

the class BasicAuthenticationTests method httpBasicWhenProvidedThen200.

@Test
public void httpBasicWhenProvidedThen200() throws Exception {
    MockMvc mockMvc = createMockMvc("classpath:/spring/http-security-basic.xml", "classpath:/spring/in-memory-provider.xml", "classpath:/spring/testapp-servlet.xml");
    MockHttpServletRequestBuilder request = get("/secure/index").with(httpBasic("johnc", "johncspassword"));
    mockMvc.perform(request).andExpect(status().isOk());
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Aggregations

MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)9 Test (org.junit.Test)7 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 CsrfToken (org.springframework.security.web.csrf.CsrfToken)2 MockMvc (org.springframework.test.web.servlet.MockMvc)2 MvcResult (org.springframework.test.web.servlet.MvcResult)2 IOException (java.io.IOException)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpStatus (org.springframework.http.HttpStatus)1 MockClientHttpRequest (org.springframework.mock.http.client.MockClientHttpRequest)1 MockClientHttpResponse (org.springframework.mock.http.client.MockClientHttpResponse)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpSession (org.springframework.mock.web.MockHttpSession)1 SecurityContext (org.springframework.security.core.context.SecurityContext)1 SessionDestroyedEvent (org.springframework.security.core.session.SessionDestroyedEvent)1 WithMockUser (org.springframework.security.test.context.support.WithMockUser)1 DefaultCsrfToken (org.springframework.security.web.csrf.DefaultCsrfToken)1 HttpSessionCsrfTokenRepository (org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository)1 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)1