Search in sources :

Example 11 with HttpHeaders

use of io.netty.handler.codec.http.HttpHeaders in project riposte by Nike-Inc.

the class HttpUtilsTest method determineCharsetFromContentType_works.

@Test
@DataProvider(value = { "text/text charset=US-ASCII |   UTF-8   | US-ASCII", "text/text charset=us-ascii |   UTF-8   | US-ASCII", "text/text                  |   UTF-8   | UTF-8", "                           |   UTF-8   | UTF-8", "null                       |   UTF-8   | UTF-8" }, splitBy = "\\|")
public void determineCharsetFromContentType_works(String contentTypeHeader, String defaultCharsetString, String expectedCharsetString) {
    // given
    Charset defaultCharset = Charset.forName(defaultCharsetString);
    Charset expectedCharset = Charset.forName(expectedCharsetString);
    HttpHeaders headers = new DefaultHttpHeaders().add(HttpHeaders.Names.CONTENT_TYPE, String.valueOf(contentTypeHeader));
    // when
    Charset actualCharset = HttpUtils.determineCharsetFromContentType(headers, defaultCharset);
    // then
    assertThat(actualCharset, is(expectedCharset));
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Charset(java.nio.charset.Charset) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 12 with HttpHeaders

use of io.netty.handler.codec.http.HttpHeaders in project riposte by Nike-Inc.

the class HttpUtilsTest method extractCookies_works_if_cookies_defined_in_trailing_headers.

@Test
public void extractCookies_works_if_cookies_defined_in_trailing_headers() {
    // given
    Cookie cookie1 = new DefaultCookie(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    Cookie cookie2 = new DefaultCookie(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    HttpHeaders trailingHeaders = new DefaultHttpHeaders().add(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX.encode(cookie1, cookie2));
    FullHttpRequest nettyRequestMock = mock(FullHttpRequest.class);
    doReturn(new DefaultHttpHeaders()).when(nettyRequestMock).headers();
    doReturn(trailingHeaders).when(nettyRequestMock).trailingHeaders();
    // when
    Set<Cookie> extractedCookies = HttpUtils.extractCookies(nettyRequestMock);
    // then
    assertThat(extractedCookies.contains(cookie1), is(true));
    assertThat(extractedCookies.contains(cookie2), is(true));
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Test(org.junit.Test)

Example 13 with HttpHeaders

use of io.netty.handler.codec.http.HttpHeaders in project riposte by Nike-Inc.

the class HttpUtilsTest method extractCookies_works_if_cookies_defined_in_headers.

@Test
public void extractCookies_works_if_cookies_defined_in_headers() {
    // given
    Cookie cookie1 = new DefaultCookie(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    Cookie cookie2 = new DefaultCookie(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    HttpHeaders headers = new DefaultHttpHeaders().add(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX.encode(cookie1, cookie2));
    HttpRequest nettyRequestMock = mock(HttpRequest.class);
    doReturn(headers).when(nettyRequestMock).headers();
    // when
    Set<Cookie> extractedCookies = HttpUtils.extractCookies(nettyRequestMock);
    // then
    assertThat(extractedCookies.contains(cookie1), is(true));
    assertThat(extractedCookies.contains(cookie2), is(true));
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Test(org.junit.Test)

Example 14 with HttpHeaders

use of io.netty.handler.codec.http.HttpHeaders in project riposte by Nike-Inc.

the class ChunkedResponseInfoTest method uber_constructor_for_chunked_response_sets_fields_as_expected.

@Test
public void uber_constructor_for_chunked_response_sets_fields_as_expected() {
    // given
    int httpStatusCode = 200;
    HttpHeaders headers = new DefaultHttpHeaders();
    String mimeType = "text/text";
    Charset contentCharset = CharsetUtil.UTF_8;
    Set<Cookie> cookies = Sets.newHashSet(new DefaultCookie("key1", "val1"), new DefaultCookie("key2", "val2"));
    boolean preventCompressedResponse = true;
    // when
    ChunkedResponseInfo responseInfo = new ChunkedResponseInfo(httpStatusCode, headers, mimeType, contentCharset, cookies, preventCompressedResponse);
    // then
    assertThat(responseInfo.getHttpStatusCode(), is(httpStatusCode));
    assertThat(responseInfo.getHeaders(), is(headers));
    assertThat(responseInfo.getDesiredContentWriterMimeType(), is(mimeType));
    assertThat(responseInfo.getDesiredContentWriterEncoding(), is(contentCharset));
    assertThat(responseInfo.getCookies(), is(cookies));
    assertThat(responseInfo.getUncompressedRawContentLength(), nullValue());
    assertThat(responseInfo.getFinalContentLength(), nullValue());
    assertThat(responseInfo.isPreventCompressedOutput(), is(preventCompressedResponse));
    assertThat(responseInfo.isChunkedResponse(), is(true));
    assertThat(responseInfo.isResponseSendingStarted(), is(false));
    assertThat(responseInfo.isResponseSendingLastChunkSent(), is(false));
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Charset(java.nio.charset.Charset) Test(org.junit.Test)

Example 15 with HttpHeaders

use of io.netty.handler.codec.http.HttpHeaders in project riposte by Nike-Inc.

the class HttpServletResponseWrapperForResponseInfoTest method containsHeader_delegates_to_responseInfo.

@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void containsHeader_delegates_to_responseInfo(boolean containsHeader) {
    // given
    HttpHeaders headersMock = mock(HttpHeaders.class);
    doReturn(headersMock).when(responseInfoMock).getHeaders();
    String headerName = UUID.randomUUID().toString();
    doReturn(containsHeader).when(headersMock).contains(headerName);
    // expect
    assertThat(wrapper.containsHeader(headerName)).isEqualTo(containsHeader);
    verify(headersMock).contains(headerName);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Aggregations

HttpHeaders (io.netty.handler.codec.http.HttpHeaders)248 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)119 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)67 Test (org.junit.Test)61 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)56 Test (org.testng.annotations.Test)51 Test (org.junit.jupiter.api.Test)50 HttpRequest (io.netty.handler.codec.http.HttpRequest)33 AsciiString (io.netty.util.AsciiString)29 ByteBuf (io.netty.buffer.ByteBuf)27 BStruct (org.ballerinalang.model.values.BStruct)26 HttpResponse (io.netty.handler.codec.http.HttpResponse)23 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)21 Cookie (io.netty.handler.codec.http.cookie.Cookie)20 HttpServletResponse (javax.servlet.http.HttpServletResponse)20 BValue (org.ballerinalang.model.values.BValue)19 ChannelPromise (io.netty.channel.ChannelPromise)18 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)18 BString (org.ballerinalang.model.values.BString)18 DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)17