HeimdallDecorationFilter extends PreDecorationFilter { @Override public Object run() { long startTime = System.currentTimeMillis(); try { process(); detail.setStatus(Constants.SUCCESS); } catch (Exception e) { detail.setStatus(Constants.FAILED); throw e; } finally { long endTime = System.currentTimeMillis(); long duration = (endTime - startTime); detail.setTimeInMillisRun(duration); TraceContextHolder.getInstance().getActualTrace().addFilter(this.getClass().getSimpleName(), detail); } return null; } HeimdallDecorationFilter(ProxyRouteLocator routeLocator, String dispatcherServletPath, ZuulProperties properties, ProxyRequestHelper proxyRequestHelper, RequestHelper requestHelper, CredentialRepository credentialRepository, EnvironmentInfoRepository environmentInfoRepository); @Override boolean shouldFilter(); @Override Object run();  }
@Test public void routeWithoutApiBasePath() { this.request.setRequestURI("/v2/api/foo/1"); this.request.setMethod(HttpMethod.GET.name()); Map<String, ZuulRoute> routes = new LinkedHashMap<>(); ZuulRoute route = new ZuulRoute("idFoo", "/v2/api/foo/{id}", null, "my.dns.com.br", true, null, Collections.newSetFromMap(new ConcurrentHashMap<>())); routes.put("/v2/api/foo/{id}", route); EnvironmentInfo environmentInfo = new EnvironmentInfo(); environmentInfo.setId(1L); environmentInfo.setOutboundURL("http: environmentInfo.setVariables(new HashMap<>()); Credential opPost = new Credential(HttpMethod.POST.name(), "/api/foo", "/v2", "apiName", 10L, 88L, 10L, false); Credential opGet = new Credential(HttpMethod.GET.name(), "/api/foo/{id}", "/v2", "apiName", 10L, 88L, 10L, false); Credential opDelete = new Credential(HttpMethod.DELETE.name(), "/api/foo/{id}", "/v2", "apiName", 10L, 88L, 10L, false); Mockito.when(routeLocator.getAtomicRoutes()).thenReturn(new AtomicReference<>(routes)); Mockito.when(credentialRepository.findByPattern("/v2/api/foo/{id}")).thenReturn(Lists.newArrayList(opPost, opGet, opDelete)); Mockito.when(environmentInfoRepository.findByApiIdAndEnvironmentInboundURL(10L, "http: this.filter.run(); assertEquals("/api/foo/1", this.ctx.get(REQUEST_URI_KEY)); assertTrue(this.ctx.sendZuulResponse()); }
@Test public void matchRouteWithMultiEnvironments() { this.request.setRequestURI("/path/api/foo"); this.request.setMethod(HttpMethod.GET.name()); this.request.addHeader("host", "some-path.com"); Map<String, ZuulRoute> routes = new LinkedHashMap<>(); ZuulRoute route = new ZuulRoute("idFoo", "/path/api/foo", null, "my.dns.com.br", true, null, Collections.newSetFromMap(new ConcurrentHashMap<>())); routes.put("/path/api/foo", route); EnvironmentInfo environmentInfo = new EnvironmentInfo(); environmentInfo.setId(1L); environmentInfo.setOutboundURL("https: environmentInfo.setVariables(new HashMap<>()); Credential opGet = new Credential(HttpMethod.GET.name(), "/api/foo", "/path", "apiName", 10L, 88L, 10L, false); Credential opDelete = new Credential(HttpMethod.DELETE.name(), "/api/foo", "/path", "apiName", 10L, 88L, 10L, false); Mockito.when(routeLocator.getAtomicRoutes()).thenReturn(new AtomicReference<>(routes)); Mockito.when(credentialRepository.findByPattern("/path/api/foo")).thenReturn(Lists.newArrayList(opGet, opDelete)); Mockito.when(environmentInfoRepository.findByApiIdAndEnvironmentInboundURL(10L, "some-path.com")).thenReturn(environmentInfo); this.filter.run(); assertEquals("/api/foo", this.ctx.get(REQUEST_URI_KEY)); assertTrue(this.ctx.sendZuulResponse()); }
@Test public void throwNotAllowedRoute() { this.request.setRequestURI("/v2/api/foo/1"); this.request.setMethod(HttpMethod.DELETE.name()); Map<String, ZuulRoute> routes = new LinkedHashMap<>(); ZuulRoute route = new ZuulRoute("idFoo", "/v2/api/foo/{id}", null, "my.dns.com.br", true, null, Collections.newSetFromMap(new ConcurrentHashMap<>())); routes.put("/v2/api/foo/{id}", route); Credential opGet = new Credential(HttpMethod.GET.name(), "/api/foo/{id}", "/path", "apiName", 10L, 88L, 10L, false); Mockito.when(routeLocator.getAtomicRoutes()).thenReturn(new AtomicReference<>(routes)); Mockito.when(credentialRepository.findByPattern("/v2/api/foo/{id}")).thenReturn(Lists.newArrayList(opGet)); this.filter.run(); assertFalse(this.ctx.sendZuulResponse()); assertEquals(HttpStatus.METHOD_NOT_ALLOWED.value(), this.ctx.getResponseStatusCode()); assertEquals(HttpStatus.METHOD_NOT_ALLOWED.getReasonPhrase(), this.ctx.getResponseBody()); }