-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
netty more extractions are implemented (#997)
Co-authored-by: sokomishalov <[email protected]>
- Loading branch information
1 parent
c9d3feb
commit c1ed50d
Showing
4 changed files
with
108 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
logbook-netty/src/test/java/org/zalando/logbook/netty/RequestUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.zalando.logbook.netty; | ||
|
||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelPipeline; | ||
import io.netty.channel.local.LocalAddress; | ||
import io.netty.handler.codec.http.DefaultHttpHeaders; | ||
import io.netty.handler.codec.http.HttpMethod; | ||
import io.netty.handler.codec.http.HttpRequest; | ||
import io.netty.handler.codec.http.HttpVersion; | ||
import io.netty.handler.ssl.SslHandler; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE; | ||
import static io.netty.handler.codec.http.HttpHeaderNames.HOST; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.zalando.logbook.Origin.LOCAL; | ||
import static org.zalando.logbook.Origin.REMOTE; | ||
|
||
/** | ||
* @author sokomishalov | ||
*/ | ||
public class RequestUnitTest { | ||
|
||
@Test | ||
void shouldBeDefaultRequest() { | ||
HttpRequest req = mock(HttpRequest.class); | ||
when(req.uri()).thenReturn("/test"); | ||
when(req.headers()).thenReturn(new DefaultHttpHeaders().add(CONTENT_TYPE, "text/plain")); | ||
when(req.method()).thenReturn(HttpMethod.GET); | ||
when(req.protocolVersion()).thenReturn(HttpVersion.HTTP_1_1); | ||
|
||
ChannelHandlerContext context = mock(ChannelHandlerContext.class); | ||
|
||
when(context.channel()).thenReturn(mock(Channel.class)); | ||
when(context.channel().pipeline()).thenReturn(mock(ChannelPipeline.class)); | ||
when(context.channel().pipeline().get(SslHandler.class)).thenReturn(mock(SslHandler.class)); | ||
|
||
when(context.channel().remoteAddress()).thenReturn(LocalAddress.ANY); | ||
|
||
Request remoteRequest = new Request(context, REMOTE, req); | ||
|
||
assertThat(remoteRequest.getScheme()).isEqualTo("https"); | ||
assertThat(remoteRequest.getHost()).isEqualTo("unknown"); | ||
assertThat(remoteRequest.getPort()).isEmpty(); | ||
assertThat(remoteRequest.getContentType()).isEqualTo("text/plain"); | ||
assertThat(remoteRequest.getCharset()).isEqualTo(UTF_8); | ||
assertThat(remoteRequest.getRemote()).isEqualTo("local:any"); | ||
assertThat(remoteRequest.getRequestUri()).isEqualTo("https://unknown/test"); | ||
assertThat(remoteRequest.getOrigin()).isEqualTo(REMOTE); | ||
assertThat(remoteRequest.getMethod()).isEqualTo("GET"); | ||
assertThat(remoteRequest.getPath()).isEqualTo("/test"); | ||
assertThat(remoteRequest.getProtocolVersion()).isEqualTo("HTTP/1.1"); | ||
|
||
|
||
when(req.headers()).thenReturn(new DefaultHttpHeaders().add(HOST, "localhost")); | ||
Request localRequest = new Request(context, LOCAL, req); | ||
|
||
assertThat(localRequest.getHost()).isEqualTo("localhost"); | ||
} | ||
} |