use of org.springframework.web.context.request.RequestAttributes in project spring-integration by spring-projects.
the class HttpProxyScenarioTests method testHttpMultipartProxyScenario.
@Test
public void testHttpMultipartProxyScenario() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/testmp");
request.addHeader("Connection", "Keep-Alive");
request.setContentType("multipart/form-data;boundary=----WebKitFormBoundarywABD2xqC1FLBijlQ");
request.setContent("foo".getBytes());
Object handler = this.handlerMapping.getHandler(request).getHandler();
assertNotNull(handler);
MockHttpServletResponse response = new MockHttpServletResponse();
RestTemplate template = Mockito.spy(new RestTemplate());
Mockito.doAnswer(invocation -> {
URI uri = invocation.getArgument(0);
assertEquals(new URI("http://testServer/testmp"), uri);
HttpEntity<?> httpEntity = (HttpEntity<?>) invocation.getArguments()[2];
HttpHeaders httpHeaders = httpEntity.getHeaders();
assertEquals("Keep-Alive", httpHeaders.getFirst("Connection"));
assertEquals("multipart/form-data;boundary=----WebKitFormBoundarywABD2xqC1FLBijlQ", httpHeaders.getContentType().toString());
HttpEntity<?> entity = (HttpEntity<?>) invocation.getArguments()[2];
assertThat(entity.getBody(), instanceOf(byte[].class));
assertEquals("foo", new String((byte[]) entity.getBody()));
MultiValueMap<String, String> responseHeaders = new LinkedMultiValueMap<String, String>(httpHeaders);
responseHeaders.set("Connection", "close");
responseHeaders.set("Content-Type", "text/plain");
return new ResponseEntity<Object>(responseHeaders, HttpStatus.OK);
}).when(template).exchange(Mockito.any(URI.class), Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class), (Class<?>) isNull());
PropertyAccessor dfa = new DirectFieldAccessor(this.handlermp);
dfa.setPropertyValue("restTemplate", template);
RequestAttributes attributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(attributes);
this.handlerAdapter.handle(request, response, handler);
assertEquals("close", response.getHeaderValue("Connection"));
assertEquals("text/plain", response.getContentType());
RequestContextHolder.resetRequestAttributes();
}
use of org.springframework.web.context.request.RequestAttributes in project spring-integration by spring-projects.
the class Int2312RequestMappingIntegrationTests method testURIVariablesAndHeaders.
@Test
@SuppressWarnings("unchecked")
public // INT-1362
void testURIVariablesAndHeaders() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
String testRequest = "aBc;q1=1;q2=2";
String requestURI = "/test/" + testRequest;
request.setRequestURI(requestURI);
request.setContentType("text/plain");
final Map<String, String> params = new HashMap<String, String>();
params.put("foo", "bar");
request.setParameters(params);
request.setContent("hello".getBytes());
final Cookie cookie = new Cookie("foo", "bar");
request.setCookies(cookie);
request.addHeader("toLowerCase", true);
// See org.springframework.web.servlet.FrameworkServlet#initContextHolders
final RequestAttributes attributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(attributes);
this.toLowerCaseChannel.subscribe(message -> {
MessageHeaders headers = message.getHeaders();
assertEquals(attributes, headers.get("requestAttributes"));
Object requestParams = headers.get("requestParams");
assertNotNull(requestParams);
assertEquals(params, ((MultiValueMap<String, String>) requestParams).toSingleValueMap());
Object matrixVariables = headers.get("matrixVariables");
assertThat(matrixVariables, Matchers.instanceOf(Map.class));
Object value = ((Map<?, ?>) matrixVariables).get("value");
assertThat(value, Matchers.instanceOf(MultiValueMap.class));
assertEquals("1", ((MultiValueMap<String, ?>) value).getFirst("q1"));
assertEquals("2", ((MultiValueMap<String, ?>) value).getFirst("q2"));
Object requestHeaders = headers.get("requestHeaders");
assertNotNull(requestParams);
assertEquals(MediaType.TEXT_PLAIN, ((HttpHeaders) requestHeaders).getContentType());
Map<String, Cookie> cookies = (Map<String, Cookie>) headers.get("cookies");
assertEquals(1, cookies.size());
Cookie foo = cookies.get("foo");
assertNotNull(foo);
assertEquals(cookie, foo);
});
MockHttpServletResponse response = new MockHttpServletResponse();
Object handler = this.handlerMapping.getHandler(request).getHandler();
this.handlerAdapter.handle(request, response, handler);
final String testResponse = response.getContentAsString();
assertEquals(testRequest.split(";")[0].toLowerCase(), testResponse);
RequestContextHolder.resetRequestAttributes();
}
use of org.springframework.web.context.request.RequestAttributes in project irida by phac-nml.
the class UnitTestListener method testRunStarted.
/**
* {@inheritDoc}
*/
public void testRunStarted(Description description) throws Exception {
logger.debug("Configuring Spring MockHTTPServletRequest.");
// fake out the servlet response so that the URI builder will work.
RequestAttributes ra = new ServletRequestAttributes(new MockHttpServletRequest());
RequestContextHolder.setRequestAttributes(ra);
}
use of org.springframework.web.context.request.RequestAttributes in project irida by phac-nml.
the class RootControllerTest method setUp.
@Before
public void setUp() {
// fake out the servlet response so that the URI builder will work.
RequestAttributes ra = new ServletRequestAttributes(new MockHttpServletRequest());
RequestContextHolder.setRequestAttributes(ra);
controller.initLinks();
}
use of org.springframework.web.context.request.RequestAttributes in project BroadleafCommerce by BroadleafCommerce.
the class FrameworkMvcUriComponentsBuilder method getWebApplicationContext.
private static WebApplicationContext getWebApplicationContext() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes == null) {
logger.debug("No request bound to the current thread: not in a DispatcherServlet request?");
return null;
}
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
WebApplicationContext wac = (WebApplicationContext) request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (wac == null) {
logger.debug("No WebApplicationContext found: not in a DispatcherServlet request?");
return null;
}
return wac;
}
Aggregations