Search in sources :

Example 16 with DefaultCookie

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

the class BaseResponseInfoBuilderTest method builder_stores_values_as_expected.

@Test
public void builder_stores_values_as_expected() {
    // given
    String content = UUID.randomUUID().toString();
    int httpStatusCode = 200;
    HttpHeaders headers = new DefaultHttpHeaders();
    String mimeType = "text/text";
    Charset contentCharset = CharsetUtil.ISO_8859_1;
    Set<Cookie> cookies = Sets.newHashSet(new DefaultCookie("key1", "val1"), new DefaultCookie("key2", "val2"));
    boolean preventCompressedOutput = true;
    // when
    BaseResponseInfoBuilder<String> responseInfoBuilder = new BaseResponseInfoBuilder<String>() {
    }.withHttpStatusCode(httpStatusCode).withHeaders(headers).withDesiredContentWriterMimeType(mimeType).withDesiredContentWriterEncoding(contentCharset).withCookies(cookies).withPreventCompressedOutput(preventCompressedOutput);
    // then
    assertThat(responseInfoBuilder.getHttpStatusCode(), is(httpStatusCode));
    assertThat(responseInfoBuilder.getHeaders(), is(headers));
    assertThat(responseInfoBuilder.getDesiredContentWriterMimeType(), is(mimeType));
    assertThat(responseInfoBuilder.getDesiredContentWriterEncoding(), is(contentCharset));
    assertThat(responseInfoBuilder.getCookies(), is(cookies));
    assertThat(responseInfoBuilder.isPreventCompressedOutput(), is(preventCompressedOutput));
}
Also used : DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) Cookie(io.netty.handler.codec.http.cookie.Cookie) 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 17 with DefaultCookie

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

the class BaseResponseInfoTest method uber_constructor_for_full_response_sets_fields_as_expected.

@Test
public void uber_constructor_for_full_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
    BaseResponseInfo<?> responseInfo = createNewBaseResponseInfoForTesting(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.isPreventCompressedOutput(), is(preventCompressedResponse));
    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 18 with DefaultCookie

use of io.netty.handler.codec.http.cookie.DefaultCookie 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 19 with DefaultCookie

use of io.netty.handler.codec.http.cookie.DefaultCookie 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 20 with DefaultCookie

use of io.netty.handler.codec.http.cookie.DefaultCookie in project async-http-client by AsyncHttpClient.

the class RequestBuilderTest method testAddOrReplaceCookies.

public void testAddOrReplaceCookies() {
    RequestBuilder requestBuilder = new RequestBuilder();
    Cookie cookie = new DefaultCookie("name", "value");
    cookie.setDomain("google.com");
    cookie.setPath("/");
    cookie.setMaxAge(1000);
    cookie.setSecure(true);
    cookie.setHttpOnly(true);
    requestBuilder.addOrReplaceCookie(cookie);
    assertEquals(requestBuilder.cookies.size(), 1, "cookies size should be 1 after adding one cookie");
    assertEquals(requestBuilder.cookies.get(0), cookie, "cookie does not match");
    Cookie cookie2 = new DefaultCookie("name", "value");
    cookie2.setDomain("google2.com");
    cookie2.setPath("/path");
    cookie2.setMaxAge(1001);
    cookie2.setSecure(false);
    cookie2.setHttpOnly(false);
    requestBuilder.addOrReplaceCookie(cookie2);
    assertEquals(requestBuilder.cookies.size(), 1, "cookies size should remain 1 as we just replaced a cookie with same name");
    assertEquals(requestBuilder.cookies.get(0), cookie2, "cookie does not match");
    Cookie cookie3 = new DefaultCookie("name", "value");
    cookie3.setDomain("google.com");
    cookie3.setPath("/");
    cookie3.setMaxAge(1000);
    cookie3.setSecure(true);
    cookie3.setHttpOnly(true);
    requestBuilder.addOrReplaceCookie(cookie3);
    assertEquals(requestBuilder.cookies.size(), 2, "cookie size must be 2 after adding 1 more cookie i.e. cookie3");
}
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)

Aggregations

DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)21 Cookie (io.netty.handler.codec.http.cookie.Cookie)15 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)12 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)11 Test (org.junit.Test)11 Charset (java.nio.charset.Charset)7 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)4 HttpMethod (io.netty.handler.codec.http.HttpMethod)3 HttpVersion (io.netty.handler.codec.http.HttpVersion)3 HashMap (java.util.HashMap)3 List (java.util.List)3 RequestInfo (com.nike.riposte.server.http.RequestInfo)2 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)2 DataProviderRunner (com.tngtech.java.junit.dataprovider.DataProviderRunner)2 ByteBuf (io.netty.buffer.ByteBuf)2 Channel (io.netty.channel.Channel)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)2 ResponseCookie (org.springframework.http.ResponseCookie)2