Search in sources :

Example 36 with RequestContext

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

the class SendErrorFilterTests method setResponseCode.

@Test
public void setResponseCode() {
    SendErrorFilter filter = createSendErrorFilter(new MockHttpServletRequest());
    filter.run();
    RequestContext ctx = RequestContext.getCurrentContext();
    int resCode = ctx.getResponse().getStatus();
    int ctxCode = ctx.getResponseStatusCode();
    assertEquals("invalid response code: " + resCode, HttpStatus.NOT_FOUND.value(), resCode);
    assertEquals("invalid response code in RequestContext: " + ctxCode, resCode, ctxCode);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(com.netflix.zuul.context.RequestContext) Test(org.junit.Test)

Example 37 with RequestContext

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

the class SendErrorFilterTests method setTestRequestcontext.

@Before
public void setTestRequestcontext() {
    MonitoringHelper.initMocks();
    RequestContext context = new RequestContext();
    RequestContext.testSetCurrentContext(context);
}
Also used : RequestContext(com.netflix.zuul.context.RequestContext) Before(org.junit.Before)

Example 38 with RequestContext

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

the class SendErrorFilterTests method createSendErrorFilter.

private SendErrorFilter createSendErrorFilter(HttpServletRequest request) {
    RequestContext context = new RequestContext();
    context.setRequest(request);
    context.setResponse(new MockHttpServletResponse());
    context.setThrowable(new ZuulException(new RuntimeException(), HttpStatus.NOT_FOUND.value(), null));
    RequestContext.testSetCurrentContext(context);
    SendErrorFilter filter = new SendErrorFilter();
    filter.setErrorPath("/error");
    return filter;
}
Also used : ZuulException(com.netflix.zuul.exception.ZuulException) RequestContext(com.netflix.zuul.context.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 39 with RequestContext

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

the class SendResponseFilterTests method closeResponseOutputStreamError.

@Test
public void closeResponseOutputStreamError() 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);
    SendResponseFilter filter = new SendResponseFilter();
    ServletOutputStream zuuloutputstream = mock(ServletOutputStream.class);
    doThrow(new IOException("Response to client closed")).when(zuuloutputstream).write(isA(byte[].class), anyInt(), anyInt());
    when(response.getOutputStream()).thenReturn(zuuloutputstream);
    try {
        filter.run();
    } catch (UndeclaredThrowableException ex) {
        assertThat(ex.getUndeclaredThrowable().getMessage(), is("Response to client closed"));
    }
    verify(zuulResponse).close();
    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) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestContext(com.netflix.zuul.context.RequestContext) IOException(java.io.IOException) Test(org.junit.Test)

Example 40 with RequestContext

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

the class SendResponseFilterTests method setTestRequestcontext.

@Before
public void setTestRequestcontext() {
    RequestContext context = new RequestContext();
    RequestContext.testSetCurrentContext(context);
}
Also used : RequestContext(com.netflix.zuul.context.RequestContext) Before(org.junit.Before)

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