ClientIdInterceptorService { public void validate(Long apiId, Location location) { RequestContext context = RequestContext.getCurrentContext(); String clientId; if (Location.HEADER.equals(location)) { clientId = context.getZuulRequestHeaders().get(CLIENT_ID); if (clientId == null) clientId = context.getRequest().getHeader(CLIENT_ID); } else clientId = context.getRequest().getParameter(CLIENT_ID); this.validateClientId(apiId, clientId); }  void validate(Long apiId, Location location);  }
@Test public void successCaseTest() { Mockito.when(appRepository.findByClientId(clientId)).thenReturn(app); clientIdInterceptorService.validate(api1.getId(), Location.HEADER); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), this.ctx.getResponseStatusCode()); }
@Test public void successCase2Test() { Mockito.when(appRepository.findByClientId(clientId)).thenReturn(app); clientIdInterceptorService.validate(api2.getId(), Location.HEADER); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), this.ctx.getResponseStatusCode()); }
@Test public void clientIdNullTest() { clientIdInterceptorService.validate( 10L, null); assertEquals(HttpStatus.UNAUTHORIZED.value(), this.ctx.getResponseStatusCode()); assertTrue((Boolean) this.ctx.get(INTERRUPT)); assertFalse(ctx.sendZuulResponse()); }
@Test public void apiIdNullTest() { clientIdInterceptorService.validate(null, Location.HEADER); assertEquals(HttpStatus.UNAUTHORIZED.value(), this.ctx.getResponseStatusCode()); assertTrue((Boolean) this.ctx.get(INTERRUPT)); assertFalse(ctx.sendZuulResponse()); }
@Test public void unauthorizedClientIdTest() { this.request.addHeader("client_id", someOtherClientId); Mockito.when(appRepository.findByClientId(someOtherClientId)).thenReturn(null); clientIdInterceptorService.validate(api1.getId(), Location.HEADER); assertEquals(HttpStatus.UNAUTHORIZED.value(), this.ctx.getResponseStatusCode()); assertTrue((Boolean) this.ctx.get(INTERRUPT)); assertFalse(ctx.sendZuulResponse()); }
@Test public void apiNotInPlanTest() { Api api = new Api(); api.setId(30L); Mockito.when(appRepository.findByClientId(clientId)).thenReturn(app); clientIdInterceptorService.validate(api.getId(), Location.HEADER); assertEquals(HttpStatus.UNAUTHORIZED.value(), this.ctx.getResponseStatusCode()); assertTrue((Boolean) this.ctx.get(INTERRUPT)); assertFalse(ctx.sendZuulResponse()); }
@Test public void planNotInAppTest() { Api api = new Api(); api.setId(30L); Plan plan = new Plan(); plan.setApi(api); Mockito.when(appRepository.findByClientId(clientId)).thenReturn(app); clientIdInterceptorService.validate(api.getId(), Location.HEADER); assertEquals(HttpStatus.UNAUTHORIZED.value(), this.ctx.getResponseStatusCode()); assertTrue((Boolean) this.ctx.get(INTERRUPT)); assertFalse(ctx.sendZuulResponse()); }