Search in sources :

Example 96 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class AbsoluteAddressHttpRequesterFilterTest method shouldNotAddAuthorityToAbsoluteFormRequestTarget.

@Test
void shouldNotAddAuthorityToAbsoluteFormRequestTarget() throws Exception {
    request.requestTarget("https://otherhost:443/otherpath?otherQuery");
    filter.request(request).toFuture().get();
    verify(delegate).request(requestCapture.capture());
    final StreamingHttpRequest capturedRequest = requestCapture.getValue();
    MatcherAssert.assertThat(capturedRequest.requestTarget(), is("https://otherhost:443/otherpath?otherQuery"));
}
Also used : StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Test(org.junit.jupiter.api.Test)

Example 97 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class AbstractConditionalHttpFilterTest method testFilter.

private void testFilter(boolean expectAccepted) throws Exception {
    final StreamingHttpRequest req = REQ_RES_FACTORY.get(expectAccepted ? "/accept" : "/reject");
    final StreamingHttpResponse res = sendTestRequest(req).toFuture().get();
    assertThat(res.headers().get(FILTERED_HEADER), is(Boolean.toString(expectAccepted)));
}
Also used : StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Example 98 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class AbstractEchoServerBasedHttpRequesterTest method makeRequestValidateResponseAndClose.

static void makeRequestValidateResponseAndClose(StreamingHttpRequester requester) throws ExecutionException, InterruptedException {
    try {
        StreamingHttpRequest request = requester.get("/request?foo=bar&foo=baz").payloadBody(from(DEFAULT_ALLOCATOR.fromAscii("Testing123")));
        request.headers().set(HOST, "mock.servicetalk.io");
        StreamingHttpResponse resp = awaitIndefinitelyNonNull(requester.request(request).retryWhen(retryWithExponentialBackoffFullJitter(10, t -> true, ofMillis(100), ofDays(10), CTX.executor())));
        assertThat(resp.status(), equalTo(OK));
        Single<String> respBody = resp.payloadBody().collect(StringBuilder::new, (sb, buf) -> {
            sb.append(buf.toString(UTF_8));
            return sb;
        }).map(StringBuilder::toString);
        HttpHeaders headers = resp.headers();
        assertThat(headers.get("test-req-method"), hasToString(GET.toString()));
        assertThat(headers.get("test-req-target"), hasToString("/request?foo=bar&foo=baz"));
        assertThat(headers.get("test-req-header-host"), hasToString("mock.servicetalk.io"));
        assertThat(headers.get("test-req-header-transfer-encoding"), equalTo(CHUNKED));
        assertThat(respBody.toFuture().get(), equalTo("Testing123"));
    } finally {
        requester.closeAsync().toFuture().get();
    }
}
Also used : Matchers.hasToString(org.hamcrest.Matchers.hasToString) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) HttpHeaders(io.servicetalk.http.api.HttpHeaders) StreamingHttpRequester(io.servicetalk.http.api.StreamingHttpRequester) HttpServers.forAddress(io.servicetalk.http.netty.HttpServers.forAddress) AfterAll(org.junit.jupiter.api.AfterAll) HttpServiceContext(io.servicetalk.http.api.HttpServiceContext) TestInstance(org.junit.jupiter.api.TestInstance) BeforeAll(org.junit.jupiter.api.BeforeAll) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HOST(io.servicetalk.http.api.HttpHeaderNames.HOST) BlockingTestUtils.awaitIndefinitelyNonNull(io.servicetalk.concurrent.api.BlockingTestUtils.awaitIndefinitelyNonNull) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) HttpExecutionStrategies.offloadNone(io.servicetalk.http.api.HttpExecutionStrategies.offloadNone) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) CHUNKED(io.servicetalk.http.api.HttpHeaderValues.CHUNKED) Duration.ofDays(java.time.Duration.ofDays) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) RetryStrategies.retryWithExponentialBackoffFullJitter(io.servicetalk.concurrent.api.RetryStrategies.retryWithExponentialBackoffFullJitter) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Single(io.servicetalk.concurrent.api.Single) ExecutionContextExtension(io.servicetalk.transport.netty.internal.ExecutionContextExtension) OK(io.servicetalk.http.api.HttpResponseStatus.OK) GET(io.servicetalk.http.api.HttpRequestMethod.GET) ExecutionException(java.util.concurrent.ExecutionException) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) Duration.ofMillis(java.time.Duration.ofMillis) HttpHeaders(io.servicetalk.http.api.HttpHeaders) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Matchers.hasToString(org.hamcrest.Matchers.hasToString) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Example 99 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class AbstractH2DuplexHandlerTest method noDataFramesForEmptyBuffers.

@ParameterizedTest
@EnumSource(Variant.class)
void noDataFramesForEmptyBuffers(Variant variant) {
    setUp(variant);
    Buffer[] payload = { EMPTY_BUFFER, DEFAULT_ALLOCATOR.fromAscii("data"), EMPTY_BUFFER };
    HttpMetaData msg;
    switch(variant) {
        case CLIENT_HANDLER:
            StreamingHttpRequest request = newRequest(GET, "/", HTTP_2_0, HEADERS_FACTORY.newHeaders(), DEFAULT_ALLOCATOR, HEADERS_FACTORY);
            request.payloadBody(from(payload));
            msg = request;
            break;
        case SERVER_HANDLER:
            StreamingHttpResponse response = newResponse(HttpResponseStatus.OK, HTTP_2_0, HEADERS_FACTORY.newHeaders(), DEFAULT_ALLOCATOR, HEADERS_FACTORY);
            response.payloadBody(from(payload));
            msg = response;
            break;
        default:
            throw new IllegalStateException("Unexpected variant: " + variant);
    }
    channel.writeOutbound(msg);
    verify(closeHandler).protocolPayloadBeginOutbound(any());
    verify(closeHandler, never()).protocolPayloadEndOutbound(any(), any());
    for (Buffer buffer : payload) {
        channel.writeOutbound(buffer);
    }
    verify(closeHandler, never()).protocolPayloadEndOutbound(any(), any());
    channel.writeOutbound(EmptyHttpHeaders.INSTANCE);
    verify(closeHandler).protocolPayloadEndOutbound(any(), any());
    Http2HeadersFrame headersFrame = channel.readOutbound();
    assertThat("Unexpected endStream flag value at headers frame", headersFrame.isEndStream(), is(false));
    assertEmptyDataFrame();
    Http2DataFrame dataFrame = channel.readOutbound();
    assertThat("Unexpected data", dataFrame.content().toString(US_ASCII), is("data"));
    assertThat("Unexpected endStream flag value at data frame", dataFrame.isEndStream(), is(false));
    assertEmptyDataFrame();
    dataFrame = channel.readOutbound();
    assertThat("Unexpected endStream flag value at last frame", dataFrame.isEndStream(), is(true));
    assertThat("Unexpected outbound messages", channel.outboundMessages(), empty());
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) DefaultHttp2DataFrame(io.netty.handler.codec.http2.DefaultHttp2DataFrame) Http2DataFrame(io.netty.handler.codec.http2.Http2DataFrame) Http2HeadersFrame(io.netty.handler.codec.http2.Http2HeadersFrame) DefaultHttp2HeadersFrame(io.netty.handler.codec.http2.DefaultHttp2HeadersFrame) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) HttpMetaData(io.servicetalk.http.api.HttpMetaData) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 100 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class AbstractHttpConnectionTest method requestShouldWriteFlatStreamToConnectionAndReadFlatStreamSplicedIntoResponseAndPayload.

@SuppressWarnings("unchecked")
@Test
void requestShouldWriteFlatStreamToConnectionAndReadFlatStreamSplicedIntoResponseAndPayload() throws Exception {
    Buffer chunk1 = allocator.fromAscii("test");
    Buffer chunk2 = allocator.fromAscii("payload");
    Buffer chunk3 = allocator.fromAscii("payload");
    HttpHeaders trailers = headersFactory.newEmptyTrailers();
    HttpHeaders headers = headersFactory.newHeaders();
    headers.add(TRANSFER_ENCODING, CHUNKED);
    StreamingHttpRequest req = newTransportRequest(GET, "/foo", HTTP_1_1, headers, allocator, from(chunk1, chunk2, chunk3, trailers), false, headersFactory);
    HttpResponseMetaData respMeta = newResponseMetaData(HTTP_1_1, OK, INSTANCE.newHeaders().add(CONTENT_TYPE, TEXT_PLAIN));
    Publisher<Object> respFlat = from(respMeta, chunk1, chunk2, chunk3, trailers);
    ArgumentCaptor<Publisher<Object>> reqFlatCaptor = ArgumentCaptor.forClass(Publisher.class);
    when(reqResp.apply(reqFlatCaptor.capture())).thenReturn(respFlat);
    Single<StreamingHttpResponse> responseSingle = http.request(req);
    StreamingHttpResponse resp = awaitIndefinitelyNonNull(responseSingle);
    assertThat(reqFlatCaptor.getValue().toFuture().get(), contains(req, chunk1, chunk2, chunk3, trailers));
    assertThat(resp.status(), equalTo(OK));
    assertThat(resp.version(), equalTo(HTTP_1_1));
    assertThat(resp.headers().get(CONTENT_TYPE), equalTo(TEXT_PLAIN));
    assertThat(resp.payloadBody().toFuture().get(), contains(chunk1, chunk2, chunk3));
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpHeaders(io.servicetalk.http.api.HttpHeaders) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Publisher(io.servicetalk.concurrent.api.Publisher) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)169 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)84 Test (org.junit.jupiter.api.Test)82 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)66 MethodSource (org.junit.jupiter.params.provider.MethodSource)47 Single (io.servicetalk.concurrent.api.Single)35 Buffer (io.servicetalk.buffer.api.Buffer)33 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)32 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)25 HttpServiceContext (io.servicetalk.http.api.HttpServiceContext)23 StreamingHttpResponseFactory (io.servicetalk.http.api.StreamingHttpResponseFactory)23 Nullable (javax.annotation.Nullable)22 InetSocketAddress (java.net.InetSocketAddress)21 OK (io.servicetalk.http.api.HttpResponseStatus.OK)20 StreamingHttpServiceFilter (io.servicetalk.http.api.StreamingHttpServiceFilter)19 CountDownLatch (java.util.concurrent.CountDownLatch)19 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)18 HttpHeaders (io.servicetalk.http.api.HttpHeaders)17 ServerContext (io.servicetalk.transport.api.ServerContext)17 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)17