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);
}
}
};
}
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;
}
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());
}
}
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());
}
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());
}
Aggregations