Java Code Examples for com.netflix.zuul.context.RequestContext#setResponse()

The following examples show how to use com.netflix.zuul.context.RequestContext#setResponse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageRedirectionFilterTest.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
@BeforeEach
public void setUp() {
    discoveryClient = mock(DiscoveryClient.class);

    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.clear();
    ctx.set(SERVICE_ID_KEY, SERVICE_ID);
    MockHttpServletRequest request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    ctx.setRequest(request);
    ctx.setResponse(response);

    GatewayConfigProperties gatewayConfigProperties = getGatewayConfigProperties(ctx);

    this.filter = new PageRedirectionFilter(this.discoveryClient, gatewayConfigProperties);
}
 
Example 2
Source File: SwaggerBasePathRewritingFilterTest.java    From jhipster-registry with Apache License 2.0 6 votes vote down vote up
@Test
public void run_on_valid_response_gzip() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(true);
    context.setResponse(response);

    context.setResponseDataStream(new ByteArrayInputStream(gzipData("{\"basePath\":\"/\"}")));

    filter.run();

    assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");

    InputStream responseDataStream = new GZIPInputStream(context.getResponseDataStream());
    String responseBody = IOUtils.toString(responseDataStream, StandardCharsets.UTF_8);
    assertThat(responseBody).isEqualTo("{\"basePath\":\"/service1\"}");
}
 
Example 3
Source File: SwaggerBasePathRewritingFilterTest.java    From jhipster-registry with Apache License 2.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);

    filter.run();

    assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
    assertThat(context.getResponseBody()).isEqualTo("{\"basePath\":\"/service1\"}");
}
 
Example 4
Source File: RateLimitPreFilterTest.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void setUp() {
    MockitoAnnotations.initMocks(this);
    CounterFactory.initialize(new EmptyCounterFactory());

    when(httpServletRequest.getContextPath()).thenReturn("");
    when(httpServletRequest.getRequestURI()).thenReturn("/servicea/test");
    when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1");
    RequestContext requestContext = new RequestContext();
    requestContext.setRequest(httpServletRequest);
    requestContext.setResponse(httpServletResponse);
    RequestContext.testSetCurrentContext(requestContext);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    rateLimitProperties = new RateLimitProperties();
    rateLimitProperties.setAddResponseHeaders(false);
    UrlPathHelper urlPathHelper = new UrlPathHelper();
    RateLimitUtils rateLimitUtils = new DefaultRateLimitUtils(rateLimitProperties);
    Route route = new Route("servicea", "/test", "servicea", "/servicea", null, Collections.emptySet());
    TestRouteLocator routeLocator = new TestRouteLocator(Collections.emptyList(), Lists.newArrayList(route));
    target = new RateLimitPreFilter(rateLimitProperties, routeLocator, urlPathHelper, rateLimiter, rateLimitKeyGenerator, rateLimitUtils, eventPublisher);
}
 
Example 5
Source File: _SwaggerBasePathRewritingFilterTest.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}");
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 6
Source File: SwaggerBasePathRewritingFilterTest.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}");
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 7
Source File: SwaggerBasePathRewritingFilterTest.java    From jhipster-microservices-example with Apache License 2.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 8
Source File: SwaggerBasePathRewritingFilterIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 9
Source File: SwaggerBasePathRewritingFilterTest.java    From flair-registry with Apache License 2.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 10
Source File: SwaggerBasePathRewritingFilterTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void run_on_valid_response_gzip() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(true);
    context.setResponse(response);

    context.setResponseDataStream(new ByteArrayInputStream(gzipData("{\"basePath\":\"/\"}")));

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());

    InputStream responseDataStream = new GZIPInputStream(context.getResponseDataStream());
    String responseBody = IOUtils.toString(responseDataStream, StandardCharsets.UTF_8);
    assertEquals("{\"basePath\":\"/service1\"}", responseBody);
}
 
Example 11
Source File: PreviewFilter.java    From Taroco with Apache License 2.0 6 votes vote down vote up
@Override
public Object run() {
    final RequestContext ctx = RequestContext.getCurrentContext();
    final Response result = Response.failure(DefaultError.SHOW_AUTH_CONTROL);
    final HttpServletResponse response = ctx.getResponse();

    response.setCharacterEncoding(Charset.defaultCharset().name());
    response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
    response.setStatus(479);
    try {
        response.getWriter().print(JSONObject.toJSONString(result));
    } catch (IOException e1) {
        log.error("response io异常");
        e1.printStackTrace();
    }
    ctx.setSendZuulResponse(false);
    ctx.setResponse(response);
    return null;
}
 
Example 12
Source File: ValidateCodeFilter.java    From Taroco with Apache License 2.0 6 votes vote down vote up
@Override
public Object run() {
    try {
        checkCode(RequestContext.getCurrentContext().getRequest());
    } catch (ValidateCodeException e) {
        final RequestContext ctx = RequestContext.getCurrentContext();
        final Response result = Response.failure(e.getMessage());
        final HttpServletResponse response = ctx.getResponse();

        response.setCharacterEncoding(Charset.defaultCharset().name());
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        response.setStatus(478);
        try {
            response.getWriter().print(JsonUtils.toJsonString(result));
        } catch (IOException e1) {
            log.error("response io异常");
            e1.printStackTrace();
        }
        ctx.setSendZuulResponse(false);
        ctx.setResponse(response);
    }
    return null;
}
 
Example 13
Source File: SwaggerBasePathRewritingFilterTest.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 6 votes vote down vote up
@Test
public void run_on_valid_response_gzip() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(true);
    context.setResponse(response);

    context.setResponseDataStream(new ByteArrayInputStream(gzipData("{\"basePath\":\"/\"}")));

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());

    InputStream responseDataStream = new GZIPInputStream(context.getResponseDataStream());
    String responseBody = IOUtils.toString(responseDataStream, StandardCharsets.UTF_8);
    assertEquals("{\"basePath\":\"/service1\"}", responseBody);
}
 
Example 14
Source File: SwaggerBasePathRewritingFilterTest.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 15
Source File: SwaggerBasePathRewritingFilterTest.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 16
Source File: SwaggerBasePathRewritingFilterTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 17
Source File: SwaggerBasePathRewritingFilterTest.java    From java-microservices-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 18
Source File: SwaggerBasePathRewritingFilterTest.java    From flair-registry with Apache License 2.0 6 votes vote down vote up
@Test
public void run_on_valid_response_gzip() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(true);
    context.setResponse(response);

    context.setResponseDataStream(new ByteArrayInputStream(gzipData("{\"basePath\":\"/\"}")));

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());

    InputStream responseDataStream = new GZIPInputStream(context.getResponseDataStream());
    String responseBody = IOUtils.toString(responseDataStream, StandardCharsets.UTF_8);
    assertEquals("{\"basePath\":\"/service1\"}", responseBody);
}
 
Example 19
Source File: EncodedCharactersFilterTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@BeforeEach
public void setup() {
    filter = new EncodedCharactersFilter(discoveryClient, messageService);
    serviceInstanceWithConfiguration.getMetadata().put(METADATA_KEY, "true");
    serviceInstanceWithoutConfiguration.getMetadata().put(METADATA_KEY, "false");
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.clear();
    ctx.set(PROXY_KEY, "api/v1/" + SERVICE_ID);
    ctx.set(SERVICE_ID_KEY, SERVICE_ID);
    ctx.setResponse(new MockHttpServletResponse());
}
 
Example 20
Source File: SlashFilterTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@BeforeEach
public void setUp() throws Exception {
    this.filter = new SlashFilter();
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.clear();
    ctx.set(PROXY_KEY, "ui/service");
    ctx.set(SERVICE_ID_KEY, "service");
    ctx.setResponse(new MockHttpServletResponse());
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setRequestURI("/ui/service");
    ctx.setRequest(mockRequest);
}