Search in sources :

Example 96 with RequestContext

use of com.netflix.zuul.context.RequestContext in project bucket4j-spring-boot-starter by MarcGiffing.

the class ZuulRateLimitFilterTest method should_execute_all_checks_when_using_RateLimitConditionMatchingStrategy_All.

@Test
public void should_execute_all_checks_when_using_RateLimitConditionMatchingStrategy_All() {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/url");
    RequestContext context = new RequestContext();
    context.setRequest(request);
    RequestContext.testSetCurrentContext(context);
    when(rateLimitCheck1.rateLimit(any())).thenReturn(consumptionProbe);
    when(rateLimitCheck2.rateLimit(any())).thenReturn(consumptionProbe);
    when(rateLimitCheck3.rateLimit(any())).thenReturn(consumptionProbe);
    configuration.setStrategy(RateLimitConditionMatchingStrategy.ALL);
    filter.run();
    verify(rateLimitCheck1, times(1)).rateLimit(any());
    verify(rateLimitCheck2, times(1)).rateLimit(any());
    verify(rateLimitCheck3, times(1)).rateLimit(any());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(com.netflix.zuul.context.RequestContext) Test(org.junit.Test)

Example 97 with RequestContext

use of com.netflix.zuul.context.RequestContext in project bucket4j-spring-boot-starter by MarcGiffing.

the class ZuulRateLimitFilterTest method should_execute_only_one_check_when_using_RateLimitConditionMatchingStrategy_FIRST.

@Test
public void should_execute_only_one_check_when_using_RateLimitConditionMatchingStrategy_FIRST() {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/url");
    RequestContext context = new RequestContext();
    context.setRequest(request);
    RequestContext.testSetCurrentContext(context);
    configuration.setStrategy(RateLimitConditionMatchingStrategy.FIRST);
    when(rateLimitCheck1.rateLimit(any())).thenReturn(consumptionProbe);
    filter.run();
    verify(rateLimitCheck1, times(1)).rateLimit(any());
    verify(rateLimitCheck2, times(0)).rateLimit(any());
    verify(rateLimitCheck3, times(0)).rateLimit(any());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(com.netflix.zuul.context.RequestContext) Test(org.junit.Test)

Example 98 with RequestContext

use of com.netflix.zuul.context.RequestContext in project bucket4j-spring-boot-starter by MarcGiffing.

the class ZuulRateLimitFilterTest method setup.

@Before
public void setup() {
    consumptionProbe = Mockito.mock(ConsumptionProbe.class);
    when(consumptionProbe.isConsumed()).thenReturn(true);
    rateLimitCheck1 = mock(RateLimitCheck.class);
    rateLimitCheck2 = mock(RateLimitCheck.class);
    rateLimitCheck3 = mock(RateLimitCheck.class);
    configuration = new FilterConfiguration();
    configuration.setRateLimitChecks(Arrays.asList(rateLimitCheck1, rateLimitCheck2, rateLimitCheck3));
    configuration.setUrl("url");
    filter = new ZuulRateLimitFilter(configuration) {

        @Override
        protected RequestContext getCurrentRequestContext() {
            return Mockito.mock(RequestContext.class);
        }
    };
}
Also used : RateLimitCheck(com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck) FilterConfiguration(com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration) ConsumptionProbe(io.github.bucket4j.ConsumptionProbe) RequestContext(com.netflix.zuul.context.RequestContext) Before(org.junit.Before)

Example 99 with RequestContext

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

the class PreDecorationFilterTests method sensitiveHeadersCaseInsensitive.

@Test
public void sensitiveHeadersCaseInsensitive() throws Exception {
    this.properties.setPrefix("/api");
    this.properties.setStripPrefix(true);
    this.properties.setSensitiveHeaders(Collections.singleton("X-bAr"));
    this.request.setRequestURI("/api/foo/1");
    this.routeLocator.addRoute("/foo/**", "foo");
    this.filter.run();
    RequestContext ctx = RequestContext.getCurrentContext();
    @SuppressWarnings("unchecked") Set<String> sensitiveHeaders = (Set<String>) ctx.get(ProxyRequestHelper.IGNORED_HEADERS);
    assertTrue("sensitiveHeaders is wrong: " + sensitiveHeaders, sensitiveHeaders.containsAll(Collections.singletonList("x-bar")));
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) RequestContext(com.netflix.zuul.context.RequestContext) Test(org.junit.Test)

Example 100 with RequestContext

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

the class PreDecorationFilterTests method ignoredHeadersAlreadySetInRequestContextDontGetOverridden.

@Test
public void ignoredHeadersAlreadySetInRequestContextDontGetOverridden() throws Exception {
    this.properties.setPrefix("/api");
    this.properties.setStripPrefix(true);
    this.properties.setSensitiveHeaders(Collections.singleton("x-bar"));
    this.request.setRequestURI("/api/foo/1");
    this.routeLocator.addRoute("/foo/**", "foo");
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.set(ProxyRequestHelper.IGNORED_HEADERS, new HashSet<>(Arrays.asList("x-foo")));
    this.filter.run();
    @SuppressWarnings("unchecked") Set<String> sensitiveHeaders = (Set<String>) ctx.get(ProxyRequestHelper.IGNORED_HEADERS);
    assertTrue("sensitiveHeaders is wrong: " + sensitiveHeaders, sensitiveHeaders.containsAll(Arrays.asList("x-bar", "x-foo")));
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) 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