Search in sources :

Example 16 with HttpResponseMetaData

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

the class HttpResponseEncoderTest method contentLengthNoTrailersHeaderWhiteSpaceEncodedWithValidationOff.

@Test
void contentLengthNoTrailersHeaderWhiteSpaceEncodedWithValidationOff() {
    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, new DefaultHttpHeadersFactory(false, false, false).newHeaders());
    response.headers().add(" " + CONNECTION + " ", " " + KEEP_ALIVE).add("  " + SERVER + "   ", "    unit-test   ").add(CONTENT_LENGTH, valueOf(content.length));
    channel.writeOutbound(response);
    channel.writeOutbound(buffer.duplicate());
    ByteBuf byteBuf = channel.readOutbound();
    String actualMetaData = byteBuf.toString(US_ASCII);
    byteBuf.release();
    assertTrue(actualMetaData.contains("HTTP/1.1 200 OK" + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
    assertTrue(actualMetaData.contains(" " + CONNECTION + " :  " + KEEP_ALIVE + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
    assertTrue(actualMetaData.contains("  " + SERVER + "   :     unit-test   " + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
    assertTrue(actualMetaData.contains(CONTENT_LENGTH + ": " + buffer.readableBytes() + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
    assertTrue(actualMetaData.endsWith("\r\n" + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
    byteBuf = channel.readOutbound();
    assertEquals(buffer.toNioBuffer(), byteBuf.nioBuffer());
    byteBuf.release();
    assertFalse(channel.finishAndReleaseAll());
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Integer.toHexString(java.lang.Integer.toHexString) ByteBuf(io.netty.buffer.ByteBuf) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData) DefaultHttpHeadersFactory(io.servicetalk.http.api.DefaultHttpHeadersFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 17 with HttpResponseMetaData

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

the class HttpResponseEncoderTest method chunkedWithTrailers.

@Test
void chunkedWithTrailers() {
    EmbeddedChannel channel = newEmbeddedChannel();
    byte[] content = new byte[128];
    ThreadLocalRandom.current().nextBytes(content);
    Buffer buffer = DEFAULT_ALLOCATOR.wrap(content);
    HttpHeaders trailers = DefaultHttpHeadersFactory.INSTANCE.newTrailers();
    trailers.add("TrailerStatus", "good");
    HttpResponseMetaData response = newResponseMetaData(HTTP_1_1, OK, INSTANCE.newHeaders());
    response.headers().add(CONNECTION, KEEP_ALIVE).add(SERVER, "unit-test").add(TRANSFER_ENCODING, CHUNKED);
    channel.writeOutbound(response);
    channel.writeOutbound(buffer.duplicate());
    channel.writeOutbound(trailers);
    verifyHttpResponse(channel, buffer, TransferEncoding.Chunked, true);
    assertFalse(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) Test(org.junit.jupiter.api.Test) 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