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