Search in sources :

Example 1 with MockHttpServletRequest

use of org.springframework.mock.web.test.MockHttpServletRequest in project spring-framework by spring-projects.

the class OpenEntityManagerInViewTests method testOpenEntityManagerInViewInterceptor.

@Test
public void testOpenEntityManagerInViewInterceptor() throws Exception {
    OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
    interceptor.setEntityManagerFactory(this.factory);
    MockServletContext sc = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    interceptor.preHandle(new ServletWebRequest(request));
    assertTrue(TransactionSynchronizationManager.hasResource(this.factory));
    // check that further invocations simply participate
    interceptor.preHandle(new ServletWebRequest(request));
    interceptor.preHandle(new ServletWebRequest(request));
    interceptor.postHandle(new ServletWebRequest(request), null);
    interceptor.afterCompletion(new ServletWebRequest(request), null);
    interceptor.postHandle(new ServletWebRequest(request), null);
    interceptor.afterCompletion(new ServletWebRequest(request), null);
    interceptor.preHandle(new ServletWebRequest(request));
    interceptor.postHandle(new ServletWebRequest(request), null);
    interceptor.afterCompletion(new ServletWebRequest(request), null);
    interceptor.postHandle(new ServletWebRequest(request), null);
    assertTrue(TransactionSynchronizationManager.hasResource(factory));
    given(manager.isOpen()).willReturn(true);
    interceptor.afterCompletion(new ServletWebRequest(request), null);
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
    verify(manager).close();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 2 with MockHttpServletRequest

use of org.springframework.mock.web.test.MockHttpServletRequest in project spring-framework by spring-projects.

the class OpenEntityManagerInViewTests method setUp.

@Before
public void setUp() throws Exception {
    factory = mock(EntityManagerFactory.class);
    manager = mock(EntityManager.class);
    given(factory.createEntityManager()).willReturn(manager);
    this.request = new MockHttpServletRequest();
    this.request.setAsyncSupported(true);
    this.response = new MockHttpServletResponse();
    this.webRequest = new ServletWebRequest(this.request);
}
Also used : EntityManager(javax.persistence.EntityManager) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) EntityManagerFactory(javax.persistence.EntityManagerFactory) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 3 with MockHttpServletRequest

use of org.springframework.mock.web.test.MockHttpServletRequest in project spring-framework by spring-projects.

the class StandaloneMockMvcBuilderTests method placeHoldersInRequestMapping.

// SPR-10825
@Test
public void placeHoldersInRequestMapping() throws Exception {
    TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PlaceholderController());
    builder.addPlaceholderValue("sys.login.ajax", "/foo");
    builder.build();
    RequestMappingHandlerMapping hm = builder.wac.getBean(RequestMappingHandlerMapping.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    HandlerExecutionChain chain = hm.getHandler(request);
    assertNotNull(chain);
    assertEquals("handleWithPlaceholders", ((HandlerMethod) chain.getHandler()).getMethod().getName());
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 4 with MockHttpServletRequest

use of org.springframework.mock.web.test.MockHttpServletRequest in project spring-framework by spring-projects.

the class ObjectToStringHttpMessageConverterTests method read.

@Test
public void read() throws IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContentType(MediaType.TEXT_PLAIN_VALUE);
    Short shortValue = Short.valueOf((short) 781);
    request.setContent(shortValue.toString().getBytes(StringHttpMessageConverter.DEFAULT_CHARSET));
    assertEquals(shortValue, this.converter.read(Short.class, new ServletServerHttpRequest(request)));
    Float floatValue = Float.valueOf(123);
    request.setCharacterEncoding("UTF-16");
    request.setContent(floatValue.toString().getBytes("UTF-16"));
    assertEquals(floatValue, this.converter.read(Float.class, new ServletServerHttpRequest(request)));
    Long longValue = Long.valueOf(55819182821331L);
    request.setCharacterEncoding("UTF-8");
    request.setContent(longValue.toString().getBytes("UTF-8"));
    assertEquals(longValue, this.converter.read(Long.class, new ServletServerHttpRequest(request)));
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 5 with MockHttpServletRequest

use of org.springframework.mock.web.test.MockHttpServletRequest in project spring-framework by spring-projects.

the class ServerHttpRequestTests method createHttpRequest.

private ServerHttpRequest createHttpRequest(String path) throws Exception {
    HttpServletRequest request = new MockHttpServletRequest("GET", path) {

        @Override
        public ServletInputStream getInputStream() {
            return new TestServletInputStream();
        }
    };
    AsyncContext asyncContext = new MockAsyncContext(request, new MockHttpServletResponse());
    return new ServletServerHttpRequest(request, asyncContext, new DefaultDataBufferFactory(), 1024);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) AsyncContext(javax.servlet.AsyncContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse)

Aggregations

MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)646 Test (org.junit.Test)540 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)330 Before (org.junit.Before)78 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)77 MockServletContext (org.springframework.mock.web.test.MockServletContext)56 TestBean (org.springframework.tests.sample.beans.TestBean)43 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)41 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)37 HttpServletRequest (javax.servlet.http.HttpServletRequest)35 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)30 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)29 ITestBean (org.springframework.tests.sample.beans.ITestBean)28 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)28 ServletException (javax.servlet.ServletException)27 HashMap (java.util.HashMap)26 FilterChain (javax.servlet.FilterChain)26 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)23 Cookie (javax.servlet.http.Cookie)22