Search in sources :

Example 16 with HttpRequest

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);
}
Also used : ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) HttpRequest(cn.taketoday.http.HttpRequest) ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 17 with HttpRequest

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);
}
Also used : ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) HttpRequest(cn.taketoday.http.HttpRequest) ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 18 with HttpRequest

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");
}
Also used : ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) HttpRequest(cn.taketoday.http.HttpRequest) ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with HttpRequest

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);
        }
    });
}
Also used : Supplier(java.util.function.Supplier) ResponseCookie(cn.taketoday.http.ResponseCookie) OptionalLong(java.util.OptionalLong) HttpHeaders(cn.taketoday.http.HttpHeaders) ClientHttpResponse(cn.taketoday.http.client.reactive.ClientHttpResponse) Charset(java.nio.charset.Charset) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) Hints(cn.taketoday.core.codec.Hints) Map(java.util.Map) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MultiValueMap(cn.taketoday.core.MultiValueMap) TypeReference(cn.taketoday.core.TypeReference) BodyExtractor(cn.taketoday.web.reactive.function.BodyExtractor) HttpRequest(cn.taketoday.http.HttpRequest) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) Mono(reactor.core.publisher.Mono) ResponseEntity(cn.taketoday.http.ResponseEntity) BodyExtractors(cn.taketoday.web.reactive.function.BodyExtractors) Flux(reactor.core.publisher.Flux) List(java.util.List) Optional(java.util.Optional) HttpStatus(cn.taketoday.http.HttpStatus) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) MimeType(cn.taketoday.util.MimeType) HttpRequest(cn.taketoday.http.HttpRequest) HttpStatus(cn.taketoday.http.HttpStatus) Charset(java.nio.charset.Charset)

Example 20 with HttpRequest

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();
}
Also used : StepVerifier(reactor.test.StepVerifier) MockClientHttpResponse(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResponseCookie(cn.taketoday.http.ResponseCookie) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) HttpMethod(cn.taketoday.http.HttpMethod) Flux(reactor.core.publisher.Flux) HttpHeaders(cn.taketoday.http.HttpHeaders) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) HttpStatus(cn.taketoday.http.HttpStatus) HttpRequest(cn.taketoday.http.HttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) HttpRequest(cn.taketoday.http.HttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MockClientHttpResponse(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpRequest (cn.taketoday.http.HttpRequest)69 Test (org.junit.jupiter.api.Test)64 ServletServerHttpRequest (cn.taketoday.http.server.ServletServerHttpRequest)56 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)56 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)27 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)27 URI (java.net.URI)10 IOException (java.io.IOException)8 HttpMethod (cn.taketoday.http.HttpMethod)6 HttpRequestDecorator (cn.taketoday.http.client.support.HttpRequestDecorator)6 ArrayList (java.util.ArrayList)4 ValueSource (org.junit.jupiter.params.provider.ValueSource)4 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)3 HttpHeaders (cn.taketoday.http.HttpHeaders)3 HttpStatus (cn.taketoday.http.HttpStatus)3 ResponseCookie (cn.taketoday.http.ResponseCookie)3 List (java.util.List)3 Flux (reactor.core.publisher.Flux)3 DefaultDataBufferFactory (cn.taketoday.core.io.buffer.DefaultDataBufferFactory)2 MockClientHttpRequest (cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest)2