Search in sources :

Example 41 with RequestContext

use of com.netflix.zuul.context.RequestContext in project spring-cloud-netflix by spring-cloud.

the class SendResponseFilterTests method testCloseResponseDataStream.

@Test
public void testCloseResponseDataStream() throws Exception {
    HttpServletResponse response = mock(HttpServletResponse.class);
    InputStream mockStream = spy(new ByteArrayInputStream("Hello\n".getBytes("UTF-8")));
    RequestContext context = new RequestContext();
    context.setRequest(new MockHttpServletRequest());
    context.setResponse(response);
    context.setResponseDataStream(mockStream);
    Closeable zuulResponse = mock(Closeable.class);
    context.set("zuulResponse", zuulResponse);
    RequestContext.testSetCurrentContext(context);
    when(response.getOutputStream()).thenReturn(mock(ServletOutputStream.class));
    SendResponseFilter filter = new SendResponseFilter();
    filter.run();
    verify(mockStream).close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ServletOutputStream(javax.servlet.ServletOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Closeable(java.io.Closeable) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestContext(com.netflix.zuul.context.RequestContext) Test(org.junit.Test)

Example 42 with RequestContext

use of com.netflix.zuul.context.RequestContext in project spring-cloud-netflix by spring-cloud.

the class PreDecorationFilterTests method xForwardedPortOnlyAppends.

@Test
public void xForwardedPortOnlyAppends() throws Exception {
    this.properties.setPrefix("/api");
    this.request.setRequestURI("/api/foo/1");
    this.request.setRemoteAddr("5.6.7.8");
    this.request.setServerPort(8080);
    this.request.addHeader("X-Forwarded-Port", "456");
    this.routeLocator.addRoute(new ZuulRoute("foo", "/foo/**", "foo", null, false, null, null));
    this.filter.run();
    RequestContext ctx = RequestContext.getCurrentContext();
    assertEquals("localhost:8080", ctx.getZuulRequestHeaders().get("x-forwarded-host"));
    assertEquals("456,8080", ctx.getZuulRequestHeaders().get("x-forwarded-port"));
    assertEquals("http", ctx.getZuulRequestHeaders().get("x-forwarded-proto"));
}
Also used : ZuulRoute(org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute) RequestContext(com.netflix.zuul.context.RequestContext) Test(org.junit.Test)

Example 43 with RequestContext

use of com.netflix.zuul.context.RequestContext in project spring-cloud-netflix by spring-cloud.

the class PreDecorationFilterTests method setTestRequestContext.

private void setTestRequestContext() {
    RequestContext context = new RequestContext();
    RequestContext.testSetCurrentContext(context);
}
Also used : RequestContext(com.netflix.zuul.context.RequestContext)

Example 44 with RequestContext

use of com.netflix.zuul.context.RequestContext in project spring-cloud-netflix by spring-cloud.

the class PreDecorationFilterTests method xForwardedProtoHttpOnlyAppends.

@Test
public void xForwardedProtoHttpOnlyAppends() throws Exception {
    this.properties.setPrefix("/api");
    this.request.setRequestURI("/api/foo/1");
    this.request.setRemoteAddr("5.6.7.8");
    this.request.setServerPort(8080);
    this.request.addHeader("X-Forwarded-Proto", "http");
    this.routeLocator.addRoute(new ZuulRoute("foo", "/foo/**", "foo", null, false, null, null));
    this.filter.run();
    RequestContext ctx = RequestContext.getCurrentContext();
    assertEquals("localhost:8080", ctx.getZuulRequestHeaders().get("x-forwarded-host"));
    assertEquals("80,8080", ctx.getZuulRequestHeaders().get("x-forwarded-port"));
    assertEquals("http,http", ctx.getZuulRequestHeaders().get("x-forwarded-proto"));
}
Also used : ZuulRoute(org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute) RequestContext(com.netflix.zuul.context.RequestContext) Test(org.junit.Test)

Example 45 with RequestContext

use of com.netflix.zuul.context.RequestContext in project spring-cloud-netflix by spring-cloud.

the class PreDecorationFilterTests method hostHeaderSet.

@Test
public void hostHeaderSet() throws Exception {
    this.properties.setPrefix("/api");
    this.properties.setAddHostHeader(true);
    this.request.setRequestURI("/api/foo/1");
    this.request.setRemoteAddr("5.6.7.8");
    this.request.setServerPort(8080);
    this.routeLocator.addRoute(new ZuulRoute("foo", "/foo/**", "foo", null, false, null, null));
    this.filter.run();
    RequestContext ctx = RequestContext.getCurrentContext();
    assertEquals("localhost:8080", ctx.getZuulRequestHeaders().get("x-forwarded-host"));
    assertEquals("localhost:8080", ctx.getZuulRequestHeaders().get("host"));
}
Also used : ZuulRoute(org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute) RequestContext(com.netflix.zuul.context.RequestContext) Test(org.junit.Test)

Aggregations

RequestContext (com.netflix.zuul.context.RequestContext)163 Test (org.junit.Test)59 HttpServletRequest (javax.servlet.http.HttpServletRequest)27 ZuulRoute (org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute)24 Before (org.junit.Before)23 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)22 Set (java.util.Set)10 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)9 ZuulException (com.netflix.zuul.exception.ZuulException)7 InputStream (java.io.InputStream)7 HashSet (java.util.HashSet)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 Route (org.springframework.cloud.netflix.zuul.filters.Route)7 ZuulProperties (org.springframework.cloud.netflix.zuul.filters.ZuulProperties)7 IOException (java.io.IOException)6 HttpServletRequestWrapper (com.netflix.zuul.http.HttpServletRequestWrapper)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 List (java.util.List)4 HttpHeaders (org.springframework.http.HttpHeaders)4 RateLimitCheck (com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck)3