use of cn.taketoday.http.HttpRequest in project today-infrastructure by TAKETODAY.
the class UriComponentsBuilderTests method fromHttpRequestWithForwardedHostIncludingPort.
@Test
// SPR-10701
void fromHttpRequestWithForwardedHostIncludingPort() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setScheme("http");
request.setServerName("localhost");
request.setServerPort(-1);
request.setRequestURI("/mvc-showcase");
request.addHeader("X-Forwarded-Host", "webtest.foo.bar.com:443");
HttpRequest httpRequest = new ServletServerHttpRequest(request);
UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
assertThat(result.getHost()).isEqualTo("webtest.foo.bar.com");
assertThat(result.getPort()).isEqualTo(443);
}
use of cn.taketoday.http.HttpRequest in project today-infrastructure by TAKETODAY.
the class UriComponentsBuilderTests method fromHttpRequestWithForwardedHostAndPort.
@Test
// SPR-11855
void fromHttpRequestWithForwardedHostAndPort() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setScheme("http");
request.setServerName("localhost");
request.setServerPort(8080);
request.addHeader("X-Forwarded-Host", "foobarhost");
request.addHeader("X-Forwarded-Port", "9090");
HttpRequest httpRequest = new ServletServerHttpRequest(request);
UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
assertThat(result.getHost()).isEqualTo("foobarhost");
assertThat(result.getPort()).isEqualTo(9090);
}
use of cn.taketoday.http.HttpRequest in project today-infrastructure by TAKETODAY.
the class UriComponentsBuilderTests method fromHttpRequestMultipleForwardedHeader.
@Test
void fromHttpRequestMultipleForwardedHeader() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Forwarded", "host=84.198.58.199;proto=https");
request.addHeader("Forwarded", "proto=ftp; host=1.2.3.4");
request.setScheme("http");
request.setServerName("example.com");
request.setRequestURI("/rest/mobile/users/1");
HttpRequest httpRequest = new ServletServerHttpRequest(request);
UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
assertThat(result.getScheme()).isEqualTo("https");
assertThat(result.getHost()).isEqualTo("84.198.58.199");
assertThat(result.getPath()).isEqualTo("/rest/mobile/users/1");
}
use of cn.taketoday.http.HttpRequest in project today-infrastructure by TAKETODAY.
the class DefaultClientResponse method createException.
@Override
public Mono<WebClientResponseException> createException() {
return bodyToMono(byte[].class).defaultIfEmpty(EMPTY).onErrorReturn(ex -> !(ex instanceof Error), EMPTY).map(bodyBytes -> {
HttpRequest request = this.requestSupplier.get();
Charset charset = headers().contentType().map(MimeType::getCharset).orElse(null);
int statusCode = rawStatusCode();
HttpStatus httpStatus = HttpStatus.resolve(statusCode);
if (httpStatus != null) {
return WebClientResponseException.create(statusCode, httpStatus.getReasonPhrase(), headers().asHttpHeaders(), bodyBytes, charset, request);
} else {
return new UnknownHttpStatusCodeException(statusCode, headers().asHttpHeaders(), bodyBytes, charset, request);
}
});
}
use of cn.taketoday.http.HttpRequest in project today-infrastructure by TAKETODAY.
the class DefaultClientResponseBuilderTests method mutate.
@Test
public void mutate() {
Flux<DataBuffer> otherBody = Flux.just("foo", "bar").map(s -> s.getBytes(StandardCharsets.UTF_8)).map(DefaultDataBufferFactory.sharedInstance::wrap);
HttpRequest mockClientHttpRequest = new MockClientHttpRequest(HttpMethod.GET, "/path");
MockClientHttpResponse httpResponse = new MockClientHttpResponse(HttpStatus.OK);
httpResponse.getHeaders().add("foo", "bar");
httpResponse.getHeaders().add("bar", "baz");
httpResponse.getCookies().add("baz", ResponseCookie.from("baz", "qux").build());
httpResponse.setBody(otherBody);
DefaultClientResponse otherResponse = new DefaultClientResponse(httpResponse, ExchangeStrategies.withDefaults(), "my-prefix", "", () -> mockClientHttpRequest);
ClientResponse result = otherResponse.mutate().statusCode(HttpStatus.BAD_REQUEST).headers(headers -> headers.set("foo", "baar")).cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build())).build();
assertThat(result.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(result.headers().asHttpHeaders().size()).isEqualTo(3);
assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("baar");
assertThat(result.headers().asHttpHeaders().getFirst("bar")).isEqualTo("baz");
assertThat(result.cookies().size()).isEqualTo(1);
assertThat(result.cookies().getFirst("baz").getValue()).isEqualTo("quux");
assertThat(result.logPrefix()).isEqualTo("my-prefix");
StepVerifier.create(result.bodyToFlux(String.class)).expectNext("foobar").verifyComplete();
}
Aggregations