Search in sources :

Example 46 with HttpHeaders

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

the class ChunkedResponseInfoTest method uber_constructor_for_chunked_response_sets_fields_as_expected.

@Test
public void uber_constructor_for_chunked_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
    ChunkedResponseInfo responseInfo = new ChunkedResponseInfo(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.getFinalContentLength(), nullValue());
    assertThat(responseInfo.isPreventCompressedOutput(), is(preventCompressedResponse));
    assertThat(responseInfo.isChunkedResponse(), is(true));
    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 47 with HttpHeaders

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

the class HttpServletResponseWrapperForResponseInfoTest method containsHeader_delegates_to_responseInfo.

@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void containsHeader_delegates_to_responseInfo(boolean containsHeader) {
    // given
    HttpHeaders headersMock = mock(HttpHeaders.class);
    doReturn(headersMock).when(responseInfoMock).getHeaders();
    String headerName = UUID.randomUUID().toString();
    doReturn(containsHeader).when(headersMock).contains(headerName);
    // expect
    assertThat(wrapper.containsHeader(headerName)).isEqualTo(containsHeader);
    verify(headersMock).contains(headerName);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 48 with HttpHeaders

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

the class AccessLogger method logMessageAdditions.

/**
     * @param request
     *     The request to log; will not be null. This is the request that came into the application. WARNING: The
     *     content may have been released already, so you cannot rely on any of the methods that return the payload
     *     content in any form.
     * @param finalResponseObject
     *     The {@link HttpResponse} that was the actual final response object sent to the client; may be null. NOTE:
     *     This should be preferred over {@code responseInfo} whenever both have overlapping data since this argument
     *     may have been modified by outbound handlers after being initially populated by {@code responseInfo}.
     * @param responseInfo
     *     {@link com.nike.riposte.server.http.ResponseInfo} object that was initially used to build {@code
     *     finalResponseObject}; may be null. NOTE: {@code finalResponseObject} may have been modified by other outbound
     *     handlers (e.g. compression/gzip), so if it is non-null it should be preferred over this where possible.
     *
     * @return A list of key/value pairs of data that should be added to the access log message following {@link
     * #combinedLogFormatPrefix(RequestInfo, HttpResponse, ResponseInfo)}. This will include some defaults specified in
     * this method as well as anything returned by {@link #customApplicationLogMessageExtras(RequestInfo, HttpResponse,
     * ResponseInfo, Long)}.
     */
protected List<Pair<String, String>> logMessageAdditions(RequestInfo<?> request, HttpResponse finalResponseObject, ResponseInfo responseInfo, Long elapsedTimeMillis) {
    String httpStatusCode = null;
    String contentLengthResponseHeader = null;
    String transferEncodingResponseHeader = null;
    String errorUid = null;
    String responseTraceId = null;
    String uncompressedRawContentLength = null;
    String finalContentLength = null;
    if (finalResponseObject != null && finalResponseObject.getStatus() != null)
        httpStatusCode = String.valueOf(finalResponseObject.getStatus().code());
    else if (responseInfo != null && responseInfo.getHttpStatusCode() != null)
        httpStatusCode = String.valueOf(responseInfo.getHttpStatusCode());
    HttpHeaders responseHeadersToUse = null;
    if (finalResponseObject != null && finalResponseObject.headers() != null)
        responseHeadersToUse = finalResponseObject.headers();
    else if (responseInfo != null)
        responseHeadersToUse = responseInfo.getHeaders();
    if (responseHeadersToUse != null) {
        contentLengthResponseHeader = responseHeadersToUse.get(CONTENT_LENGTH);
        transferEncodingResponseHeader = responseHeadersToUse.get(TRANSFER_ENCODING);
        errorUid = responseHeadersToUse.get("error_uid");
        responseTraceId = responseHeadersToUse.get(TRACE_ID);
    }
    if (responseInfo != null) {
        if (responseInfo.getUncompressedRawContentLength() != null)
            uncompressedRawContentLength = String.valueOf(responseInfo.getUncompressedRawContentLength());
        if (responseInfo.getFinalContentLength() != null)
            finalContentLength = String.valueOf(responseInfo.getFinalContentLength());
    }
    List<Pair<String, String>> logMessageAdditions = new ArrayList<>();
    logMessageAdditions.addAll(Arrays.asList(Pair.of("accept-Req", request.getHeaders().get(ACCEPT)), Pair.of("content-type-Req", request.getHeaders().get(CONTENT_TYPE)), Pair.of("content-length-Res", contentLengthResponseHeader), Pair.of("transfer_encoding-Res", transferEncodingResponseHeader), Pair.of("http_status_code-Res", httpStatusCode), Pair.of("error_uid-Res", errorUid), Pair.of(TRACE_ENABLED + "-Req", request.getHeaders().get(TRACE_ENABLED)), Pair.of(SPAN_ID + "-Req", request.getHeaders().get(SPAN_ID)), Pair.of(SPAN_NAME + "-Req", request.getHeaders().get(SPAN_NAME)), Pair.of(TRACE_ID + "-Req", request.getHeaders().get(TRACE_ID)), Pair.of(TRACE_ID + "-Res", responseTraceId), Pair.of("raw_content_length-Req", String.valueOf(request.getRawContentLengthInBytes())), Pair.of("raw_content_length-Res", uncompressedRawContentLength), Pair.of("final_content_length-Res", finalContentLength), Pair.of("elapsed_time_millis", String.valueOf(elapsedTimeMillis))));
    List<Pair<String, String>> customApplicationLogMessageExtras = customApplicationLogMessageExtras(request, finalResponseObject, responseInfo, elapsedTimeMillis);
    if (customApplicationLogMessageExtras != null)
        logMessageAdditions.addAll(customApplicationLogMessageExtras);
    return logMessageAdditions;
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) ArrayList(java.util.ArrayList) Pair(com.nike.internal.util.Pair)

Example 49 with HttpHeaders

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

the class HttpServletRequestWrapperForRequestInfoTest method getHeaders_delegates_to_requestInfo_headers.

@Test
public void getHeaders_delegates_to_requestInfo_headers() {
    // given
    HttpHeaders headersMock = mock(HttpHeaders.class);
    String headerKey = UUID.randomUUID().toString();
    List<String> headerVal = Arrays.asList(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    doReturn(headerVal).when(headersMock).getAll(headerKey);
    doReturn(headersMock).when(requestInfoMock).getHeaders();
    // when
    Enumeration<String> result = wrapper.getHeaders(headerKey);
    // then
    verify(headersMock).getAll(headerKey);
    assertThat(Collections.list(result)).isEqualTo(headerVal);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Test(org.junit.Test)

Example 50 with HttpHeaders

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

the class HttpServletRequestWrapperForRequestInfoTest method getHeader_delegates_to_requestInfo_headers.

@Test
public void getHeader_delegates_to_requestInfo_headers() {
    // given
    HttpHeaders headersMock = mock(HttpHeaders.class);
    String headerKey = UUID.randomUUID().toString();
    String headerVal = UUID.randomUUID().toString();
    doReturn(headerVal).when(headersMock).get(headerKey);
    doReturn(headersMock).when(requestInfoMock).getHeaders();
    // when
    String result = wrapper.getHeader(headerKey);
    // then
    verify(headersMock).get(headerKey);
    assertThat(result).isEqualTo(headerVal);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Test(org.junit.Test)

Aggregations

HttpHeaders (io.netty.handler.codec.http.HttpHeaders)128 Test (org.junit.Test)61 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)49 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)42 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)34 AsciiString (io.netty.util.AsciiString)27 Test (org.testng.annotations.Test)25 ByteBuf (io.netty.buffer.ByteBuf)22 HttpServletResponse (javax.servlet.http.HttpServletResponse)17 ChannelPromise (io.netty.channel.ChannelPromise)15 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)12 HttpRequest (io.netty.handler.codec.http.HttpRequest)12 Cookie (io.netty.handler.codec.http.cookie.Cookie)12 DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)12 Map (java.util.Map)12 HttpTest (org.asynchttpclient.testserver.HttpTest)12 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)11 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)11 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)10 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)10