Search in sources :

Example 11 with DefaultHttpHeaders

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

the class RequestInfoForLoggingRiposteAdapterTest method getHeaderMapDelegatesToRequestInfoAndCachesResult.

@Test
public void getHeaderMapDelegatesToRequestInfoAndCachesResult() {
    Map<String, List<String>> expectedHeaderMap = new TreeMap<>(MapBuilder.<String, List<String>>builder().put("header1", Arrays.asList("h1val1")).put("header2", Arrays.asList("h2val1", "h2val2")).build());
    HttpHeaders nettyHeaders = new DefaultHttpHeaders();
    for (Map.Entry<String, List<String>> headerEntry : expectedHeaderMap.entrySet()) {
        nettyHeaders.add(headerEntry.getKey(), headerEntry.getValue());
    }
    setFieldOnRequestInfo("headers", nettyHeaders);
    Map<String, List<String>> actualHeaderMap = adapter.getHeadersMap();
    assertThat(actualHeaderMap, is(expectedHeaderMap));
    assertThat(adapter.getHeadersMap(), sameInstance(actualHeaderMap));
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) List(java.util.List) TreeMap(java.util.TreeMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 12 with DefaultHttpHeaders

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

the class BasicHttpTest method postUnboundedInputStreamAsBodyStream.

@Test
public void postUnboundedInputStreamAsBodyStream() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            HttpHeaders h = new DefaultHttpHeaders();
            h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
            server.enqueue(new AbstractHandler() {

                EchoHandler chain = new EchoHandler();

                @Override
                public void handle(String target, org.eclipse.jetty.server.Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
                    assertEquals(request.getHeader(TRANSFER_ENCODING.toString()), HttpHeaderValues.CHUNKED.toString());
                    assertNull(request.getHeader(CONTENT_LENGTH.toString()));
                    chain.handle(target, request, httpServletRequest, httpServletResponse);
                }
            });
            server.enqueueEcho();
            client.preparePost(getTargetUrl()).setHeaders(h).setBody(new ByteArrayInputStream("{}".getBytes(StandardCharsets.ISO_8859_1))).execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    assertEquals(response.getStatusCode(), 200);
                    assertEquals(response.getResponseBody(), "{}");
                    return response;
                }
            }).get(TIMEOUT, SECONDS);
        });
    });
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) EchoHandler(org.asynchttpclient.testserver.HttpServer.EchoHandler) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 13 with DefaultHttpHeaders

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

the class BasicHttpTest method postFormParametersAsBodyStream.

@Test
public void postFormParametersAsBodyStream() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            HttpHeaders h = new DefaultHttpHeaders();
            h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 5; i++) {
                sb.append("param_").append(i).append("=value_").append(i).append("&");
            }
            sb.setLength(sb.length() - 1);
            server.enqueueEcho();
            client.preparePost(getTargetUrl()).setHeaders(h).setBody(new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8))).execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    assertEquals(response.getStatusCode(), 200);
                    for (int i = 1; i < 5; i++) {
                        assertEquals(response.getHeader("X-param_" + i), "value_" + i);
                    }
                    return response;
                }
            }).get(TIMEOUT, SECONDS);
        });
    });
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 14 with DefaultHttpHeaders

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

the class NettyAsyncResponseTest method testCookieParseExpires.

@Test(groups = "standalone")
public void testCookieParseExpires() {
    // e.g. "Tue, 27 Oct 2015 12:54:24 GMT";
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date date = new Date(System.currentTimeMillis() + 60000);
    final String cookieDef = String.format("efmembercheck=true; expires=%s; path=/; domain=.eclipse.org", sdf.format(date));
    HttpResponseHeaders responseHeaders = new HttpResponseHeaders(new DefaultHttpHeaders().add(SET_COOKIE, cookieDef));
    NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null), responseHeaders, null);
    List<Cookie> cookies = response.getCookies();
    assertEquals(cookies.size(), 1);
    Cookie cookie = cookies.get(0);
    assertTrue(cookie.maxAge() >= 58 && cookie.maxAge() <= 60);
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) HttpResponseHeaders(org.asynchttpclient.HttpResponseHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 15 with DefaultHttpHeaders

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

the class ResumableAsyncHandlerTest method testOnHeadersReceived.

@Test
public void testOnHeadersReceived() throws Exception {
    ResumableAsyncHandler handler = new ResumableAsyncHandler();
    HttpHeaders responseHeaders = new DefaultHttpHeaders();
    HttpResponseHeaders headers = new HttpResponseHeaders(responseHeaders);
    State status = handler.onHeadersReceived(headers);
    assertEquals(status, AsyncHandler.State.CONTINUE, "State should be CONTINUE for a successful onHeadersReceived");
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpResponseHeaders(org.asynchttpclient.HttpResponseHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) State(org.asynchttpclient.AsyncHandler.State) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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