Search in sources :

Example 26 with SessionContext

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

the class ZuulMessageImplTest method testMultiSetBodyGetBody.

@Test
public void testMultiSetBodyGetBody() {
    final ZuulMessage msg = new ZuulMessageImpl(new SessionContext(), new Headers());
    msg.setBody(TEXT1.getBytes());
    String body = new String(msg.getBody());
    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"));
    msg.setBody(TEXT2.getBytes());
    body = new String(msg.getBody());
    assertTrue(msg.hasBody());
    assertTrue(msg.hasCompleteBody());
    assertEquals(TEXT2, body);
    assertEquals(1, msg.getHeaders().getAll("Content-Length").size());
    assertEquals(String.valueOf(TEXT2.length()), msg.getHeaders().getFirst("Content-Length"));
}
Also used : SessionContext(com.netflix.zuul.context.SessionContext) Test(org.junit.Test)

Example 27 with SessionContext

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

the class ZuulMessageImplTest method testBufferBody3GetBodyAsText.

@Test
public void testBufferBody3GetBodyAsText() {
    final ZuulMessage msg = new ZuulMessageImpl(new SessionContext(), new Headers());
    msg.bufferBodyContents(new DefaultHttpContent(Unpooled.copiedBuffer("Hello ".getBytes())));
    msg.bufferBodyContents(new DefaultHttpContent(Unpooled.copiedBuffer("World!".getBytes())));
    msg.bufferBodyContents(new DefaultLastHttpContent());
    final String body = msg.getBodyAsText();
    assertTrue(msg.hasBody());
    assertTrue(msg.hasCompleteBody());
    assertEquals("Hello World!", body);
    assertEquals(0, msg.getHeaders().getAll("Content-Length").size());
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) SessionContext(com.netflix.zuul.context.SessionContext) Test(org.junit.Test)

Example 28 with SessionContext

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

the class HttpRequestMessageImplTest method getOriginalPort_fallsBackOnUnbracketedIpv6Address.

@Test
public void getOriginalPort_fallsBackOnUnbracketedIpv6Address() throws URISyntaxException {
    Headers headers = new Headers();
    headers.add("Host", "ba::33");
    assertEquals(9999, HttpRequestMessageImpl.getOriginalPort(new SessionContext(), headers, 9999));
}
Also used : Headers(com.netflix.zuul.message.Headers) SessionContext(com.netflix.zuul.context.SessionContext) Test(org.junit.Test)

Example 29 with SessionContext

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

the class HttpRequestMessageImplTest method testOriginalRequestInfo.

@Test
public void testOriginalRequestInfo() {
    HttpQueryParams queryParams = new HttpQueryParams();
    queryParams.add("flag", "5");
    Headers headers = new Headers();
    headers.add("Host", "blah.netflix.com");
    request = new HttpRequestMessageImpl(new SessionContext(), "HTTP/1.1", "POST", "/some/where", queryParams, headers, "192.168.0.2", "https", 7002, "localhost", new LocalAddress("777"), false);
    request.storeInboundRequest();
    HttpRequestInfo originalRequest = request.getInboundRequest();
    Assert.assertEquals(request.getPort(), originalRequest.getPort());
    Assert.assertEquals(request.getPath(), originalRequest.getPath());
    Assert.assertEquals(request.getQueryParams().getFirst("flag"), originalRequest.getQueryParams().getFirst("flag"));
    Assert.assertEquals(request.getHeaders().getFirst("Host"), originalRequest.getHeaders().getFirst("Host"));
    request.setPort(8080);
    request.setPath("/another/place");
    request.getQueryParams().set("flag", "20");
    request.getHeaders().set("Host", "wah.netflix.com");
    Assert.assertEquals(7002, originalRequest.getPort());
    Assert.assertEquals("/some/where", originalRequest.getPath());
    Assert.assertEquals("5", originalRequest.getQueryParams().getFirst("flag"));
    Assert.assertEquals("blah.netflix.com", originalRequest.getHeaders().getFirst("Host"));
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) Headers(com.netflix.zuul.message.Headers) SessionContext(com.netflix.zuul.context.SessionContext) Test(org.junit.Test)

Example 30 with SessionContext

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

the class HttpRequestMessageImplTest method testReconstructURI_immutable.

@Test
public void testReconstructURI_immutable() {
    HttpQueryParams queryParams = new HttpQueryParams();
    queryParams.add("flag", "5");
    Headers headers = new Headers();
    headers.add("Host", "blah.netflix.com");
    request = new HttpRequestMessageImpl(new SessionContext(), "HTTP/1.1", "POST", "/some/where", queryParams, headers, "192.168.0.2", "https", 7002, "localhost", new SocketAddress() {
    }, true);
    // Check it's the same value 2nd time.
    Assert.assertEquals("https://blah.netflix.com:7002/some/where?flag=5", request.reconstructURI());
    Assert.assertEquals("https://blah.netflix.com:7002/some/where?flag=5", request.reconstructURI());
    // Check that cached on 1st usage.
    request = new HttpRequestMessageImpl(new SessionContext(), "HTTP/1.1", "POST", "/some/where", queryParams, headers, "192.168.0.2", "https", 7002, "localhost", new SocketAddress() {
    }, true);
    request = spy(request);
    when(request._reconstructURI()).thenReturn("http://testhost/blah");
    verify(request, times(1))._reconstructURI();
    Assert.assertEquals("http://testhost/blah", request.reconstructURI());
    Assert.assertEquals("http://testhost/blah", request.reconstructURI());
    // Check that throws exception if we try to mutate it.
    try {
        request.setPath("/new-path");
        fail();
    } catch (IllegalStateException e) {
        assertTrue(true);
    }
}
Also used : Headers(com.netflix.zuul.message.Headers) SessionContext(com.netflix.zuul.context.SessionContext) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) 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