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);
}
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");
}
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");
}
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));
}
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));
}
Aggregations