Search in sources :

Example 6 with SessionContext

use of com.netflix.zuul.context.SessionContext in project zuul by Netflix.

the class ZuulFilterChainRunnerTest method before.

@Before
public void before() {
    SessionContext context = new SessionContext();
    Headers headers = new Headers();
    ChannelHandlerContext chc = mock(ChannelHandlerContext.class);
    when(chc.executor()).thenReturn(ImmediateEventExecutor.INSTANCE);
    context.put(NETTY_SERVER_CHANNEL_HANDLER_CONTEXT, chc);
    request = new HttpRequestMessageImpl(context, "http", "GET", "/foo/bar", new HttpQueryParams(), headers, "127.0.0.1", "http", 8080, "server123");
    request.storeInboundRequest();
    response = new HttpResponseMessageImpl(context, request, 200);
}
Also used : Headers(com.netflix.zuul.message.Headers) HttpQueryParams(com.netflix.zuul.message.http.HttpQueryParams) HttpResponseMessageImpl(com.netflix.zuul.message.http.HttpResponseMessageImpl) SessionContext(com.netflix.zuul.context.SessionContext) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpRequestMessageImpl(com.netflix.zuul.message.http.HttpRequestMessageImpl) Before(org.junit.Before)

Example 7 with SessionContext

use of com.netflix.zuul.context.SessionContext in project zuul by Netflix.

the class HttpUtilsTest method getBodySizeIfKnown_returnsResponseBodySize.

@Test
public void getBodySizeIfKnown_returnsResponseBodySize() {
    SessionContext context = new SessionContext();
    Headers headers = new Headers();
    HttpQueryParams queryParams = new HttpQueryParams();
    HttpRequestMessage request = new HttpRequestMessageImpl(context, "http", "GET", "/path", queryParams, headers, "127.0.0.1", "scheme", 6666, "server-name");
    request.storeInboundRequest();
    HttpResponseMessage response = new HttpResponseMessageImpl(context, request, 200);
    response.setBodyAsText("Hello world");
    assertThat(HttpUtils.getBodySizeIfKnown(response)).isEqualTo(Integer.valueOf(11));
}
Also used : HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) Headers(com.netflix.zuul.message.Headers) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) HttpQueryParams(com.netflix.zuul.message.http.HttpQueryParams) HttpResponseMessageImpl(com.netflix.zuul.message.http.HttpResponseMessageImpl) SessionContext(com.netflix.zuul.context.SessionContext) HttpRequestMessageImpl(com.netflix.zuul.message.http.HttpRequestMessageImpl) Test(org.junit.Test)

Example 8 with SessionContext

use of com.netflix.zuul.context.SessionContext in project zuul by Netflix.

the class ClientRequestReceiverTest method multipleHostHeaders_setBadRequestStatus.

@Test
public void multipleHostHeaders_setBadRequestStatus() {
    ClientRequestReceiver receiver = new ClientRequestReceiver(null);
    EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestEncoder());
    PassportLoggingHandler loggingHandler = new PassportLoggingHandler(new DefaultRegistry());
    // Required for messages
    channel.attr(SourceAddressChannelHandler.ATTR_SERVER_LOCAL_PORT).set(1234);
    channel.pipeline().addLast(new HttpServerCodec());
    channel.pipeline().addLast(receiver);
    channel.pipeline().addLast(loggingHandler);
    HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/post");
    httpRequest.headers().add("Host", "foo.bar.com");
    httpRequest.headers().add("Host", "bar.foo.com");
    channel.writeOutbound(httpRequest);
    ByteBuf byteBuf = channel.readOutbound();
    channel.writeInbound(byteBuf);
    channel.readInbound();
    channel.close();
    HttpRequestMessage request = ClientRequestReceiver.getRequestFromChannel(channel);
    SessionContext context = request.getContext();
    assertEquals(StatusCategoryUtils.getStatusCategory(context), ZuulStatusCategory.FAILURE_CLIENT_BAD_REQUEST);
    assertEquals("Multiple Host headers", context.getError().getMessage());
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) SessionContext(com.netflix.zuul.context.SessionContext) ByteBuf(io.netty.buffer.ByteBuf) PassportLoggingHandler(com.netflix.zuul.netty.insights.PassportLoggingHandler) Test(org.junit.Test)

Example 9 with SessionContext

use of com.netflix.zuul.context.SessionContext in project zuul by Netflix.

the class ZuulMessageImplTest method testClone.

@Test
public void testClone() {
    SessionContext ctx1 = new SessionContext();
    ctx1.set("k1", "v1");
    Headers headers1 = new Headers();
    headers1.set("k1", "v1");
    ZuulMessage msg1 = new ZuulMessageImpl(ctx1, headers1);
    ZuulMessage msg2 = msg1.clone();
    assertEquals(msg1.getBodyAsText(), msg2.getBodyAsText());
    assertEquals(msg1.getHeaders(), msg2.getHeaders());
    assertEquals(msg1.getContext(), msg2.getContext());
    // Verify that values of the 2 messages are decoupled.
    msg1.getHeaders().set("k1", "v_new");
    msg1.getContext().set("k1", "v_new");
    assertEquals("v1", msg2.getHeaders().getFirst("k1"));
    assertEquals("v1", msg2.getContext().get("k1"));
}
Also used : SessionContext(com.netflix.zuul.context.SessionContext) Test(org.junit.Test)

Example 10 with SessionContext

use of com.netflix.zuul.context.SessionContext in project zuul by Netflix.

the class ZuulMessageImplTest method testSetBodyAsTextGetBodyAsText.

@Test
public void testSetBodyAsTextGetBodyAsText() {
    final ZuulMessage msg = new ZuulMessageImpl(new SessionContext(), new Headers());
    msg.setBodyAsText(TEXT1);
    final String body = msg.getBodyAsText();
    assertTrue(msg.hasBody());
    assertTrue(msg.hasCompleteBody());
    assertEquals(TEXT1, body);
    assertEquals(1, msg.getHeaders().getAll("Content-Length").size());
    assertEquals(String.valueOf(TEXT1.length()), msg.getHeaders().getFirst("Content-Length"));
}
Also used : SessionContext(com.netflix.zuul.context.SessionContext) Test(org.junit.Test)

Aggregations

SessionContext (com.netflix.zuul.context.SessionContext)43 Test (org.junit.Test)23 Headers (com.netflix.zuul.message.Headers)16 HttpRequestMessage (com.netflix.zuul.message.http.HttpRequestMessage)6 HttpResponseMessage (com.netflix.zuul.message.http.HttpResponseMessage)5 HttpResponseMessageImpl (com.netflix.zuul.message.http.HttpResponseMessageImpl)5 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)5 HttpQueryParams (com.netflix.zuul.message.http.HttpQueryParams)4 InetSocketAddress (java.net.InetSocketAddress)4 ClientException (com.netflix.client.ClientException)3 ZuulException (com.netflix.zuul.exception.ZuulException)3 HttpRequestMessageImpl (com.netflix.zuul.message.http.HttpRequestMessageImpl)3 StatusCategory (com.netflix.zuul.stats.status.StatusCategory)3 ByteBuf (io.netty.buffer.ByteBuf)3 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)3 ErrorType (com.netflix.zuul.exception.ErrorType)2 OutboundException (com.netflix.zuul.exception.OutboundException)2 ZuulMessage (com.netflix.zuul.message.ZuulMessage)2 ZuulMessageImpl (com.netflix.zuul.message.ZuulMessageImpl)2 CurrentPassport (com.netflix.zuul.passport.CurrentPassport)2