use of org.springframework.test.web.servlet.RequestBuilder in project spring-framework by spring-projects.
the class MockMvcRequestBuilders method asyncDispatch.
/**
* Create a {@link RequestBuilder} for an async dispatch from the
* {@link MvcResult} of the request that started async processing.
* <p>Usage involves performing a request that starts async processing first:
* <pre class="code">
* MvcResult mvcResult = this.mockMvc.perform(get("/1"))
* .andExpect(request().asyncStarted())
* .andReturn();
* </pre>
* <p>And then performing the async dispatch re-using the {@code MvcResult}:
* <pre class="code">
* this.mockMvc.perform(asyncDispatch(mvcResult))
* .andExpect(status().isOk())
* .andExpect(content().contentType(MediaType.APPLICATION_JSON))
* .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
* </pre>
* @param mvcResult the result from the request that started async processing
*/
public static RequestBuilder asyncDispatch(final MvcResult mvcResult) {
// There must be an async result before dispatching
mvcResult.getAsyncResult();
return new RequestBuilder() {
@Override
public MockHttpServletRequest buildRequest(ServletContext servletContext) {
MockHttpServletRequest request = mvcResult.getRequest();
request.setDispatcherType(DispatcherType.ASYNC);
request.setAsyncStarted(false);
return request;
}
};
}
use of org.springframework.test.web.servlet.RequestBuilder 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.RequestBuilder in project gs-spring-security-3.2 by rwinch.
the class SecurityTests method inboxShowsOnlyRobsMessages.
@Test
public void inboxShowsOnlyRobsMessages() throws Exception {
RequestBuilder request = get("/").with(user(rob).roles("USER"));
mvc.perform(request).andExpect(model().attribute("messages", new BaseMatcher<List<Message>>() {
@Override
public boolean matches(Object other) {
@SuppressWarnings("unchecked") List<Message> messages = (List<Message>) other;
return messages.size() == 1 && messages.get(0).getId() == 100;
}
@Override
public void describeTo(Description d) {
}
}));
}
use of org.springframework.test.web.servlet.RequestBuilder in project gs-spring-security-3.2 by rwinch.
the class SecurityTests method validUsernamePassword.
@Test
public void validUsernamePassword() throws Exception {
RequestBuilder request = post("/login").param("username", "rob@example.com").param("password", "password").with(csrf());
mvc.perform(request).andExpect(redirectedUrl("/"));
}
use of org.springframework.test.web.servlet.RequestBuilder in project gs-spring-security-3.2 by rwinch.
the class SecurityTests method robCannotAccessLukesMessage.
@Test
public void robCannotAccessLukesMessage() throws Exception {
RequestBuilder request = get("/110").with(user(rob).roles("USER"));
mvc.perform(request).andExpect(status().isForbidden());
}
Aggregations