Search in sources :

Example 1 with DefaultMaxBytesRecvByteBufAllocator

use of io.netty.channel.DefaultMaxBytesRecvByteBufAllocator in project ambry by linkedin.

the class CopyForcingByteBuf method conversionWithGoodInputTest.

/**
 * Tests conversion of {@link HttpRequest} to {@link NettyRequest} given good input.
 * @throws RestServiceException
 */
@Test
public void conversionWithGoodInputTest() throws RestServiceException, CertificateException, SSLException {
    // headers
    HttpHeaders headers = new DefaultHttpHeaders(false);
    headers.add(HttpHeaderNames.CONTENT_LENGTH, new Random().nextInt(Integer.MAX_VALUE));
    headers.add("headerKey", "headerValue1");
    headers.add("headerKey", "headerValue2");
    headers.add("overLoadedKey", "headerOverloadedValue");
    headers.add("paramNoValueInUriButValueInHeader", "paramValueInHeader");
    // params
    Map<String, List<String>> params = new HashMap<String, List<String>>();
    List<String> values = new ArrayList<String>(2);
    values.add("paramValue1");
    values.add("paramValue2");
    params.put("paramKey", values);
    values = new ArrayList<String>(1);
    values.add("paramOverloadedValue");
    params.put("overLoadedKey", values);
    params.put("paramNoValue", null);
    params.put("paramNoValueInUriButValueInHeader", null);
    params.put(BLACKLISTED_QUERY_PARAM, values);
    StringBuilder uriAttachmentBuilder = new StringBuilder("?");
    for (Map.Entry<String, List<String>> param : params.entrySet()) {
        if (param.getValue() != null) {
            for (String value : param.getValue()) {
                uriAttachmentBuilder.append(param.getKey()).append("=").append(value).append("&");
            }
        } else {
            uriAttachmentBuilder.append(param.getKey()).append("&");
        }
    }
    uriAttachmentBuilder.deleteCharAt(uriAttachmentBuilder.length() - 1);
    String uriAttachment = uriAttachmentBuilder.toString();
    NettyRequest nettyRequest;
    String uri;
    Set<Cookie> cookies = new HashSet<>();
    Cookie httpCookie = new DefaultCookie("CookieKey1", "CookieValue1");
    cookies.add(httpCookie);
    httpCookie = new DefaultCookie("CookieKey2", "CookieValue2");
    cookies.add(httpCookie);
    headers.add(RestUtils.Headers.COOKIE, getCookiesHeaderValue(cookies));
    for (MockChannel channel : Arrays.asList(new MockChannel(), new MockChannel().addSslHandlerToPipeline())) {
        uri = "/GET" + uriAttachment;
        nettyRequest = createNettyRequest(HttpMethod.GET, uri, headers, channel);
        validateRequest(nettyRequest, RestMethod.GET, uri, headers, params, cookies, channel);
        closeRequestAndValidate(nettyRequest, channel);
        RecvByteBufAllocator savedAllocator = channel.config().getRecvByteBufAllocator();
        int[] bufferWatermarks = { -1, 0, 1, DEFAULT_WATERMARK };
        for (int bufferWatermark : bufferWatermarks) {
            NettyRequest.bufferWatermark = bufferWatermark;
            uri = "/POST" + uriAttachment;
            nettyRequest = createNettyRequest(HttpMethod.POST, uri, headers, channel);
            validateRequest(nettyRequest, RestMethod.POST, uri, headers, params, cookies, channel);
            if (bufferWatermark > 0) {
                assertTrue("RecvAllocator should have changed", channel.config().getRecvByteBufAllocator() instanceof DefaultMaxBytesRecvByteBufAllocator);
            } else {
                assertEquals("RecvAllocator not as expected", savedAllocator, channel.config().getRecvByteBufAllocator());
            }
            closeRequestAndValidate(nettyRequest, channel);
            assertEquals("Allocator not as expected", savedAllocator, channel.config().getRecvByteBufAllocator());
        }
        for (int bufferWatermark : bufferWatermarks) {
            NettyRequest.bufferWatermark = bufferWatermark;
            uri = "/PUT" + uriAttachment;
            nettyRequest = createNettyRequest(HttpMethod.PUT, uri, headers, channel);
            validateRequest(nettyRequest, RestMethod.PUT, uri, headers, params, cookies, channel);
            if (bufferWatermark > 0) {
                assertTrue("RecvAllocator should have changed", channel.config().getRecvByteBufAllocator() instanceof DefaultMaxBytesRecvByteBufAllocator);
            } else {
                assertEquals("RecvAllocator not as expected", savedAllocator, channel.config().getRecvByteBufAllocator());
            }
            closeRequestAndValidate(nettyRequest, channel);
            assertEquals("Allocator not as expected", savedAllocator, channel.config().getRecvByteBufAllocator());
        }
        NettyRequest.bufferWatermark = DEFAULT_WATERMARK;
        uri = "/DELETE" + uriAttachment;
        nettyRequest = createNettyRequest(HttpMethod.DELETE, uri, headers, channel);
        validateRequest(nettyRequest, RestMethod.DELETE, uri, headers, params, cookies, channel);
        closeRequestAndValidate(nettyRequest, channel);
        uri = "/HEAD" + uriAttachment;
        nettyRequest = createNettyRequest(HttpMethod.HEAD, uri, headers, channel);
        validateRequest(nettyRequest, RestMethod.HEAD, uri, headers, params, cookies, channel);
        closeRequestAndValidate(nettyRequest, channel);
        uri = "/OPTIONS" + uriAttachment;
        nettyRequest = createNettyRequest(HttpMethod.OPTIONS, uri, headers, channel);
        validateRequest(nettyRequest, RestMethod.OPTIONS, uri, headers, params, cookies, channel);
        closeRequestAndValidate(nettyRequest, channel);
    }
}
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) DefaultMaxBytesRecvByteBufAllocator(io.netty.channel.DefaultMaxBytesRecvByteBufAllocator) RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DefaultMaxBytesRecvByteBufAllocator(io.netty.channel.DefaultMaxBytesRecvByteBufAllocator) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) Random(java.util.Random) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

DefaultMaxBytesRecvByteBufAllocator (io.netty.channel.DefaultMaxBytesRecvByteBufAllocator)1 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)1 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 Cookie (io.netty.handler.codec.http.cookie.Cookie)1 DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Random (java.util.Random)1 Test (org.junit.Test)1