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