Java Code Examples for com.netflix.zuul.context.RequestContext#testSetCurrentContext()
The following examples show how to use
com.netflix.zuul.context.RequestContext#testSetCurrentContext() .
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: ZuulRateLimitFilterTest.java From bucket4j-spring-boot-starter with Apache License 2.0 | 6 votes |
@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(), Mockito.anyBoolean())).thenReturn(consumptionProbeHolder); when(rateLimitCheck2.rateLimit(any(), Mockito.anyBoolean())).thenReturn(consumptionProbeHolder); when(rateLimitCheck3.rateLimit(any(), Mockito.anyBoolean())).thenReturn(consumptionProbeHolder); configuration.setStrategy(RateLimitConditionMatchingStrategy.ALL); filter.run(); verify(rateLimitCheck1, times(1)).rateLimit(any(), Mockito.anyBoolean()); verify(rateLimitCheck2, times(1)).rateLimit(any(), Mockito.anyBoolean()); verify(rateLimitCheck3, times(1)).rateLimit(any(), Mockito.anyBoolean()); }
Example 2
Source File: RateLimitPreFilterTest.java From spring-cloud-zuul-ratelimit with Apache License 2.0 | 6 votes |
@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 3
Source File: HttpBasicPassTicketSchemeTest.java From api-layer with Eclipse Public License 2.0 | 6 votes |
@Test public void givenRequest_whenApplyToRequest_thenSetsAuthorizationBasic() throws IRRPassTicketGenerationException { PassTicketService passTicketService = mock(PassTicketService.class); httpBasicPassTicketScheme = new HttpBasicPassTicketScheme(passTicketService, authConfigurationProperties); Calendar calendar = Calendar.getInstance(); Authentication authentication = new Authentication(AuthenticationScheme.HTTP_BASIC_PASSTICKET, "APPLID"); QueryResponse queryResponse = new QueryResponse("domain", USERNAME, calendar.getTime(), calendar.getTime(), QueryResponse.Source.ZOWE); HttpRequest httpRequest = new HttpGet("/test/request"); RequestContext requestContext = new RequestContext(); RequestContext.testSetCurrentContext(requestContext); doReturn("HI").when(passTicketService).generate(ArgumentMatchers.any(), ArgumentMatchers.any()); AuthenticationCommand ac = httpBasicPassTicketScheme.createCommand(authentication, () -> queryResponse); ac.applyToRequest(httpRequest); assertThat(httpRequest.getHeaders(HttpHeaders.AUTHORIZATION).length, is(not(0))); assertThat(httpRequest.getHeaders(HttpHeaders.AUTHORIZATION), hasItemInArray(hasToString( "Authorization: Basic VVNFUk5BTUU6SEk=" // USERNAME:HI ))); }
Example 4
Source File: BaseRateLimitPreFilterTest.java From spring-cloud-zuul-ratelimit with Apache License 2.0 | 6 votes |
@BeforeEach public void setUp() { MockitoAnnotations.initMocks(this); CounterFactory.initialize(new EmptyCounterFactory()); this.request = new MockHttpServletRequest(); this.response = new MockHttpServletResponse(); properties = this.properties(); RateLimitUtils rateLimitUtils = new DefaultRateLimitUtils(properties); RateLimitKeyGenerator rateLimitKeyGenerator = new DefaultRateLimitKeyGenerator(properties, rateLimitUtils); UrlPathHelper urlPathHelper = new UrlPathHelper(); this.filter = new RateLimitPreFilter(properties, this.routeLocator(), urlPathHelper, this.rateLimiter, rateLimitKeyGenerator, rateLimitUtils, eventPublisher); this.context = new RequestContext(); RequestContext.testSetCurrentContext(this.context); RequestContextHolder.setRequestAttributes(requestAttributes); this.context.clear(); this.context.setRequest(this.request); this.context.setResponse(this.response); }
Example 5
Source File: ZosmfSchemeTest.java From api-layer with Eclipse Public License 2.0 | 6 votes |
@Test public void givenZosmfToken_whenApplyToRequest_thenTestJwtToken() { Calendar calendar = Calendar.getInstance(); QueryResponse queryResponse = new QueryResponse("domain", "username", calendar.getTime(), calendar.getTime(), QueryResponse.Source.ZOSMF); AuthConfigurationProperties.CookieProperties cookieProperties = mock(AuthConfigurationProperties.CookieProperties.class); when(cookieProperties.getCookieName()).thenReturn("apimlAuthenticationToken"); when(authConfigurationProperties.getCookieProperties()).thenReturn(cookieProperties); RequestContext requestContext = spy(new RequestContext()); RequestContext.testSetCurrentContext(requestContext); HttpRequest httpRequest = new HttpGet("/test/request"); httpRequest.setHeader(COOKIE_HEADER, "cookie1=1"); Authentication authentication = new Authentication(AuthenticationScheme.ZOSMF, null); HttpServletRequest request = new MockHttpServletRequest(); requestContext.setRequest(request); when(authenticationService.getJwtTokenFromRequest(request)).thenReturn(Optional.of("jwtToken2")); when(authenticationService.parseJwtToken("jwtToken2")).thenReturn(queryResponse); when(authConfigurationProperties.getCookieProperties().getCookieName()).thenReturn("apimlAuthenticationToken"); zosmfScheme.createCommand(authentication, () -> queryResponse).applyToRequest(httpRequest); assertEquals("cookie1=1;jwtToken=jwtToken2", httpRequest.getFirstHeader("cookie").getValue()); }
Example 6
Source File: ZosmfSchemeTest.java From api-layer with Eclipse Public License 2.0 | 5 votes |
@BeforeEach public void prepareContextForTests() { Calendar calendar = Calendar.getInstance(); authentication = new Authentication(AuthenticationScheme.ZOSMF, null); queryResponse = new QueryResponse("domain", "username", calendar.getTime(), calendar.getTime(), QueryResponse.Source.ZOWE); requestContext = spy(new RequestContext()); RequestContext.testSetCurrentContext(requestContext); request = new MockHttpServletRequest(); requestContext.setRequest(request); scheme = new ZosmfScheme(authenticationService, authConfigurationProperties); }
Example 7
Source File: ServiceAuthenticationFilterTest.java From api-layer with Eclipse Public License 2.0 | 5 votes |
private AuthenticationCommand createJwtValidationCommand(String jwtToken) { RequestContext requestContext = mock(RequestContext.class); when(requestContext.get(SERVICE_ID_KEY)).thenReturn("service"); RequestContext.testSetCurrentContext(requestContext); doReturn(Optional.of(jwtToken)).when(authenticationService).getJwtTokenFromRequest(any()); AuthenticationCommand cmd = mock(AuthenticationCommand.class); doReturn(cmd).when(serviceAuthenticationService).getAuthenticationCommand("service", jwtToken); doReturn(true).when(cmd).isRequiredValidJwt(); return cmd; }
Example 8
Source File: HttpBasicPassTicketSchemeTest.java From api-layer with Eclipse Public License 2.0 | 5 votes |
@Test void givenJwtInCookie_whenApply_thenJwtIsRemoved() { AuthenticationCommand command = getPassTicketCommand(); RequestContext requestContext = new RequestContext(); requestContext.addZuulRequestHeader("cookie", authConfigurationProperties.getCookieProperties().getCookieName() + "=jwt;" + "abc=def" ); RequestContext.testSetCurrentContext(requestContext); command.apply(null); String cookies = requestContext.getZuulRequestHeaders().get("cookie"); assertEquals("abc=def", cookies); }
Example 9
Source File: RateLimitPostFilterTest.java From spring-cloud-zuul-ratelimit with Apache License 2.0 | 5 votes |
@BeforeEach public void setUp() { MockitoAnnotations.initMocks(this); when(httpServletRequest.getContextPath()).thenReturn("/servicea/test"); when(httpServletRequest.getRequestURI()).thenReturn("/servicea/test"); RequestContext requestContext = new RequestContext(); requestContext.setRequest(httpServletRequest); RequestContext.testSetCurrentContext(requestContext); RequestContextHolder.setRequestAttributes(requestAttributes); rateLimitProperties = new RateLimitProperties(); UrlPathHelper urlPathHelper = new UrlPathHelper(); RateLimitUtils rateLimitUtils = new DefaultRateLimitUtils(rateLimitProperties); target = new RateLimitPostFilter(rateLimitProperties, routeLocator, urlPathHelper, rateLimiter, rateLimitKeyGenerator, rateLimitUtils); }
Example 10
Source File: SentinelZuulPreFilterTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Before public void setUp() { MockitoAnnotations.initMocks(this); when(httpServletRequest.getContextPath()).thenReturn(""); when(httpServletRequest.getPathInfo()).thenReturn(URI); RequestContext requestContext = new RequestContext(); requestContext.set(SERVICE_ID_KEY, SERVICE_ID); requestContext.setRequest(httpServletRequest); RequestContext.testSetCurrentContext(requestContext); }
Example 11
Source File: ServiceAuthenticationServiceImplTest.java From api-layer with Eclipse Public License 2.0 | 5 votes |
@Test public void testLoadBalancerAuthenticationCommand() { ServiceAuthenticationServiceImpl.LoadBalancerAuthenticationCommand lbac = serviceAuthenticationServiceImpl.new LoadBalancerAuthenticationCommand(); assertFalse(lbac.isExpired()); RequestContext requestContext = new RequestContext(); RequestContext.testSetCurrentContext(requestContext); assertNull(requestContext.get(AUTHENTICATION_COMMAND_KEY)); lbac.apply(null); assertTrue(requestContext.get(AUTHENTICATION_COMMAND_KEY) instanceof ServiceAuthenticationServiceImpl.UniversalAuthenticationCommand); assertFalse(lbac.isRequiredValidJwt()); }
Example 12
Source File: ServiceAuthenticationServiceImplTest.java From api-layer with Eclipse Public License 2.0 | 5 votes |
@Test public void testUniversalAuthenticationCommand() { ServiceAuthenticationServiceImpl.UniversalAuthenticationCommand uac = serviceAuthenticationServiceImpl.new UniversalAuthenticationCommand(); assertFalse(uac.isExpired()); try { uac.apply(null); fail(); } catch (NullPointerException e) { // this command cannot be applied without parameter (null) } AuthenticationCommand ac = mock(AuthenticationCommand.class); InstanceInfo ii = createInstanceInfo("inst0001", AuthenticationScheme.HTTP_BASIC_PASSTICKET, "applid0001"); RequestContext requestContext = mock(RequestContext.class); HttpServletRequest request = mock(HttpServletRequest.class); when(requestContext.getRequest()).thenReturn(request); RequestContext.testSetCurrentContext(requestContext); when(authenticationService.getJwtTokenFromRequest(request)).thenReturn(Optional.of("jwtToken01")); AbstractAuthenticationScheme scheme = mock(AbstractAuthenticationScheme.class); when(scheme.createCommand(eq(new Authentication(AuthenticationScheme.HTTP_BASIC_PASSTICKET, "applid0001")), any())).thenReturn(ac); when(authenticationSchemeFactory.getSchema(AuthenticationScheme.HTTP_BASIC_PASSTICKET)).thenReturn(scheme); uac.apply(ii); verify(ac, times(1)).apply(null); }
Example 13
Source File: ServiceAuthenticationServiceImplTest.java From api-layer with Eclipse Public License 2.0 | 5 votes |
@BeforeEach public void init() { lockAndClearRequestContext(); MockitoAnnotations.initMocks(this); RequestContext.testSetCurrentContext(null); serviceAuthenticationService.evictCacheAllService(); serviceAuthenticationServiceImpl = new ServiceAuthenticationServiceImpl(discoveryClient, authenticationSchemeFactory, authenticationService, cacheManager, new CacheUtils()); }
Example 14
Source File: SentinelZuulPreFilterTest.java From Sentinel with Apache License 2.0 | 5 votes |
@Before public void setUp() { MockitoAnnotations.initMocks(this); when(httpServletRequest.getContextPath()).thenReturn(""); when(httpServletRequest.getPathInfo()).thenReturn(URI); RequestContext requestContext = new RequestContext(); requestContext.set(SERVICE_ID_KEY, SERVICE_ID); requestContext.setRequest(httpServletRequest); RequestContext.testSetCurrentContext(requestContext); }
Example 15
Source File: SimpleFilterTest.java From demo3_zuul_api_gateway with Apache License 2.0 | 5 votes |
@Test public void testRun() { HttpServletRequest req = mock(HttpServletRequest.class); when(req.getMethod()).thenReturn("GET"); when(req.getRequestURL()).thenReturn(new StringBuffer("http://foo")); RequestContext context = mock(RequestContext.class); when(context.getRequest()).thenReturn(req); RequestContext.testSetCurrentContext(context); filter.run(); this.outputCapture.expect(Matchers.containsString("GET request to http://foo")); }
Example 16
Source File: WebfluxRateLimitFilterrTest.java From bucket4j-spring-boot-starter with Apache License 2.0 | 5 votes |
@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); rateLimitConfig(30L, rateLimitCheck1); rateLimitConfig(0L, rateLimitCheck2); rateLimitConfig(10L, rateLimitCheck3); HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class); when(serverHttpResponse.getHeaders()).thenReturn(httpHeaders); final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); try { filter.filter(exchange, chain ); } catch(Exception e) { System.out.println(e.getMessage()); fail("WebfluxRateLimitException expected"); } verify(httpHeaders, times(1)).set(any(), captor.capture()); List<String> values = captor.getAllValues(); assertThat(values.stream().findFirst().get(), equalTo("30")); verify(rateLimitCheck1, times(1)).rateLimit(any(), Mockito.anyBoolean()); verify(rateLimitCheck2, times(1)).rateLimit(any(), Mockito.anyBoolean()); verify(rateLimitCheck3, times(1)).rateLimit(any(), Mockito.anyBoolean()); }
Example 17
Source File: HttpBasicPassTicketSchemeTest.java From api-layer with Eclipse Public License 2.0 | 4 votes |
@AfterEach public void tearDown() { RequestContext.testSetCurrentContext(null); }
Example 18
Source File: ServiceAuthenticationFilterTest.java From api-layer with Eclipse Public License 2.0 | 4 votes |
@Test public void testRun() { Mockito.when(serviceAuthenticationService.getAuthenticationCommand(anyString(), any())).thenReturn(command); HttpServletRequest request = mock(HttpServletRequest.class); RequestContext requestContext = mock(RequestContext.class); when(requestContext.getRequest()).thenReturn(request); when(requestContext.get(SERVICE_ID_KEY)).thenReturn("service"); RequestContext.testSetCurrentContext(requestContext); when(authenticationService.getJwtTokenFromRequest(any())).thenReturn(Optional.of("token")); serviceAuthenticationFilter.run(); verify(serviceAuthenticationService, times(1)).getAuthenticationCommand("service", "token"); verify(command, times(1)).apply(null); when(authenticationService.getJwtTokenFromRequest(any())).thenReturn(Optional.empty()); serviceAuthenticationFilter.run(); verify(serviceAuthenticationService, times(1)).getAuthenticationCommand("service", null); verify(serviceAuthenticationService, times(2)).getAuthenticationCommand(anyString(), any()); reset(requestContext); reset(authenticationService); CounterFactory.initialize(new CounterFactory() { @Override public void increment(String name) { } }); when(requestContext.get(SERVICE_ID_KEY)).thenReturn("error"); when(authenticationService.getJwtTokenFromRequest(any())).thenReturn(Optional.of("token")); when(serviceAuthenticationService.getAuthenticationCommand(eq("error"), any())) .thenThrow(new RuntimeException("Potential exception")); try { serviceAuthenticationFilter.run(); fail(); } catch (ZuulRuntimeException zre) { assertTrue(zre.getCause() instanceof ZuulException); ZuulException ze = (ZuulException) zre.getCause(); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), ze.nStatusCode); assertEquals(String.valueOf(new RuntimeException("Potential exception")), ze.errorCause); } }
Example 19
Source File: CurrentRequestContextTest.java From api-layer with Eclipse Public License 2.0 | 4 votes |
public void unlockRequestContext() { RequestContext.testSetCurrentContext(null); ctx.clear(); currentRequestContext.unlock(); }
Example 20
Source File: GatewayApplicationTest.java From demo3_zuul_api_gateway with Apache License 2.0 | 4 votes |
@Before public void setup() { RequestContext.testSetCurrentContext(new RequestContext()); }