Search in sources :

Example 21 with HttpRequest

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

the class InterceptingClientHttpRequestFactoryTests method changeURI.

@Test
public void changeURI() throws Exception {
    final URI changedUri = new URI("https://example.com/2");
    ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            return execution.execute(new HttpRequestDecorator(request) {

                @Override
                public URI getURI() {
                    return changedUri;
                }
            }, body);
        }
    };
    requestFactoryMock = new RequestFactoryMock() {

        @Override
        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            assertThat(uri).isEqualTo(changedUri);
            return super.createRequest(uri, httpMethod);
        }
    };
    requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
    ClientHttpRequest request = requestFactory.createRequest(new URI("https://example.com"), HttpMethod.GET);
    request.execute();
}
Also used : HttpRequest(cn.taketoday.http.HttpRequest) IOException(java.io.IOException) URI(java.net.URI) HttpMethod(cn.taketoday.http.HttpMethod) HttpRequestDecorator(cn.taketoday.http.client.support.HttpRequestDecorator) Test(org.junit.jupiter.api.Test)

Example 22 with HttpRequest

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

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 {
            HttpRequestDecorator wrapper = new HttpRequestDecorator(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);
            assertThat(headerValues.size()).isEqualTo(2);
            assertThat(headerValues.get(0)).isEqualTo(headerValue);
            assertThat(headerValues.get(1)).isEqualTo(otherValue);
            return super.execute();
        }
    };
    requestMock.getHeaders().add(headerName, headerValue);
    requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
    ClientHttpRequest request = requestFactory.createRequest(new URI("https://example.com"), HttpMethod.GET);
    request.execute();
}
Also used : HttpRequest(cn.taketoday.http.HttpRequest) IOException(java.io.IOException) URI(java.net.URI) HttpRequestDecorator(cn.taketoday.http.client.support.HttpRequestDecorator) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 23 with HttpRequest

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

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("https://example.com"), HttpMethod.GET);
    ClientHttpResponse response = request.execute();
    assertThat(((NoOpInterceptor) interceptors.get(1)).invoked).isFalse();
    assertThat(requestMock.executed).isFalse();
    assertThat(response).isSameAs(responseMock);
}
Also used : HttpRequest(cn.taketoday.http.HttpRequest) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 24 with HttpRequest

use of cn.taketoday.http.HttpRequest in project today-infrastructure 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 25 with HttpRequest

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

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 HttpRequestDecorator(request) {

                @Override
                public HttpMethod getMethod() {
                    return changedMethod;
                }
            }, body);
        }
    };
    requestFactoryMock = new RequestFactoryMock() {

        @Override
        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            assertThat(httpMethod).isEqualTo(changedMethod);
            return super.createRequest(uri, httpMethod);
        }
    };
    requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
    ClientHttpRequest request = requestFactory.createRequest(new URI("https://example.com"), HttpMethod.GET);
    request.execute();
}
Also used : HttpRequest(cn.taketoday.http.HttpRequest) IOException(java.io.IOException) URI(java.net.URI) HttpMethod(cn.taketoday.http.HttpMethod) HttpRequestDecorator(cn.taketoday.http.client.support.HttpRequestDecorator) 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