Example usage for io.netty.handler.codec.http HttpMethod GET

List of usage examples for io.netty.handler.codec.http HttpMethod GET

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpMethod GET.

Prototype

HttpMethod GET

To view the source code for io.netty.handler.codec.http HttpMethod GET.

Click Source Link

Document

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI.

Usage

From source file:org.cloudfoundry.reactor._TestRequest.java

License:Apache License

public void assertEquals(RecordedRequest request) {
    assertThat(getMethod()).hasToString(request.getMethod());
    assertThat(extractPath(request)).isEqualTo(getPath());

    assertThat(request.getHeader(HttpHeaderNames.TRANSFER_ENCODING.toString()))
            .as("Does not have Transfer-Encoding header").isNull();

    if (!HttpMethod.GET.toString().equals(request.getMethod())) {
        assertThat(request.getHeader(HttpHeaderNames.CONTENT_LENGTH.toString())).as("Has Content-Length header")
                .isNotNull();//from   w  ww  .  j  a v  a  2s .com
    }

    getHeaders().forEach((key, value) -> {
        if (value == null) {
            assertThat(request.getHeader(key)).as("Does not have %s header", key).isNull();
        } else {
            assertThat(request.getHeader(key)).as("Header %s value", key).isEqualTo(value);
        }
    });

    if (getPayload().isPresent()) {
        assertBodyEquals(request.getBody(), getPayload().map(_TestRequest::getBuffer).get());
    } else if (getContents().isPresent()) {
        getContents().get().accept(Tuples.of(request.getHeaders(), request.getBody()));
    } else {
        assertThat(request.getBodySize()).as("Invalid request body: %s", request.getBody().readUtf8())
                .isEqualTo(0);
    }
}

From source file:org.ebayopensource.scc.cache.RequestKeyGeneratorTest.java

License:Apache License

@Test
public void test() {
    RequestKeyGenerator keyGen = new RequestKeyGenerator(s_appConfig);
    DefaultFullHttpRequest req1 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET,
            "http://s.ebay.com/v1/s1");
    DefaultFullHttpRequest req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET,
            "http://s.ebay.com/v1/s1");
    assertEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));

    req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.POST, "http://s.ebay.com/v1/s1");
    assertNotEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));

    req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://s.ebay.com/v1/s1");
    assertEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));

    req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "http://s.ebay.com/v1/s2");
    assertNotEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));

    req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "http://s.ebay.com/v1/s1");
    req1.headers().add("header1", "value1");
    req2.headers().add("header1", "value1");
    assertEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));

    req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "http://s.ebay.com/v1/s1");
    req2.headers().add("header1", "value2");
    assertNotEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));

    req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "http://s.ebay.com/v1/s1");
    req2.headers().add("header1", "value1");
    req2.trailingHeaders().add("header1", "value1");
    assertEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));

    req2.headers().add("Date", "idvalue");
    assertEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));
}

From source file:org.ebayopensource.scc.cache.RequestKeyGeneratorTest.java

License:Apache License

@Test
public void testHttpsReq() {
    RequestKeyGenerator keyGen = new RequestKeyGenerator(s_appConfig);
    DefaultFullHttpRequest req1 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/v1/s1");

    assertTrue(keyGen.generateKey(req1).startsWith("/v1/s1"));

    HttpHeaders.setHost(req1, "s.ebay.com");
    assertTrue(keyGen.generateKey(req1).startsWith("https://s.ebay.com/v1/s1"));
}

From source file:org.ebayopensource.scc.cache.RequestKeyGeneratorTest.java

License:Apache License

@Test
public void testURIMatchOnly() throws IOException {
    AppConfiguration appConfig = new AppConfiguration(new ConfigLoader(),
            "./src/test/resources/testuserconfig.json");
    appConfig.init();//from ww w.  j  a  va  2  s  .  co  m
    RequestKeyGenerator keyGen = new RequestKeyGenerator(appConfig);

    ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    buffer.writeBytes("{\"fromDate\":1464251112185,\"toDate\":1464337512185}".getBytes());
    DefaultFullHttpRequest req1 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET,
            "http://test.ebay.com/v1/s1", buffer);

    String key1 = keyGen.generateKey(req1);

    buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    buffer.writeBytes("{\"fromDate\":1464251113750,\"toDate\":1464337513750}".getBytes());
    DefaultFullHttpRequest req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET,
            "http://test.ebay.com/v1/s1", buffer);

    String key2 = keyGen.generateKey(req2);
    assertEquals(key1, key2);

    HttpHeaders.setContentLength(req2, 758);
    key2 = keyGen.generateKey(req2);
    assertEquals(key1, key2);

    appConfig.put("uriMatchOnly", null);
    keyGen = new RequestKeyGenerator(appConfig);
    key1 = keyGen.generateKey(req1);
    key2 = keyGen.generateKey(req2);

    assertNotEquals(key1, key2);
}

From source file:org.ebayopensource.scc.filter.NettyRequestProxyFilter.java

License:Apache License

protected HttpResponse handleNonProxyRequest(FullHttpRequest req) {
    String uri = req.getUri();/*ww  w.  jav a 2 s  . c o m*/
    if ("/version".equals(uri)) {
        if (HttpMethod.GET.equals(req.getMethod())) {
            JsonObject jsonObj = new JsonObject();
            jsonObj.addProperty("name", m_appConfig.getAppName());
            jsonObj.addProperty("version", m_appConfig.getAppVersion());
            byte[] content = jsonObj.toString().getBytes(CharsetUtil.UTF_8);
            DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                    HttpResponseStatus.OK, Unpooled.copiedBuffer(content));
            HttpHeaders.setKeepAlive(resp, false);
            HttpHeaders.setHeader(resp, HttpHeaders.Names.CONTENT_TYPE, "application/json");
            HttpHeaders.setContentLength(resp, content.length);
            return resp;
        }
    }

    return RESPONSE_404;
}

From source file:org.ebayopensource.scc.filter.NettyRequestProxyFilterTest.java

License:Apache License

@Test
public void testFilterRequest() throws IOException {
    AppConfiguration appConfig = new AppConfiguration(new ConfigLoader(), null);
    appConfig.init();//ww  w .  j a v  a2s . c o m

    PolicyManager policyManager = mock(PolicyManager.class);
    NettyRequestProxyFilter filter = new NettyRequestProxyFilter(policyManager, appConfig);

    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.attr(any(AttributeKey.class))).thenReturn(mock(Attribute.class));
    assertNull(filter.filterRequest(mock(HttpRequest.class), ctx));

    DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
            "http://test.ebay.com/s/");
    when(policyManager.cacheIsNeededFor(any(CacheDecisionObject.class))).thenReturn(false);
    assertNull(filter.filterRequest(req, ctx));

    when(policyManager.cacheIsNeededFor(any(CacheDecisionObject.class))).thenReturn(true);
    CacheManager cm = mock(CacheManager.class);
    when(policyManager.getCacheManager()).thenReturn(cm);
    assertNull(filter.filterRequest(req, ctx));

    FullHttpResponse resp = mock(FullHttpResponse.class);
    HttpHeaders respHeaders = mock(HttpHeaders.class);
    when(resp.headers()).thenReturn(respHeaders);
    when(respHeaders.get(any(CharSequence.class))).thenReturn("100");
    when(cm.get(anyString())).thenReturn(resp);
    Channel channel = mock(Channel.class);
    SocketChannelConfig config = mock(SocketChannelConfig.class);
    when(channel.config()).thenReturn(config);
    when(ctx.channel()).thenReturn(channel);
    req.headers().add("h1", "v1");

    when(resp.content()).thenReturn(new EmptyByteBuf(new PooledByteBufAllocator()))
            .thenReturn(Unpooled.copiedBuffer("Hello".getBytes()));
    assertEquals(resp, filter.filterRequest(req, ctx));
    assertEquals(resp, filter.filterRequest(req, ctx));
}

From source file:org.ebayopensource.scc.filter.NettyRequestProxyFilterTest.java

License:Apache License

@Test
public void testHandleNonProxyRequest() throws IOException {
    AppConfiguration appConfig = new AppConfiguration(new ConfigLoader(), null);
    appConfig.init();/*w  ww . ja  v  a  2s .c o m*/
    PolicyManager policyManager = mock(PolicyManager.class);
    NettyRequestProxyFilter filter = new NettyRequestProxyFilter(policyManager, appConfig);

    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.attr(any(AttributeKey.class))).thenReturn(mock(Attribute.class));
    assertNull(filter.filterRequest(mock(HttpRequest.class), ctx));

    DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/version");
    HttpHeaders.setHost(req, "localhost:32876");

    HttpResponse resp = filter.filterRequest(req, ctx);
    assertTrue(resp instanceof FullHttpResponse);
    FullHttpResponse response = (FullHttpResponse) resp;
    assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
    assertTrue(HttpHeaders.getContentLength(response) > 0);
    assertEquals(HttpResponseStatus.OK, response.getStatus());

    req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/version");
    HttpHeaders.setHost(req, "127.0.0.1:32876");

    resp = filter.filterRequest(req, ctx);
    assertTrue(resp instanceof FullHttpResponse);
    response = (FullHttpResponse) resp;
    assertEquals(HttpResponseStatus.NOT_FOUND, response.getStatus());
    assertEquals(0, HttpHeaders.getContentLength(response));

    req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/NOTFOUND");
    HttpHeaders.setHost(req, "127.0.0.1:32876");

    resp = filter.filterRequest(req, ctx);
    assertTrue(resp instanceof FullHttpResponse);
    response = (FullHttpResponse) resp;
    assertEquals(HttpResponseStatus.NOT_FOUND, response.getStatus());
    assertEquals(0, HttpHeaders.getContentLength(response));
}

From source file:org.ebayopensource.scc.filter.NettyRequestProxyFilterTest.java

License:Apache License

@Test
public void testIsProxyRequest() throws IOException {
    AppConfiguration appConfig = new AppConfiguration(new ConfigLoader(), null);
    appConfig.init();//from www . j a va2s. co  m
    PolicyManager policyManager = mock(PolicyManager.class);
    NettyRequestProxyFilter filter = new NettyRequestProxyFilter(policyManager, appConfig);

    DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/version");
    assertTrue(filter.isProxyRequest(req));
    assertRequest(filter, req, false, "localhost:32876");
    assertRequest(filter, req, false, "127.0.0.1:32876");
    assertRequest(filter, req, true, "localhost:32877");
    assertRequest(filter, req, true, "localhost");
    assertRequest(filter, req, true, "127.0.0.1");
    assertRequest(filter, req, true, "trace.vip.ebay.com");
    assertRequest(filter, req, true, "");
}

From source file:org.elasticsearch.hadoop.http.netty4.Netty4HttpRequest.java

License:Apache License

@Override
public Method method() {
    HttpMethod httpMethod = request.method();
    if (httpMethod == HttpMethod.GET)
        return Method.GET;

    if (httpMethod == HttpMethod.POST)
        return Method.POST;

    if (httpMethod == HttpMethod.PUT)
        return Method.PUT;

    if (httpMethod == HttpMethod.DELETE)
        return Method.DELETE;

    if (httpMethod == HttpMethod.HEAD) {
        return Method.HEAD;
    }/*from www .  j  a v a2 s .  co m*/

    if (httpMethod == HttpMethod.OPTIONS) {
        return Method.OPTIONS;
    }

    return Method.GET;
}

From source file:org.elasticsearch.http.netty4.Netty4CorsTests.java

License:Apache License

private FullHttpResponse executeRequest(final Settings settings, final String originValue, final String host) {
    // construct request and send it over the transport layer
    final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
    if (originValue != null) {
        httpRequest.headers().add(HttpHeaderNames.ORIGIN, originValue);
    }/*from   ww  w  .  jav a  2 s.c  o  m*/
    httpRequest.headers().add(HttpHeaderNames.HOST, host);
    EmbeddedChannel embeddedChannel = new EmbeddedChannel();
    embeddedChannel.pipeline()
            .addLast(new Netty4CorsHandler(Netty4HttpServerTransport.buildCorsConfig(settings)));
    Netty4HttpRequest nettyRequest = new Netty4HttpRequest(httpRequest, 0);
    embeddedChannel.writeOutbound(nettyRequest.createResponse(RestStatus.OK, new BytesArray("content")));
    return embeddedChannel.readOutbound();
}