Search in sources :

Example 26 with HttpRequest

use of cn.taketoday.http.HttpRequest in project today-framework by TAKETODAY.

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("https://example.com"), HttpMethod.GET);
    request.execute();
    assertThat(Arrays.equals(changedBody, requestMock.body.toByteArray())).isTrue();
}
Also used : HttpRequest(cn.taketoday.http.HttpRequest) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 27 with HttpRequest

use of cn.taketoday.http.HttpRequest in project today-framework 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) StandardCharsets(java.nio.charset.StandardCharsets) ResponseCookie(cn.taketoday.http.ResponseCookie) Test(org.junit.jupiter.api.Test) HttpMethod(cn.taketoday.http.HttpMethod) Flux(reactor.core.publisher.Flux) HttpHeaders(cn.taketoday.http.HttpHeaders) HttpStatusCode(cn.taketoday.http.HttpStatusCode) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) 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)

Example 28 with HttpRequest

use of cn.taketoday.http.HttpRequest in project today-framework by TAKETODAY.

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedHostMultiValuedHeader.

@Test
// SPR-11140
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();
    assertThat(result.getHost()).isEqualTo("a.example.org");
    assertThat(result.getPort()).isEqualTo(-1);
}
Also used : ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) HttpRequest(cn.taketoday.http.HttpRequest) ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 29 with HttpRequest

use of cn.taketoday.http.HttpRequest in project today-framework by TAKETODAY.

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedHostWithForwardedScheme.

@Test
void fromHttpRequestWithForwardedHostWithForwardedScheme() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(10080);
    request.addHeader("X-Forwarded-Host", "example.org");
    request.addHeader("X-Forwarded-Proto", "https");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertThat(result.getHost()).isEqualTo("example.org");
    assertThat(result.getScheme()).isEqualTo("https");
    assertThat(result.getPort()).isEqualTo(-1);
}
Also used : ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) HttpRequest(cn.taketoday.http.HttpRequest) ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 30 with HttpRequest

use of cn.taketoday.http.HttpRequest in project today-framework by TAKETODAY.

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedIPv4Host.

@Test
// SPR-14761
void fromHttpRequestWithForwardedIPv4Host() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("https");
    request.setServerName("localhost");
    request.setServerPort(-1);
    request.setRequestURI("/mvc-showcase");
    request.addHeader("Forwarded", "host=192.168.0.1");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertThat(result.toString()).isEqualTo("https://192.168.0.1/mvc-showcase");
}
Also used : ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) HttpRequest(cn.taketoday.http.HttpRequest) ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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