Search in sources :

Example 1 with Http1HeadersImpl

use of io.esastack.commons.net.netty.http.Http1HeadersImpl in project esa-restclient by esastack.

the class RestResponseBaseImplTest method testHeaders.

@Test
void testHeaders() {
    RestRequestBase request = mock(RestRequestBase.class);
    HttpResponse response = mock(HttpResponse.class);
    RestClientOptions clientOptions = mock(RestClientOptions.class);
    RestResponse restResponse = new RestResponseBaseImpl(request, response, clientOptions);
    HttpHeaders headers = new Http1HeadersImpl();
    headers.add("aaa", "aaa");
    headers.add("bbb", "bbb");
    when(response.headers()).thenReturn(headers);
    then(restResponse.headers().size()).isEqualTo(2);
    then(restResponse.headers().get("aaa")).isEqualTo("aaa");
    then(restResponse.headers().get("bbb")).isEqualTo("bbb");
    headers.add("aaa", "aaa1");
    then(restResponse.headers().getAll("aaa").size()).isEqualTo(2);
    then(restResponse.headers().getAll("aaa").get(1)).isEqualTo("aaa1");
    when(response.headers()).thenReturn(null);
    then(restResponse.headers()).isEqualTo(null);
}
Also used : HttpHeaders(io.esastack.commons.net.http.HttpHeaders) Http1HeadersImpl(io.esastack.commons.net.netty.http.Http1HeadersImpl) HttpResponse(io.esastack.httpclient.core.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 2 with Http1HeadersImpl

use of io.esastack.commons.net.netty.http.Http1HeadersImpl in project esa-restclient by esastack.

the class RestResponseBaseImplTest method testBodyToEntity.

@Test
void testBodyToEntity() throws Exception {
    RestRequestBase request = mock(RestRequestBase.class);
    HttpResponse response = mock(HttpResponse.class);
    RestClientOptions clientOptions = mock(RestClientOptions.class);
    RestResponseBase restResponse = new RestResponseBaseImpl(request, response, clientOptions);
    HttpHeaders headers = new Http1HeadersImpl();
    when(response.headers()).thenReturn(headers);
    when(response.body()).thenReturn(Buffer.defaultAlloc().buffer("Hello".getBytes(StandardCharsets.UTF_8)));
    when(request.decoder()).thenReturn(new StringCodec());
    // decodeAdvices is empty
    when(clientOptions.unmodifiableDecodeAdvices()).thenReturn(Collections.emptyList());
    then(restResponse.bodyToEntity(String.class)).isEqualTo("Hello");
    // decodeAdvices is not empty
    when(clientOptions.unmodifiableDecodeAdvices()).thenReturn(Arrays.asList(context -> {
        String result = (String) context.next();
        return result + " Test1";
    }, context -> {
        String result = (String) context.next();
        return result + " Test2";
    }));
    then(restResponse.bodyToEntity(String.class)).isEqualTo("Hello Test2 Test1");
}
Also used : Arrays(java.util.Arrays) BDDAssertions.then(org.assertj.core.api.BDDAssertions.then) HttpResponse(io.esastack.httpclient.core.HttpResponse) Mockito.when(org.mockito.Mockito.when) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) HttpVersion(io.esastack.commons.net.http.HttpVersion) HttpHeaders(io.esastack.commons.net.http.HttpHeaders) HttpHeaderNames(io.esastack.commons.net.http.HttpHeaderNames) StringCodec(io.esastack.restclient.codec.impl.StringCodec) HttpStatus(io.esastack.commons.net.http.HttpStatus) Buffer(io.esastack.commons.net.buffer.Buffer) Http1HeadersImpl(io.esastack.commons.net.netty.http.Http1HeadersImpl) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) HttpHeaders(io.esastack.commons.net.http.HttpHeaders) StringCodec(io.esastack.restclient.codec.impl.StringCodec) Http1HeadersImpl(io.esastack.commons.net.netty.http.Http1HeadersImpl) HttpResponse(io.esastack.httpclient.core.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with Http1HeadersImpl

use of io.esastack.commons.net.netty.http.Http1HeadersImpl in project esa-restclient by esastack.

the class DefaultHandleTest method testAggregate.

@Test
void testAggregate() {
    final HttpRequest request = HttpClient.ofDefault().get("/abc");
    final ExecContext ctx = ExecContextUtil.newAs();
    final TimeoutHandle tHandle = new TimeoutHandle(NoopListener.INSTANCE);
    final CompletableFuture<HttpResponse> response = new CompletableFuture<>();
    final HandleImpl handle1 = new DefaultHandle(ByteBufAllocator.DEFAULT);
    final ResponseHandle nHandle1 = new ResponseHandle(handle1, request, ctx, tHandle, response);
    final HttpMessage message1 = new HttpMessageImpl(202, HttpVersion.HTTP_1_1, new Http1HeadersImpl());
    nHandle1.onMessage(message1);
    nHandle1.onEnd();
    then(handle1.body().readableBytes()).isEqualTo(0);
    then(handle1.headers().isEmpty()).isTrue();
    then(handle1.status()).isEqualTo(HttpStatus.ACCEPTED.code());
    final HandleImpl handle2 = new DefaultHandle(ByteBufAllocator.DEFAULT);
    final ResponseHandle nHandle2 = new ResponseHandle(handle2, request, ctx, tHandle, response);
    final HttpMessage message2 = new HttpMessageImpl(HttpStatus.FOUND.code(), HttpVersion.HTTP_1_1, new Http1HeadersImpl());
    message2.headers().add("A", "B");
    final byte[] data = "Hello World!".getBytes();
    nHandle2.onMessage(message2);
    nHandle2.onData(Buffer.defaultAlloc().buffer().writeBytes(data));
    nHandle2.onData(Buffer.defaultAlloc().buffer().writeBytes(data));
    nHandle2.onData(Buffer.defaultAlloc().buffer().writeBytes(data));
    final HttpHeaders trailers = new Http1HeadersImpl();
    trailers.add("D", "E");
    nHandle2.onTrailers(trailers);
    nHandle2.onEnd();
    then(handle2.body().readableBytes()).isEqualTo(data.length * 3);
    then(handle2.headers().get("A")).isEqualTo("B");
    then(handle2.headers().get("D")).isNull();
    then(handle2.status()).isEqualTo(302);
    then(handle2.trailers().get("A")).isNull();
    then(handle2.trailers().get("D")).isEqualTo("E");
}
Also used : HttpRequest(io.esastack.httpclient.core.HttpRequest) HttpHeaders(io.esastack.commons.net.http.HttpHeaders) HttpResponse(io.esastack.httpclient.core.HttpResponse) ExecContext(io.esastack.httpclient.core.exec.ExecContext) CompletableFuture(java.util.concurrent.CompletableFuture) Http1HeadersImpl(io.esastack.commons.net.netty.http.Http1HeadersImpl) HttpMessage(io.esastack.httpclient.core.HttpMessage) Test(org.junit.jupiter.api.Test)

Example 4 with Http1HeadersImpl

use of io.esastack.commons.net.netty.http.Http1HeadersImpl in project esa-restclient by esastack.

the class FilteringHandleTest method testOnXxxError.

private void testOnXxxError(HttpRequest request, ExecContext ctx, TimeoutHandle tHandle, CompletableFuture<HttpResponse> response, HandleImpl handle, RuntimeException ex) {
    final ResponseFilter[] filters = new ResponseFilter[2];
    final CountDownLatch latch = new CountDownLatch(1);
    filters[0] = (request1, response1, ctx1) -> {
        final CompletableFuture<Void> future = new CompletableFuture<>();
        new Thread(() -> {
            try {
                latch.await();
            } catch (InterruptedException e) {
            // Ignore
            }
            response1.headers().add("rspFilter1", "value1");
            future.complete(null);
        }).start();
        return future;
    };
    filters[1] = (request12, response12, ctx12) -> {
        response12.headers().add("rspFilter2", "value2");
        return Futures.completed();
    };
    final FilterContext fCtx = new FilterContext(ctx.ctx());
    final FilteringHandle nHandle = new FilteringHandle(handle, request, ctx, tHandle, response, filters, fCtx);
    final HttpHeaders headers = new Http1HeadersImpl();
    headers.add("A", "B");
    final HttpMessage message = new HttpMessageImpl(HttpStatus.OK.code(), HttpVersion.HTTP_1_1, headers);
    nHandle.onMessage(message);
    final byte[] data = new byte[1024 * 1024];
    ThreadLocalRandom.current().nextBytes(data);
    for (int i = 0; i < 1024; i++) {
        nHandle.onData(Buffer.defaultAlloc().buffer(Arrays.copyOfRange(data, 1024 * i, 1024 * (i + 1))));
    }
    final HttpHeaders trailers = new Http1HeadersImpl();
    trailers.add("X", 'Y');
    nHandle.onTrailers(trailers);
    nHandle.onEnd();
    // Ends the response filter now.
    latch.countDown();
    // Waits the response to complete
    assertThrows(ExecutionException.class, response::get);
    assertSame(ex, Futures.getCause(response));
}
Also used : HttpHeaders(io.esastack.commons.net.http.HttpHeaders) CountDownLatch(java.util.concurrent.CountDownLatch) CompletableFuture(java.util.concurrent.CompletableFuture) Http1HeadersImpl(io.esastack.commons.net.netty.http.Http1HeadersImpl) ResponseFilter(io.esastack.httpclient.core.filter.ResponseFilter) HttpMessage(io.esastack.httpclient.core.HttpMessage) FilterContext(io.esastack.httpclient.core.filter.FilterContext)

Example 5 with Http1HeadersImpl

use of io.esastack.commons.net.netty.http.Http1HeadersImpl in project esa-restclient by esastack.

the class FilteringHandleTest method testExceptionThrownByFilter.

@Test
void testExceptionThrownByFilter() {
    final HandleImpl handle = new DefaultHandle(ByteBufAllocator.DEFAULT);
    final HttpRequest request = client.get("/abc");
    final ExecContext ctx = ExecContextUtil.newAs();
    final TimeoutHandle tHandle = new TimeoutHandle(NoopListener.INSTANCE);
    final CompletableFuture<HttpResponse> response = new CompletableFuture<>();
    final ResponseFilter[] filters = new ResponseFilter[2];
    final CountDownLatch latch = new CountDownLatch(1);
    final RuntimeException ex = new RuntimeException();
    filters[0] = (request1, response1, ctx1) -> {
        throw ex;
    };
    filters[1] = (request12, response12, ctx12) -> {
        response12.headers().add("rspFilter2", "value2");
        return Futures.completed();
    };
    final FilterContext fCtx = new FilterContext(ctx.ctx());
    final FilteringHandle nHandle = new FilteringHandle(handle, request, ctx, tHandle, response, filters, fCtx);
    final HttpHeaders headers = new Http1HeadersImpl();
    headers.add("A", "B");
    final HttpMessage message = new HttpMessageImpl(HttpStatus.OK.code(), HttpVersion.HTTP_1_1, headers);
    nHandle.onMessage(message);
    final byte[] data = new byte[1024 * 1024];
    ThreadLocalRandom.current().nextBytes(data);
    for (int i = 0; i < 1024; i++) {
        nHandle.onData(Buffer.defaultAlloc().buffer(Arrays.copyOfRange(data, 1024 * i, 1024 * (i + 1))));
    }
    final HttpHeaders trailers = new Http1HeadersImpl();
    trailers.add("X", 'Y');
    nHandle.onTrailers(trailers);
    nHandle.onEnd();
    // Ends the response filter now.
    latch.countDown();
    // Waits the response to complete
    assertThrows(ExecutionException.class, response::get);
    assertSame(ex, Futures.getCause(response));
}
Also used : HttpRequest(io.esastack.httpclient.core.HttpRequest) HttpHeaders(io.esastack.commons.net.http.HttpHeaders) HttpResponse(io.esastack.httpclient.core.HttpResponse) ExecContext(io.esastack.httpclient.core.exec.ExecContext) CountDownLatch(java.util.concurrent.CountDownLatch) CompletableFuture(java.util.concurrent.CompletableFuture) Http1HeadersImpl(io.esastack.commons.net.netty.http.Http1HeadersImpl) ResponseFilter(io.esastack.httpclient.core.filter.ResponseFilter) HttpMessage(io.esastack.httpclient.core.HttpMessage) FilterContext(io.esastack.httpclient.core.filter.FilterContext) Test(org.junit.jupiter.api.Test)

Aggregations

Http1HeadersImpl (io.esastack.commons.net.netty.http.Http1HeadersImpl)36 Test (org.junit.jupiter.api.Test)24 HttpHeaders (io.esastack.commons.net.http.HttpHeaders)20 HttpResponse (io.esastack.httpclient.core.HttpResponse)14 ExecContext (io.esastack.httpclient.core.exec.ExecContext)12 HttpRequest (io.esastack.httpclient.core.HttpRequest)11 HttpMessage (io.esastack.httpclient.core.HttpMessage)9 ChannelFuture (io.netty.channel.ChannelFuture)8 CompletableFuture (java.util.concurrent.CompletableFuture)8 Buffer (io.esastack.commons.net.buffer.Buffer)6 ChannelPromise (io.netty.channel.ChannelPromise)6 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)6 IOException (java.io.IOException)6 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 HttpVersion (io.esastack.commons.net.http.HttpVersion)4 HttpClient (io.esastack.httpclient.core.HttpClient)4 NoopListener (io.esastack.httpclient.core.NoopListener)4 FilterContext (io.esastack.httpclient.core.filter.FilterContext)4 ResponseFilter (io.esastack.httpclient.core.filter.ResponseFilter)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4