Search in sources :

Example 26 with HttpRequest

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

the class ServletUriComponentsBuilderTests method fromRequestWithForwardedHostAndPort.

// Most X-Forwarded-* tests in UriComponentsBuilderTests
@Test
public void fromRequestWithForwardedHostAndPort() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(80);
    request.setRequestURI("/mvc-showcase");
    request.addHeader("X-Forwarded-Proto", "https");
    request.addHeader("X-Forwarded-Host", "84.198.58.199");
    request.addHeader("X-Forwarded-Port", "443");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertEquals("https://84.198.58.199/mvc-showcase", result.toString());
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) UriComponents(org.springframework.web.util.UriComponents) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 27 with HttpRequest

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

the class InterceptingClientHttpRequestFactoryTests method changeHeaders.

@Test
public void changeHeaders() throws Exception {
    final String headerName = "Foo";
    final String headerValue = "Bar";
    final String otherValue = "Baz";
    ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            HttpRequestWrapper wrapper = new HttpRequestWrapper(request);
            wrapper.getHeaders().add(headerName, otherValue);
            return execution.execute(wrapper, body);
        }
    };
    requestMock = new RequestMock() {

        @Override
        public ClientHttpResponse execute() throws IOException {
            List<String> headerValues = getHeaders().get(headerName);
            assertEquals(2, headerValues.size());
            assertEquals(headerValue, headerValues.get(0));
            assertEquals(otherValue, headerValues.get(1));
            return super.execute();
        }
    };
    requestMock.getHeaders().add(headerName, headerValue);
    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) IOException(java.io.IOException) URI(java.net.URI) HttpRequestWrapper(org.springframework.http.client.support.HttpRequestWrapper) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 28 with HttpRequest

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

the class InterceptingClientHttpRequestFactoryTests method changeURI.

@Test
public void changeURI() throws Exception {
    final URI changedUri = new URI("http://example.com/2");
    ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {

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

                @Override
                public URI getURI() {
                    return changedUri;
                }
            }, body);
        }
    };
    requestFactoryMock = new RequestFactoryMock() {

        @Override
        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            assertEquals(changedUri, uri);
            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 29 with HttpRequest

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

the class InterceptingClientHttpRequestFactoryTests method noExecution.

@Test
public void noExecution() throws Exception {
    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
    interceptors.add(new ClientHttpRequestInterceptor() {

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            return responseMock;
        }
    });
    interceptors.add(new NoOpInterceptor());
    requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, interceptors);
    ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
    ClientHttpResponse response = request.execute();
    assertFalse(((NoOpInterceptor) interceptors.get(1)).invoked);
    assertFalse(requestMock.executed);
    assertSame(responseMock, response);
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URI(java.net.URI) Test(org.junit.Test)

Example 30 with HttpRequest

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

the class StreamingSimpleHttpRequestFactoryTests method interceptor.

// SPR-8809
@Test
public void interceptor() throws Exception {
    final String headerName = "MyHeader";
    final String headerValue = "MyValue";
    ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            request.getHeaders().add(headerName, headerValue);
            return execution.execute(request, body);
        }
    };
    InterceptingClientHttpRequestFactory factory = new InterceptingClientHttpRequestFactory(createRequestFactory(), Collections.singletonList(interceptor));
    ClientHttpResponse response = null;
    try {
        ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.GET);
        response = request.execute();
        assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode());
        HttpHeaders responseHeaders = response.getHeaders();
        assertEquals("Custom header invalid", headerValue, responseHeaders.getFirst(headerName));
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : HttpRequest(org.springframework.http.HttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) URI(java.net.URI) 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