Search in sources :

Example 31 with DefaultHttpHeaders

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

the class RequestInfoImplTest method netty_helper_constructor_populates_request_info_appropriately.

@Test
public void netty_helper_constructor_populates_request_info_appropriately() {
    // given
    String uri = "/some/uri/path/%24foobar%26?foo=bar&secondparam=secondvalue";
    Map<String, List<String>> expectedQueryParamMap = new HashMap<>();
    expectedQueryParamMap.put("foo", Arrays.asList("bar"));
    expectedQueryParamMap.put("secondparam", Arrays.asList("secondvalue"));
    HttpMethod method = HttpMethod.PATCH;
    String cookieName = UUID.randomUUID().toString();
    String cookieValue = UUID.randomUUID().toString();
    String content = UUID.randomUUID().toString();
    byte[] contentBytes = content.getBytes();
    Charset contentCharset = CharsetUtil.UTF_8;
    ByteBuf contentByteBuf = Unpooled.copiedBuffer(contentBytes);
    HttpHeaders headers = new DefaultHttpHeaders().add("header1", "val1").add(HttpHeaders.Names.CONTENT_TYPE, contentCharset).add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE).add(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX.encode(cookieName, cookieValue));
    HttpHeaders trailingHeaders = new DefaultHttpHeaders().add("trailingHeader1", "trailingVal1");
    HttpVersion protocolVersion = HttpVersion.HTTP_1_1;
    FullHttpRequest nettyRequestMock = mock(FullHttpRequest.class);
    doReturn(uri).when(nettyRequestMock).getUri();
    doReturn(method).when(nettyRequestMock).getMethod();
    doReturn(headers).when(nettyRequestMock).headers();
    doReturn(trailingHeaders).when(nettyRequestMock).trailingHeaders();
    doReturn(contentByteBuf).when(nettyRequestMock).content();
    doReturn(protocolVersion).when(nettyRequestMock).getProtocolVersion();
    // when
    RequestInfoImpl<?> requestInfo = new RequestInfoImpl<>(nettyRequestMock);
    // then
    assertThat("getUri was not the same value sent in", requestInfo.getUri(), is(uri));
    assertThat("getPath did not decode as expected", requestInfo.getPath(), is("/some/uri/path/$foobar&"));
    assertThat(requestInfo.getMethod(), is(method));
    assertThat(requestInfo.getHeaders(), is(headers));
    assertThat(requestInfo.getTrailingHeaders(), is(trailingHeaders));
    assertThat(requestInfo.getQueryParams(), notNullValue());
    assertThat(requestInfo.getQueryParams().parameters(), is(expectedQueryParamMap));
    assertThat(requestInfo.getCookies(), is(Sets.newHashSet(new DefaultCookie(cookieName, cookieValue))));
    assertThat(requestInfo.pathTemplate, nullValue());
    assertThat(requestInfo.pathParams.isEmpty(), is(true));
    assertThat(requestInfo.getRawContentBytes(), is(contentBytes));
    assertThat(requestInfo.getRawContent(), is(content));
    assertThat(requestInfo.content, nullValue());
    assertThat(requestInfo.getContentCharset(), is(contentCharset));
    assertThat(requestInfo.getProtocolVersion(), is(protocolVersion));
    assertThat(requestInfo.isKeepAliveRequested(), is(true));
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HashMap(java.util.HashMap) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) List(java.util.List) HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test)

Example 32 with DefaultHttpHeaders

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

the class HttpUtilsTest method determineCharsetFromContentType_throws_InvalidCharsetInContentTypeHeaderException_if_passed_header_with_invalid_charset.

@Test(expected = InvalidCharsetInContentTypeHeaderException.class)
public void determineCharsetFromContentType_throws_InvalidCharsetInContentTypeHeaderException_if_passed_header_with_invalid_charset() {
    // given
    HttpHeaders headers = new DefaultHttpHeaders().add(HttpHeaders.Names.CONTENT_TYPE, "text/text charset=garbagio");
    // expect
    HttpUtils.determineCharsetFromContentType(headers, CharsetUtil.UTF_8);
    fail("Expected an exception but none was thrown");
}
Also used : 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 33 with DefaultHttpHeaders

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

the class HttpUtilsTest method extractCookies_handles_cookie_values_leniently.

@Test
public void extractCookies_handles_cookie_values_leniently() {
    // given
    //these are cookie values seen in the wild...
    Cookie cookie1 = new DefaultCookie(UUID.randomUUID().toString(), "2094%3Az%7C2021%3Ab");
    Cookie cookie2 = new DefaultCookie(UUID.randomUUID().toString(), "geoloc=cc=US,rc=OR,tp=vhigh,tz=PST,la=45.4978,lo=-122.6937,bw=5000");
    Cookie cookie3 = new DefaultCookie(UUID.randomUUID().toString(), "\"dm=n.com&si=27431295-a282-4745-8cd5-542e7fce" + "429e&ss=1477551008358&sl=76&tt=437632&obo=12&sh=1477552753923%3D76%3A12%3A437632%2C1477552698670%3D75%3" + "A12%3A429879%2C1477552677137%3D74%3A12%3A426596%2C1477552672564%3D73%3A12%3A425585%2C1477552669893%3D72" + "%3A12%3A423456&bcn=%2F%2F3408178b.mpstat.us%2F&ld=1477552753923&r=http%3A%2F%2Fwww.nike.com%2Fbe%2Fde_de%" + "2F&ul=1477552756811\"");
    HttpHeaders headers = new DefaultHttpHeaders().add(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX.encode(cookie1, cookie2, cookie3));
    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));
    assertThat(extractedCookies.contains(cookie3), 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 34 with DefaultHttpHeaders

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

the class HttpServletResponseWrapperForResponseInfoTest method beforeMethod.

@Before
public void beforeMethod() {
    headers = new DefaultHttpHeaders();
    responseInfoMock = mock(ResponseInfo.class);
    doReturn(headers).when(responseInfoMock).getHeaders();
    wrapper = new HttpServletResponseWrapperForResponseInfo<>(responseInfoMock);
}
Also used : ResponseInfo(com.nike.riposte.server.http.ResponseInfo) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Before(org.junit.Before)

Example 35 with DefaultHttpHeaders

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

the class RequestInfoImpl method dummyInstanceForUnknownRequests.

/**
     * Creates a new RequestInfo that represents unknown requests. Usually only needed in error situations. The URI,
     * query params, and headers will be tagged with {@link #NONE_OR_UNKNOWN_TAG} to indicate that the request was
     * unknown.
     */
public static RequestInfoImpl<?> dummyInstanceForUnknownRequests() {
    HttpHeaders headers = new DefaultHttpHeaders().set(NONE_OR_UNKNOWN_TAG, "true");
    QueryStringDecoder queryParams = new QueryStringDecoder("/?" + NONE_OR_UNKNOWN_TAG + "=true");
    return new RequestInfoImpl(NONE_OR_UNKNOWN_TAG, null, headers, null, queryParams, null, null, null, null, false, true, false);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders)

Aggregations

DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)49 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)36 Test (org.testng.annotations.Test)20 Test (org.junit.Test)18 Cookie (io.netty.handler.codec.http.cookie.Cookie)14 DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)12 HttpServletResponse (javax.servlet.http.HttpServletResponse)12 HttpTest (org.asynchttpclient.testserver.HttpTest)12 AsyncCompletionHandlerAdapter (org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter)11 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)9 Charset (java.nio.charset.Charset)8 HttpResponseHeaders (org.asynchttpclient.HttpResponseHeaders)6 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 RequestInfo (com.nike.riposte.server.http.RequestInfo)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Map (java.util.Map)4 ByteBuf (io.netty.buffer.ByteBuf)3 Channel (io.netty.channel.Channel)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3