Search in sources :

Example 1 with DefaultHttpHeadersFactory

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

the class HttpRequestEncoderTest method contentLengthNoTrailersHeaderWhiteSpaceEncodedWithValidationOff.

@Test
void contentLengthNoTrailersHeaderWhiteSpaceEncodedWithValidationOff() {
    EmbeddedChannel channel = newEmbeddedChannel();
    byte[] content = new byte[128];
    ThreadLocalRandom.current().nextBytes(content);
    Buffer buffer = allocator.wrap(content);
    HttpRequestMetaData request = newRequestMetaData(HTTP_1_1, GET, "/some/path?foo=bar&baz=yyy", new DefaultHttpHeadersFactory(false, false, false).newHeaders());
    request.headers().add(" " + CONNECTION + " ", " " + KEEP_ALIVE).add("  " + USER_AGENT + "   ", "    unit-test   ").add(CONTENT_LENGTH, valueOf(content.length));
    channel.writeOutbound(request);
    channel.writeOutbound(buffer.duplicate());
    ByteBuf byteBuf = channel.readOutbound();
    String actualMetaData = byteBuf.toString(US_ASCII);
    byteBuf.release();
    assertTrue(actualMetaData.contains("GET /some/path?foo=bar&baz=yyy HTTP/1.1" + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
    assertTrue(actualMetaData.contains(" " + CONNECTION + " :  " + KEEP_ALIVE + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
    assertTrue(actualMetaData.contains("  " + USER_AGENT + "   :     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) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Integer.toHexString(java.lang.Integer.toHexString) ByteBuf(io.netty.buffer.ByteBuf) DefaultHttpHeadersFactory(io.servicetalk.http.api.DefaultHttpHeadersFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with DefaultHttpHeadersFactory

use of io.servicetalk.http.api.DefaultHttpHeadersFactory 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)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 Buffer (io.servicetalk.buffer.api.Buffer)2 DefaultHttpHeadersFactory (io.servicetalk.http.api.DefaultHttpHeadersFactory)2 Integer.toHexString (java.lang.Integer.toHexString)2 Test (org.junit.jupiter.api.Test)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 HttpRequestMetaData (io.servicetalk.http.api.HttpRequestMetaData)1 HttpResponseMetaData (io.servicetalk.http.api.HttpResponseMetaData)1