Search in sources :

Example 21 with HttpMetaData

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

the class HttpResponseDecoderTest method variableWithContent.

@Test
void variableWithContent() {
    int contentLength = 128;
    writeMsg("HTTP/1.1 200 OK" + "\r\n" + "Host: servicetalk.io" + "\r\n" + "Connection: keep-alive" + "\r\n" + "\r\n");
    writeContent(contentLength);
    // For a response, the variable length content is considered "complete" when the channel is closed.
    channel.close();
    HttpMetaData metaData = assertStartLineForContent();
    assertStandardHeaders(metaData.headers());
    assertThat(metaData.headers().get(CONTENT_LENGTH), is(nullValue()));
    Buffer chunk = channel().readInbound();
    assertThat(chunk.readableBytes(), is(contentLength));
    assertEmptyTrailers(channel());
    assertFalse(channel.finishAndReleaseAll());
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpMetaData(io.servicetalk.http.api.HttpMetaData) Test(org.junit.jupiter.api.Test)

Example 22 with HttpMetaData

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

the class HttpResponseDecoderTest method variableWithChunkedContentAndTrailers.

@Test
void variableWithChunkedContentAndTrailers() {
    int chunkSize = 128;
    writeMsg("HTTP/1.1 200 OK" + "\r\n" + "Host: servicetalk.io" + "\r\n" + "Connection: keep-alive" + "\r\n" + "\r\n");
    // Note that trailers are only allowed when chunked encoding is used. So the trailers in this case are
    // considered part of the payload (even the \r\n), and the response is terminated when the channel is closed.
    // https://tools.ietf.org/html/rfc7230.html#section-4.1
    writeChunk(chunkSize);
    writeChunk(0);
    String trailersPart = "TrailerStatus: good" + "\r\n";
    writeMsg(trailersPart);
    writeMsg("\r\n");
    // For a response, the variable length content is considered "complete" when the channel is closed.
    channel.close();
    // In this case, content is parsed without taking "chunked" encoding into account.
    int expectedContentLength = 2 + /* chunk-size */
    2 + /* CRLF */
    chunkSize + 2 + /* CRLF */
    3 + /* last-chunk */
    trailersPart.length() + 2;
    HttpMetaData metaData = assertStartLineForContent();
    assertStandardHeaders(metaData.headers());
    assertThat(isTransferEncodingChunked(metaData.headers()), is(false));
    HttpHeaders trailers = assertPayloadSize(expectedContentLength);
    assertThat("Trailers are not empty", trailers, nullValue());
    assertFalse(channel.finishAndReleaseAll());
}
Also used : HttpHeaders(io.servicetalk.http.api.HttpHeaders) Integer.toHexString(java.lang.Integer.toHexString) HttpMetaData(io.servicetalk.http.api.HttpMetaData) Test(org.junit.jupiter.api.Test)

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