Search in sources :

Example 1 with HttpRequest

use of org.springframework.http.HttpRequest in project spring-framework by spring-projects.

the class InterceptingClientHttpRequestFactoryTests method changeBody.

@Test
public void changeBody() throws Exception {
    final byte[] changedBody = "Foo".getBytes();
    ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            return execution.execute(request, changedBody);
        }
    };
    requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
    ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
    request.execute();
    assertTrue(Arrays.equals(changedBody, requestMock.body.toByteArray()));
}
Also used : HttpRequest(org.springframework.http.HttpRequest) URI(java.net.URI) Test(org.junit.Test)

Example 2 with HttpRequest

use of org.springframework.http.HttpRequest in project spring-framework by spring-projects.

the class InterceptingClientHttpRequestFactoryTests method changeMethod.

@Test
public void changeMethod() throws Exception {
    final HttpMethod changedMethod = HttpMethod.POST;
    ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            return execution.execute(new HttpRequestWrapper(request) {

                @Override
                public HttpMethod getMethod() {
                    return changedMethod;
                }
            }, body);
        }
    };
    requestFactoryMock = new RequestFactoryMock() {

        @Override
        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            assertEquals(changedMethod, httpMethod);
            return super.createRequest(uri, httpMethod);
        }
    };
    requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
    ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
    request.execute();
}
Also used : HttpRequest(org.springframework.http.HttpRequest) HttpRequestWrapper(org.springframework.http.client.support.HttpRequestWrapper) IOException(java.io.IOException) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 3 with HttpRequest

use of org.springframework.http.HttpRequest in project spring-framework by spring-projects.

the class UriComponentsBuilderTests method fromHttpRequestForwardedHeader.

// SPR-11856
@Test
public void fromHttpRequestForwardedHeader() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Forwarded", "proto=https; host=84.198.58.199");
    request.setScheme("http");
    request.setServerName("example.com");
    request.setRequestURI("/rest/mobile/users/1");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertEquals("https", result.getScheme());
    assertEquals("84.198.58.199", result.getHost());
    assertEquals("/rest/mobile/users/1", result.getPath());
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 4 with HttpRequest

use of org.springframework.http.HttpRequest in project spring-framework by spring-projects.

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedPortMultiValueHeader.

// SPR-12813
@Test
public void fromHttpRequestWithForwardedPortMultiValueHeader() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(9090);
    request.setRequestURI("/mvc-showcase");
    request.addHeader("X-Forwarded-Host", "a.example.org");
    request.addHeader("X-Forwarded-Port", "80,52022");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertEquals("http://a.example.org/mvc-showcase", result.toString());
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 5 with HttpRequest

use of org.springframework.http.HttpRequest in project spring-framework by spring-projects.

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedHostMultiValuedHeader.

// SPR-11140
@Test
public void fromHttpRequestWithForwardedHostMultiValuedHeader() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(-1);
    request.addHeader("X-Forwarded-Host", "a.example.org, b.example.org, c.example.org");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertEquals("a.example.org", result.getHost());
    assertEquals(-1, result.getPort());
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Aggregations

HttpRequest (org.springframework.http.HttpRequest)30 Test (org.junit.Test)26 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)21 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)19 IOException (java.io.IOException)6 URI (java.net.URI)6 ArrayList (java.util.ArrayList)4 HttpRequestWrapper (org.springframework.http.client.support.HttpRequestWrapper)4 HttpMethod (org.springframework.http.HttpMethod)3 ClientHttpRequestExecution (org.springframework.http.client.ClientHttpRequestExecution)3 ClientHttpRequestInterceptor (org.springframework.http.client.ClientHttpRequestInterceptor)3 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)2 UriComponents (org.springframework.web.util.UriComponents)2 Account (android.accounts.Account)1 List (java.util.List)1 Before (org.junit.Before)1 ResolvableType (org.springframework.core.ResolvableType)1 HttpHeaders (org.springframework.http.HttpHeaders)1 InvalidMediaTypeException (org.springframework.http.InvalidMediaTypeException)1 MediaType (org.springframework.http.MediaType)1