Search in sources :

Example 6 with HttpMetaData

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

the class HttpObjectDecoderTest method chunkedNoContentNoTrailers.

private void chunkedNoContentNoTrailers(boolean crlf) {
    EmbeddedChannel channel = channel(crlf);
    String br = br(crlf);
    writeMsg(startLineForContent() + br + "Host: servicetalk.io" + br + "Connection: keep-alive" + br + "Transfer-Encoding: chunked" + br + br, channel);
    writeLastChunk(channel);
    HttpMetaData metaData = assertStartLineForContent(channel);
    assertStandardHeaders(metaData.headers());
    assertEmptyTrailers(channel);
    assertFalse(channel.finishAndReleaseAll());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Matchers.emptyString(org.hamcrest.Matchers.emptyString) Integer.toHexString(java.lang.Integer.toHexString) HttpMetaData(io.servicetalk.http.api.HttpMetaData)

Example 7 with HttpMetaData

use of io.servicetalk.http.api.HttpMetaData 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 8 with HttpMetaData

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

the class AbstractH2DuplexHandlerTest method emptyMessageWrittenAsSingleFrame.

@ParameterizedTest
@EnumSource(Variant.class)
void emptyMessageWrittenAsSingleFrame(Variant variant) {
    setUp(variant);
    HttpMetaData msg;
    switch(variant) {
        case CLIENT_HANDLER:
            msg = newRequest(GET, "/", HTTP_2_0, HEADERS_FACTORY.newHeaders(), DEFAULT_ALLOCATOR, HEADERS_FACTORY);
            break;
        case SERVER_HANDLER:
            msg = newResponse(HttpResponseStatus.OK, HTTP_2_0, HEADERS_FACTORY.newHeaders(), DEFAULT_ALLOCATOR, HEADERS_FACTORY);
            break;
        default:
            throw new IllegalStateException("Unexpected variant: " + variant);
    }
    channel.writeOutbound(msg);
    verify(closeHandler).protocolPayloadBeginOutbound(any());
    verify(closeHandler).protocolPayloadEndOutbound(any(), any());
    Http2HeadersFrame frame = channel.readOutbound();
    assertThat("Unexpected endStream flag value", frame.isEndStream(), is(true));
    assertThat("Unexpected outbound messages", channel.outboundMessages(), empty());
}
Also used : Http2HeadersFrame(io.netty.handler.codec.http2.Http2HeadersFrame) DefaultHttp2HeadersFrame(io.netty.handler.codec.http2.DefaultHttp2HeadersFrame) HttpMetaData(io.servicetalk.http.api.HttpMetaData) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with HttpMetaData

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

the class AbstractH2DuplexHandlerTest method responseWithContentLengthToHeadRequest.

private void responseWithContentLengthToHeadRequest(Variant variant, boolean endStream) {
    Assumptions.assumeTrue(variant == Variant.CLIENT_HANDLER, "Only relevant for the client-side");
    int contentLength = 1;
    // Send HEAD request
    channel.writeOutbound(newRequest(HEAD, "/", HTTP_2_0, HEADERS_FACTORY.newHeaders(), DEFAULT_ALLOCATOR, HEADERS_FACTORY), true);
    // Prepare server response with content-length header:
    Http2Headers headers = new DefaultHttp2Headers();
    headers.status(OK.codeAsText());
    headers.setInt(CONTENT_LENGTH, contentLength);
    channel.writeInbound(headersFrame(headers, endStream));
    HttpMetaData metaData = channel.readInbound();
    assertThat(metaData.headers().get(CONTENT_LENGTH), contentEqualTo(valueOf(contentLength)));
    if (endStream) {
        HttpHeaders trailers = channel.readInbound();
        if (trailers != null) {
            assertThat(trailers.isEmpty(), is(true));
        }
    } else {
        // No more items at this moment:
        assertThat(channel.inboundMessages(), is(empty()));
        channel.writeInbound(headersFrame(new DefaultHttp2Headers(), true));
        HttpHeaders trailers = channel.readInbound();
        assertThat(trailers.isEmpty(), is(true));
    }
    assertThat(channel.inboundMessages(), is(empty()));
}
Also used : HttpHeaders(io.servicetalk.http.api.HttpHeaders) EmptyHttpHeaders(io.servicetalk.http.api.EmptyHttpHeaders) Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) HttpMetaData(io.servicetalk.http.api.HttpMetaData)

Example 10 with HttpMetaData

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

the class AbstractH2DuplexHandlerTest method withContentLength.

private void withContentLength(Variant variant, boolean addTrailers) {
    variant.writeOutbound(channel);
    String content = "hello";
    Http2Headers headers = variant.setHeaders(new DefaultHttp2Headers());
    headers.setInt(CONTENT_LENGTH, content.length());
    channel.writeInbound(headersFrame(headers, false));
    HttpMetaData metaData = channel.readInbound();
    assertThat(metaData.headers().get(CONTENT_LENGTH), contentEqualTo(valueOf(content.length())));
    channel.writeInbound(new DefaultHttp2DataFrame(writeAscii(UnpooledByteBufAllocator.DEFAULT, content), !addTrailers));
    Buffer buffer = channel.readInbound();
    assertThat(buffer, is(equalTo(DEFAULT_ALLOCATOR.fromAscii(content))));
    if (addTrailers) {
        channel.writeInbound(headersFrame(new DefaultHttp2Headers().set("trailer", "value"), true));
    }
    HttpHeaders trailers = channel.readInbound();
    if (trailers != null) {
        assertThat(trailers.isEmpty(), is(!addTrailers));
    }
    assertThat(channel.inboundMessages(), is(empty()));
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpHeaders(io.servicetalk.http.api.HttpHeaders) EmptyHttpHeaders(io.servicetalk.http.api.EmptyHttpHeaders) DefaultHttp2DataFrame(io.netty.handler.codec.http2.DefaultHttp2DataFrame) Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) HttpMetaData(io.servicetalk.http.api.HttpMetaData)

Aggregations

HttpMetaData (io.servicetalk.http.api.HttpMetaData)22 Test (org.junit.jupiter.api.Test)8 Integer.toHexString (java.lang.Integer.toHexString)7 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)6 Buffer (io.servicetalk.buffer.api.Buffer)6 HttpHeaders (io.servicetalk.http.api.HttpHeaders)6 Matchers.emptyString (org.hamcrest.Matchers.emptyString)6 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)4 Http2Headers (io.netty.handler.codec.http2.Http2Headers)4 EmptyHttpHeaders (io.servicetalk.http.api.EmptyHttpHeaders)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 EnumSource (org.junit.jupiter.params.provider.EnumSource)3 DefaultHttp2DataFrame (io.netty.handler.codec.http2.DefaultHttp2DataFrame)2 DefaultHttp2HeadersFrame (io.netty.handler.codec.http2.DefaultHttp2HeadersFrame)2 Http2HeadersFrame (io.netty.handler.codec.http2.Http2HeadersFrame)2 ByteBuf (io.netty.buffer.ByteBuf)1 Unpooled.directBuffer (io.netty.buffer.Unpooled.directBuffer)1 Unpooled.unreleasableBuffer (io.netty.buffer.Unpooled.unreleasableBuffer)1 Unpooled.wrappedBuffer (io.netty.buffer.Unpooled.wrappedBuffer)1 DecoderException (io.netty.handler.codec.DecoderException)1