Search in sources :

Example 41 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest 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 42 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project asterixdb by apache.

the class ConnectorApiServletTest method testGet.

@Test
public void testGet() throws Exception {
    // Starts test asterixdb cluster.
    SqlppExecutionTest.setUp();
    // Configures a test connector api servlet.
    ConnectorApiServlet let = new ConnectorApiServlet(new ConcurrentHashMap<>(), new String[] { "/" }, (ICcApplicationContext) ExecutionTestUtil.integrationUtil.cc.getApplicationContext());
    Map<String, NodeControllerInfo> nodeMap = new HashMap<>();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PrintWriter outputWriter = new PrintWriter(outputStream);
    // Creates mocks.
    IHyracksClientConnection mockHcc = mock(IHyracksClientConnection.class);
    NodeControllerInfo mockInfo1 = mock(NodeControllerInfo.class);
    NodeControllerInfo mockInfo2 = mock(NodeControllerInfo.class);
    IServletRequest mockRequest = mock(IServletRequest.class);
    IServletResponse mockResponse = mock(IServletResponse.class);
    FullHttpRequest mockHttpRequest = mock(FullHttpRequest.class);
    // Put stuff in let map
    let.ctx().put(ServletConstants.HYRACKS_CONNECTION_ATTR, mockHcc);
    // Sets up mock returns.
    when(mockRequest.getHttpRequest()).thenReturn(mockHttpRequest);
    when(mockHttpRequest.method()).thenReturn(HttpMethod.GET);
    when(mockRequest.getParameter("dataverseName")).thenReturn("Metadata");
    when(mockRequest.getParameter("datasetName")).thenReturn("Dataset");
    when(mockResponse.writer()).thenReturn(outputWriter);
    when(mockHcc.getNodeControllerInfos()).thenReturn(nodeMap);
    when(mockInfo1.getNetworkAddress()).thenReturn(new NetworkAddress("127.0.0.1", 3099));
    when(mockInfo2.getNetworkAddress()).thenReturn(new NetworkAddress("127.0.0.2", 3099));
    // Calls ConnectorAPIServlet.formResponseObject.
    nodeMap.put("asterix_nc1", mockInfo1);
    nodeMap.put("asterix_nc2", mockInfo2);
    let.handle(mockRequest, mockResponse);
    // Constructs the actual response.
    ObjectMapper om = new ObjectMapper();
    ObjectNode actualResponse = (ObjectNode) om.readTree(outputStream.toString());
    // Checks the temp-or-not, primary key, data type of the dataset.
    boolean temp = actualResponse.get("temp").asBoolean();
    Assert.assertFalse(temp);
    String primaryKey = actualResponse.get("keys").asText();
    Assert.assertEquals("DataverseName,DatasetName", primaryKey);
    ARecordType recordType = (ARecordType) JSONDeserializerForTypes.convertFromJSON(actualResponse.get("type"));
    Assert.assertEquals(getMetadataRecordType("Metadata", "Dataset"), recordType);
    // Checks the correctness of results.
    ArrayNode splits = (ArrayNode) actualResponse.get("splits");
    String path = (splits.get(0)).get("path").asText();
    Assert.assertTrue(path.endsWith("Metadata/Dataset_idx_Dataset"));
    // Tears down the asterixdb cluster.
    SqlppExecutionTest.tearDown();
}
Also used : IHyracksClientConnection(org.apache.hyracks.api.client.IHyracksClientConnection) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IServletRequest(org.apache.hyracks.http.api.IServletRequest) NetworkAddress(org.apache.hyracks.api.comm.NetworkAddress) NodeControllerInfo(org.apache.hyracks.api.client.NodeControllerInfo) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ConnectorApiServlet(org.apache.asterix.api.http.server.ConnectorApiServlet) IServletResponse(org.apache.hyracks.http.api.IServletResponse) ARecordType(org.apache.asterix.om.types.ARecordType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PrintWriter(java.io.PrintWriter) Test(org.junit.Test) SqlppExecutionTest(org.apache.asterix.test.runtime.SqlppExecutionTest)

Example 43 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project motan by weibocom.

the class NettyHttpRequestHandlerTest method testChannelRead0.

@Test
public void testChannelRead0() throws Exception {
    final MessageHandler messageHandler = mockery.mock(MessageHandler.class);
    final ChannelHandlerContext ctx = mockery.mock(ChannelHandlerContext.class);
    final FullHttpResponse response = mockery.mock(FullHttpResponse.class);
    mockery.checking(new Expectations() {

        {
            allowing(ctx).write(with(any(FullHttpResponse.class)));
            will(new CustomAction("verify") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    FullHttpResponse actualResponse = (FullHttpResponse) invocation.getParameter(0);
                    assertNotNull(actualResponse);
                    assertEquals(response, actualResponse);
                    return null;
                }
            });
            allowing(ctx).flush();
            will(returnValue(null));
            allowing(ctx).close();
            will(returnValue(null));
            atLeast(1).of(messageHandler).handle(with(any(Channel.class)), with(anything()));
            will(returnValue(response));
            allowing(response).headers();
            will(returnValue(new DefaultHttpHeaders()));
        }
    });
    FullHttpRequest httpRequest = buildHttpRequest("anyPath");
    NettyHttpRequestHandler handler = new NettyHttpRequestHandler(null, messageHandler);
    handler.channelRead0(ctx, httpRequest);
}
Also used : Expectations(org.jmock.Expectations) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) MessageHandler(com.weibo.api.motan.transport.MessageHandler) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Channel(com.weibo.api.motan.transport.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Test(org.junit.Test)

Example 44 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project moco by dreamhead.

the class DefaultHttpRequest method toFullHttpRequest.

public FullHttpRequest toFullHttpRequest() {
    ByteBuf buffer = Unpooled.buffer();
    MessageContent content = getContent();
    if (content != null) {
        buffer.writeBytes(content.getContent());
    }
    QueryStringEncoder encoder = new QueryStringEncoder(uri);
    for (Map.Entry<String, String[]> entry : queries.entrySet()) {
        String[] values = entry.getValue();
        for (String value : values) {
            encoder.addParam(entry.getKey(), value);
        }
    }
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.valueOf(getVersion().text()), io.netty.handler.codec.http.HttpMethod.valueOf(method.name()), encoder.toString(), buffer);
    for (Map.Entry<String, String> entry : getHeaders().entrySet()) {
        request.headers().add(entry.getKey(), entry.getValue());
    }
    return request;
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder)

Example 45 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project moco by dreamhead.

the class MocoHandler method handleRequest.

private FullHttpResponse handleRequest(final FullHttpRequest message) {
    HttpRequest request = DefaultHttpRequest.newRequest(message);
    DefaultMutableHttpResponse httpResponse = getHttpResponse(request);
    FullHttpResponse response = httpResponse.toFullResponse();
    prepareForKeepAlive(message, response);
    monitor.onMessageLeave(httpResponse);
    return response;
}
Also used : HttpRequest(com.github.dreamhead.moco.HttpRequest) DefaultHttpRequest(com.github.dreamhead.moco.model.DefaultHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultMutableHttpResponse(com.github.dreamhead.moco.model.DefaultMutableHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Aggregations

FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)97 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)65 Test (org.junit.Test)51 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)32 AsciiString (io.netty.util.AsciiString)25 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 ByteBuf (io.netty.buffer.ByteBuf)19 ChannelPromise (io.netty.channel.ChannelPromise)16 HttpResponse (io.netty.handler.codec.http.HttpResponse)13 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)12 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)11 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)10 URI (java.net.URI)10 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)9 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)9 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)8 ChannelFuture (io.netty.channel.ChannelFuture)7 Channel (io.netty.channel.Channel)6 NullDispatcher (org.elasticsearch.http.NullDispatcher)6 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)5