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