Search in sources :

Example 1 with RequestBuilder

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;
        }
    };
}
Also used : RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletContext(javax.servlet.ServletContext)

Example 2 with RequestBuilder

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;
}
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 RequestBuilder

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) {
        }
    }));
}
Also used : Description(org.hamcrest.Description) RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) BaseMatcher(org.hamcrest.BaseMatcher) Message(sample.data.Message) List(java.util.List) Test(org.junit.Test)

Example 4 with RequestBuilder

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("/"));
}
Also used : RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) Test(org.junit.Test)

Example 5 with RequestBuilder

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());
}
Also used : RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) Test(org.junit.Test)

Aggregations

RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)9 Test (org.junit.Test)7 List (java.util.List)1 ServletContext (javax.servlet.ServletContext)1 BaseMatcher (org.hamcrest.BaseMatcher)1 Description (org.hamcrest.Description)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 SmartRequestBuilder (org.springframework.test.web.servlet.SmartRequestBuilder)1 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)1 Message (sample.data.Message)1