Search in sources :

Example 16 with RequestAttributes

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();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) PropertyAccessor(org.springframework.beans.PropertyAccessor) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) URI(java.net.URI) ResponseEntity(org.springframework.http.ResponseEntity) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RestTemplate(org.springframework.web.client.RestTemplate) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 17 with RequestAttributes

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();
}
Also used : Cookie(javax.servlet.http.Cookie) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MessageHeaders(org.springframework.messaging.MessageHeaders) HashMap(java.util.HashMap) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) MultiValueMap(org.springframework.util.MultiValueMap) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 18 with RequestAttributes

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);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Example 19 with RequestAttributes

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();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) Before(org.junit.Before)

Example 20 with RequestAttributes

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;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Aggregations

RequestAttributes (org.springframework.web.context.request.RequestAttributes)76 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)46 HttpServletRequest (javax.servlet.http.HttpServletRequest)15 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 Test (org.junit.Test)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Response (javax.ws.rs.core.Response)3 XWorkRequestAttributes (org.onebusaway.presentation.impl.users.XWorkRequestAttributes)3 DBUnitTest (org.orcid.test.DBUnitTest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 ProjectService (ca.corefacility.bioinformatics.irida.service.ProjectService)2 UserService (ca.corefacility.bioinformatics.irida.service.user.UserService)2 UserSession (com.haulmont.cuba.security.global.UserSession)2 ActionContext (com.opensymphony.xwork2.ActionContext)2 RecordDefinition (com.revolsys.record.schema.RecordDefinition)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2