Search in sources :

Example 11 with HttpResponseMetaData

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

the class HttpResponseEncoderTest method withContentLengthAndChunked.

@Test
void withContentLengthAndChunked() {
    EmbeddedChannel channel = newEmbeddedChannel();
    byte[] content = new byte[128];
    ThreadLocalRandom.current().nextBytes(content);
    Buffer buffer = DEFAULT_ALLOCATOR.wrap(content);
    HttpResponseMetaData response = newResponseMetaData(HTTP_1_1, OK, INSTANCE.newHeaders());
    response.headers().add(CONNECTION, KEEP_ALIVE).add(SERVER, "unit-test").add(CONTENT_LENGTH, valueOf(content.length)).add(TRANSFER_ENCODING, CHUNKED);
    channel.writeOutbound(response);
    channel.writeOutbound(buffer.duplicate());
    channel.writeOutbound(EmptyHttpHeaders.INSTANCE);
    String metaData = verifyHttpResponse(channel, buffer, TransferEncoding.Chunked, false);
    assertFalse(metaData.contains(CONTENT_LENGTH), "Unexpected content-length header in meta-data while chunked encoding is used: " + metaData);
    assertFalse(channel.finishAndReleaseAll());
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Integer.toHexString(java.lang.Integer.toHexString) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with HttpResponseMetaData

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

the class HttpResponseEncoderTest method variableNoTrailers.

@Test
void variableNoTrailers() {
    EmbeddedChannel channel = newEmbeddedChannel();
    byte[] content = new byte[128];
    ThreadLocalRandom.current().nextBytes(content);
    Buffer buffer = DEFAULT_ALLOCATOR.wrap(content);
    HttpResponseMetaData response = newResponseMetaData(HTTP_1_1, OK, INSTANCE.newHeaders());
    response.headers().add(CONNECTION, KEEP_ALIVE).add(SERVER, "unit-test");
    channel.writeOutbound(response);
    channel.writeOutbound(buffer.duplicate());
    channel.writeOutbound(EmptyHttpHeaders.INSTANCE);
    verifyHttpResponse(channel, buffer, TransferEncoding.Variable, false);
    assertFalse(channel.finishAndReleaseAll());
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with HttpResponseMetaData

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

the class HttpResponseEncoderTest method contentLengthNoTrailersHeaderWhiteSpaceThrowByDefault.

@Test
void contentLengthNoTrailersHeaderWhiteSpaceThrowByDefault() {
    EmbeddedChannel channel = newEmbeddedChannel();
    HttpResponseMetaData response = newResponseMetaData(HTTP_1_1, OK, INSTANCE.newHeaders());
    assertThrows(IllegalArgumentException.class, () -> response.addHeader(" " + CONNECTION, KEEP_ALIVE));
    assertFalse(channel.finishAndReleaseAll());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with HttpResponseMetaData

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

the class HttpResponseEncoderTest method contentLengthNoTrailers.

@Test
void contentLengthNoTrailers() {
    EmbeddedChannel channel = newEmbeddedChannel();
    byte[] content = new byte[128];
    ThreadLocalRandom.current().nextBytes(content);
    Buffer buffer = DEFAULT_ALLOCATOR.wrap(content);
    HttpResponseMetaData response = newResponseMetaData(HTTP_1_1, OK, INSTANCE.newHeaders());
    response.headers().add(CONNECTION, KEEP_ALIVE).add(SERVER, "unit-test").add(CONTENT_LENGTH, valueOf(content.length));
    channel.writeOutbound(response);
    channel.writeOutbound(buffer.duplicate());
    channel.writeOutbound(EmptyHttpHeaders.INSTANCE);
    verifyHttpResponse(channel, buffer, TransferEncoding.ContentLength, false);
    consumeEmptyBufferFromTrailers(channel);
    assertFalse(channel.finishAndReleaseAll());
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 15 with HttpResponseMetaData

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

the class HttpResponseEncoderTest method variableWithTrailers.

@ParameterizedTest
@ValueSource(booleans = { true, false })
void variableWithTrailers(boolean emptyTrailers) {
    EmbeddedChannel channel = newEmbeddedChannel();
    byte[] content = new byte[128];
    ThreadLocalRandom.current().nextBytes(content);
    Buffer buffer = DEFAULT_ALLOCATOR.wrap(content);
    HttpHeaders trailers = DefaultHttpHeadersFactory.INSTANCE.newTrailers();
    if (!emptyTrailers) {
        trailers.add("TrailerStatus", "good");
    }
    HttpResponseMetaData response = newResponseMetaData(HTTP_1_1, OK, INSTANCE.newHeaders());
    response.headers().add(CONNECTION, KEEP_ALIVE).add(SERVER, "unit-test");
    channel.writeOutbound(response);
    channel.writeOutbound(buffer.duplicate());
    if (!emptyTrailers) {
        assertThrows(IOException.class, () -> channel.writeOutbound(trailers));
    } else {
        channel.writeOutbound(trailers);
        verifyHttpResponse(channel, buffer, TransferEncoding.Variable, false);
    }
    // The trailers will just not be encoded if the transfer encoding is not set correctly.
    assertNotEquals(emptyTrailers, channel.finishAndReleaseAll());
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpHeaders(io.servicetalk.http.api.HttpHeaders) EmptyHttpHeaders(io.servicetalk.http.api.EmptyHttpHeaders) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

HttpResponseMetaData (io.servicetalk.http.api.HttpResponseMetaData)17 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)11 Buffer (io.servicetalk.buffer.api.Buffer)11 Test (org.junit.jupiter.api.Test)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 HttpHeaders (io.servicetalk.http.api.HttpHeaders)7 EmptyHttpHeaders (io.servicetalk.http.api.EmptyHttpHeaders)3 Publisher (io.servicetalk.concurrent.api.Publisher)2 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)2 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)2 Integer.toHexString (java.lang.Integer.toHexString)2 ValueSource (org.junit.jupiter.params.provider.ValueSource)2 ByteBuf (io.netty.buffer.ByteBuf)1 HT (io.netty.handler.codec.http.HttpConstants.HT)1 Http2Headers (io.netty.handler.codec.http2.Http2Headers)1 DefaultHttpHeadersFactory (io.servicetalk.http.api.DefaultHttpHeadersFactory)1 GET (io.servicetalk.http.api.HttpRequestMethod.GET)1 Benchmark (org.openjdk.jmh.annotations.Benchmark)1