Search in sources :

Example 1 with HttpState

use of org.mockserver.mock.HttpState in project mockserver by mock-server.

the class MockServerLoggerTest method shouldSendEventToStateHandler.

@Test
public void shouldSendEventToStateHandler() {
    Level originalLevel = logLevel();
    try {
        // given
        logLevel("INFO");
        HttpState mockHttpStateHandler = mock(HttpState.class);
        MockServerLogger logFormatter = new MockServerLogger(mockHttpStateHandler);
        HttpRequest request = request("some_path");
        // when
        logFormatter.logEvent(new LogEntry().setLogLevel(Level.INFO).setHttpRequest(request).setMessageFormat("some random message with{}and{}").setArguments("some" + NEW_LINE + "multi-line" + NEW_LINE + "object", "another" + NEW_LINE + "multi-line" + NEW_LINE + "object"));
        // then
        ArgumentCaptor<LogEntry> captor = ArgumentCaptor.forClass(LogEntry.class);
        verify(mockHttpStateHandler, times(1)).log(captor.capture());
        LogEntry messageLogEntry = captor.getValue();
        assertThat(messageLogEntry.getHttpRequests(), is(new HttpRequest[] { request }));
        assertThat(messageLogEntry.getMessage(), containsString("some random message with" + NEW_LINE + NEW_LINE + "  some" + NEW_LINE + "  multi-line" + NEW_LINE + "  object" + NEW_LINE + NEW_LINE + " and" + NEW_LINE + NEW_LINE + "  another" + NEW_LINE + "  multi-line" + NEW_LINE + "  object" + NEW_LINE));
        assertThat(messageLogEntry.getMessageFormat(), containsString("some random message with{}and{}"));
        assertThat(messageLogEntry.getArguments(), arrayContaining(new Object[] { "some" + NEW_LINE + "multi-line" + NEW_LINE + "object", "another" + NEW_LINE + "multi-line" + NEW_LINE + "object" }));
    } finally {
        logLevel(originalLevel.toString());
    }
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) HttpState(org.mockserver.mock.HttpState) Level(org.slf4j.event.Level) ConfigurationProperties.logLevel(org.mockserver.configuration.ConfigurationProperties.logLevel) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 2 with HttpState

use of org.mockserver.mock.HttpState in project mockserver by mock-server.

the class DirectProxyUnificationHandlerTest method shouldSupportUnknownProtocol.

@Test
public void shouldSupportUnknownProtocol() {
    // given
    EmbeddedChannel embeddedChannel = new EmbeddedChannel(new MockServerUnificationInitializer(configuration(), mock(LifeCycle.class), new HttpState(configuration(), new MockServerLogger(), mock(Scheduler.class)), mock(HttpActionHandler.class), null));
    // and - channel open
    assertThat(embeddedChannel.isOpen(), is(true));
    // when - basic HTTP request
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer("UNKNOWN_PROTOCOL".getBytes(UTF_8)));
    // then - should add no handlers
    assertThat(embeddedChannel.pipeline().names(), contains("DefaultChannelPipeline$TailContext#0"));
    // and - close channel
    assertThat(embeddedChannel.isOpen(), is(false));
}
Also used : MockServerLogger(org.mockserver.logging.MockServerLogger) MockServerUnificationInitializer(org.mockserver.netty.MockServerUnificationInitializer) Scheduler(org.mockserver.scheduler.Scheduler) HttpState(org.mockserver.mock.HttpState) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 3 with HttpState

use of org.mockserver.mock.HttpState in project mockserver by mock-server.

the class DirectProxyUnificationHandlerTest method shouldSwitchToSsl.

@Test
public void shouldSwitchToSsl() {
    // given
    EmbeddedChannel embeddedChannel = new EmbeddedChannel(new MockServerUnificationInitializer(configuration(), mock(LifeCycle.class), new HttpState(configuration(), new MockServerLogger(), mock(Scheduler.class)), mock(HttpActionHandler.class), null));
    // and - no SSL handler
    assertThat(embeddedChannel.pipeline().get(SslHandler.class), is(nullValue()));
    // when - first part of a 5-byte handshake message
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(new byte[] { // handshake
    22, // major version
    3, 1, 0, // package length (5-byte)
    5 }));
    // then - should add SSL handlers first
    if (MockServerLogger.isEnabled(TRACE)) {
        assertThat(String.valueOf(embeddedChannel.pipeline().names()), embeddedChannel.pipeline().names(), contains("SniHandler#0", "LoggingHandler#0", "PortUnificationHandler#0", "DefaultChannelPipeline$TailContext#0"));
    } else {
        assertThat(String.valueOf(embeddedChannel.pipeline().names()), embeddedChannel.pipeline().names(), contains("SniHandler#0", "PortUnificationHandler#0", "DefaultChannelPipeline$TailContext#0"));
    }
}
Also used : MockServerLogger(org.mockserver.logging.MockServerLogger) MockServerUnificationInitializer(org.mockserver.netty.MockServerUnificationInitializer) Scheduler(org.mockserver.scheduler.Scheduler) HttpState(org.mockserver.mock.HttpState) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) SslHandler(io.netty.handler.ssl.SslHandler) Test(org.junit.Test)

Example 4 with HttpState

use of org.mockserver.mock.HttpState in project mockserver by mock-server.

the class HttpProxyUnificationInitializerSOCKSErrorTest method shouldSwitchToHttp.

@Test
public void shouldSwitchToHttp() {
    // given
    EmbeddedChannel embeddedChannel = new EmbeddedChannel();
    embeddedChannel.pipeline().addLast(new MockServerUnificationInitializer(configuration(), mock(LifeCycle.class), new HttpState(configuration(), new MockServerLogger(), mock(Scheduler.class)), mock(HttpActionHandler.class), null));
    // and - no HTTP handlers
    assertThat(embeddedChannel.pipeline().get(HttpServerCodec.class), is(nullValue()));
    assertThat(embeddedChannel.pipeline().get(HttpContentDecompressor.class), is(nullValue()));
    assertThat(embeddedChannel.pipeline().get(HttpObjectAggregator.class), is(nullValue()));
    // when - basic HTTP request
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer("GET /somePath HTTP/1.1\r\nHost: some.random.host\r\n\r\n".getBytes(UTF_8)));
    // then - should add HTTP handlers last
    if (MockServerLogger.isEnabled(TRACE)) {
        assertThat(String.valueOf(embeddedChannel.pipeline().names()), embeddedChannel.pipeline().names(), contains("LoggingHandler#0", "HttpServerCodec#0", "HttpContentDecompressor#0", "HttpContentLengthRemover#0", "HttpObjectAggregator#0", "CallbackWebSocketServerHandler#0", "DashboardWebSocketHandler#0", "MockServerHttpServerCodec#0", "HttpRequestHandler#0", "DefaultChannelPipeline$TailContext#0"));
    } else {
        assertThat(String.valueOf(embeddedChannel.pipeline().names()), embeddedChannel.pipeline().names(), contains("HttpServerCodec#0", "HttpContentDecompressor#0", "HttpContentLengthRemover#0", "HttpObjectAggregator#0", "CallbackWebSocketServerHandler#0", "DashboardWebSocketHandler#0", "MockServerHttpServerCodec#0", "HttpRequestHandler#0", "DefaultChannelPipeline$TailContext#0"));
    }
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) MockServerLogger(org.mockserver.logging.MockServerLogger) MockServerUnificationInitializer(org.mockserver.netty.MockServerUnificationInitializer) Scheduler(org.mockserver.scheduler.Scheduler) HttpState(org.mockserver.mock.HttpState) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) Test(org.junit.Test)

Example 5 with HttpState

use of org.mockserver.mock.HttpState in project mockserver by mock-server.

the class HttpProxyUnificationInitializerSOCKSErrorTest method shouldHandleErrorsDuringSOCKSConnection.

@Test
public void shouldHandleErrorsDuringSOCKSConnection() throws DecoderException {
    // given - embedded channel
    short localPort = 1234;
    final LifeCycle lifeCycle = mock(LifeCycle.class);
    when(lifeCycle.getScheduler()).thenReturn(mock(Scheduler.class));
    EmbeddedChannel embeddedChannel = new EmbeddedChannel(new MockServerUnificationInitializer(configuration(), lifeCycle, new HttpState(configuration(), new MockServerLogger(), mock(Scheduler.class)), mock(HttpActionHandler.class), null));
    // and - no SOCKS handlers
    assertThat(embeddedChannel.pipeline().get(Socks5ProxyHandler.class), is(nullValue()));
    assertThat(embeddedChannel.pipeline().get(SocksMessageEncoder.class), is(nullValue()));
    assertThat(embeddedChannel.pipeline().get(SocksInitRequestDecoder.class), is(nullValue()));
    // when - SOCKS INIT message
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(new byte[] { // SOCKS5
    (byte) 0x05, // 1 authentication method
    (byte) 0x02, // NO_AUTH
    (byte) 0x00, // AUTH_PASSWORD
    (byte) 0x02 }));
    // then - INIT response
    assertThat(ByteBufUtil.hexDump((ByteBuf) embeddedChannel.readOutbound()), is(Hex.encodeHexString(new byte[] { // SOCKS5
    (byte) 0x05, // NO_AUTH
    (byte) 0x00 })));
    // and then - should add SOCKS handlers first
    if (MockServerLogger.isEnabled(TRACE)) {
        assertThat(String.valueOf(embeddedChannel.pipeline().names()), embeddedChannel.pipeline().names(), contains("LoggingHandler#0", "Socks5CommandRequestDecoder#0", "Socks5ServerEncoder#0", "Socks5ProxyHandler#0", "PortUnificationHandler#0", "DefaultChannelPipeline$TailContext#0"));
    } else {
        assertThat(String.valueOf(embeddedChannel.pipeline().names()), embeddedChannel.pipeline().names(), contains("Socks5CommandRequestDecoder#0", "Socks5ServerEncoder#0", "Socks5ProxyHandler#0", "PortUnificationHandler#0", "DefaultChannelPipeline$TailContext#0"));
    }
    // and when - SOCKS CONNECT command
    String portInHex = Strings.padStart(BigInteger.valueOf(localPort).toString(16), 4, '0');
    byte[] ipAddressInBytes = NetUtil.createByteArrayFromIpAddressString("127.0.0.1");
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(Bytes.concat(new byte[] { // SOCKS5
    (byte) 0x05, // command type CONNECT
    (byte) 0x01, // reserved (must be 0x00)
    (byte) 0x00, // address type IPv4
    (byte) 0x01 }, // ip address
    ipAddressInBytes, // port
    Hex.decodeHex(portInHex))));
    // then - CONNECT response
    byte[] domainInBytes = "127.0.0.1".getBytes(CharsetUtil.US_ASCII);
    String dominLegnthAndBytes = Strings.padStart(BigInteger.valueOf(domainInBytes.length).toString(16), 2, '0') + new BigInteger(domainInBytes).toString(16);
    assertThat(ByteBufUtil.hexDump((ByteBuf) embeddedChannel.readOutbound()), is(Hex.encodeHexString(new byte[] { // SOCKS5
    (byte) 0x05, // general failure (caused by connection failure)
    (byte) 0x01, // reserved (must be 0x00)
    (byte) 0x00, // address type domain
    (byte) 0x03 }) + // ip address
    dominLegnthAndBytes + // port
    portInHex));
    // then - channel is closed after error
    assertThat(embeddedChannel.isOpen(), is(false));
}
Also used : LifeCycle(org.mockserver.lifecycle.LifeCycle) SocksMessageEncoder(io.netty.handler.codec.socks.SocksMessageEncoder) MockServerLogger(org.mockserver.logging.MockServerLogger) SocksInitRequestDecoder(io.netty.handler.codec.socks.SocksInitRequestDecoder) Scheduler(org.mockserver.scheduler.Scheduler) MockServerUnificationInitializer(org.mockserver.netty.MockServerUnificationInitializer) HttpState(org.mockserver.mock.HttpState) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Socks5ProxyHandler(org.mockserver.netty.proxy.socks.Socks5ProxyHandler) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Aggregations

HttpState (org.mockserver.mock.HttpState)21 MockServerLogger (org.mockserver.logging.MockServerLogger)20 Test (org.junit.Test)16 Scheduler (org.mockserver.scheduler.Scheduler)16 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)12 MockServerUnificationInitializer (org.mockserver.netty.MockServerUnificationInitializer)11 HttpRequest (org.mockserver.model.HttpRequest)5 Before (org.junit.Before)4 WebSocketClientRegistry (org.mockserver.closurecallback.websocketregistry.WebSocketClientRegistry)4 HttpObjectCallback (org.mockserver.model.HttpObjectCallback)4 ResponseWriter (org.mockserver.responsewriter.ResponseWriter)4 ByteBuf (io.netty.buffer.ByteBuf)3 HttpContentDecompressor (io.netty.handler.codec.http.HttpContentDecompressor)3 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)3 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)3 SocksInitRequestDecoder (io.netty.handler.codec.socks.SocksInitRequestDecoder)3 SocksMessageEncoder (io.netty.handler.codec.socks.SocksMessageEncoder)3 HttpActionHandler (org.mockserver.mock.action.http.HttpActionHandler)3 Socks5ProxyHandler (org.mockserver.netty.proxy.socks.Socks5ProxyHandler)3 SslHandler (io.netty.handler.ssl.SslHandler)2