ValidationHandler { public boolean isToValidate(HttpServerRequest request) { final Logger log = RequestLoggerFactory.getLogger(ValidationHandler.class, request); boolean doValidate = supportedMethods.contains(request.method().name()) && isJsonRequest(request) && !(request.headers().names().contains(VALID_HEADER)) && !(request.params().names().contains(EXPAND_PARAM)); if (!doValidate) { return false; } if(request.uri().contains(HOOKS_ROUTE_URI_PART) || request.uri().contains(HOOKS_LISTENERS_URI_PART)){ return false; } List<Map<String, String>> validationResources = validationResourceManager.getValidationResource().getResources(); try { for (Map<String, String> validationResource : validationResources) { if (doesRequestValueMatch(request.method().name(), validationResource.get(ValidationResource.METHOD_PROPERTY)) && doesRequestValueMatch(request.uri(), validationResource.get(ValidationResource.URL_PROPERTY))) { return true; } } } catch (PatternSyntaxException patternException) { log.error(patternException.getMessage() + " " + patternException.getPattern()); } return false; } ValidationHandler(ValidationResourceManager validationResourceManager, ResourceStorage storage, HttpClient httpClient, String schemaRoot); boolean isToValidate(HttpServerRequest request); void handle(final HttpServerRequest request); boolean isFailOnError(); void setFailOnError(boolean failOnError); static final String HOOKS_LISTENERS_URI_PART; static final String HOOKS_ROUTE_URI_PART; static final String ACCEPT; }
@Test public void testIsToValidate(TestContext context){ storage.putMockData(VALIDATION_URI, RESOURCE_GET_PUT); ValidationHandler validationHandler = new ValidationHandler(validationResourceManager, storage, httpClient, SCHEMA_ROOT); sendValidationResourcesUpdate(RESOURCE_GET_PUT); class PUTToUnmanagedResourceRequest extends HttpServerRequestMock{ @Override public HttpMethod method() { return HttpMethod.PUT; } @Override public String uri() { return "/some/other/resource"; } @Override public String path() { return "/some/other/resource"; } } context.assertFalse(validationHandler.isToValidate(new PUTToUnmanagedResourceRequest()), "PUT Requests to not managed resources should not be validated"); class PUTToGateleenSomeResourceRequest extends HttpServerRequestMock{ @Override public HttpMethod method() { return HttpMethod.PUT; } @Override public String uri() { return "/gateleen/resources/someResource"; } @Override public String path() { return "/gateleen/resources/someResource"; } } context.assertTrue(validationHandler.isToValidate(new PUTToGateleenSomeResourceRequest()), "PUT Requests to some resource should be validated"); sendValidationResourcesUpdate(RESOURCE_GET); context.assertFalse(validationHandler.isToValidate(new PUTToGateleenSomeResourceRequest()), "Now, PUT Requests to some resource should not be validated anymore (only GET Requests)"); context.assertFalse(validationHandler.isToValidate(new PUTToUnmanagedResourceRequest()), "PUT Requests to not managed resources should not be validated"); class GETToGateleenSomeResourceRequest extends PUTToGateleenSomeResourceRequest { @Override public HttpMethod method() { return HttpMethod.GET; } } context.assertTrue(validationHandler.isToValidate(new GETToGateleenSomeResourceRequest()), "GET Requests to some resource should be validated"); sendValidationResourcesUpdate(OTHER_RESOURCES_GET_PUT); context.assertFalse(validationHandler.isToValidate(new GETToGateleenSomeResourceRequest()), "GET Requests to some resource should not be validated"); context.assertTrue(validationHandler.isToValidate(new PUTToGateleenSomeResourceRequest()), "PUT Requests to some resource should be validated"); context.assertFalse(validationHandler.isToValidate(new PUTToUnmanagedResourceRequest()), "PUT Requests to not managed resources should not be validated"); class PUTToGateleenOtherResourceRequest extends HttpServerRequestMock{ @Override public HttpMethod method() { return HttpMethod.PUT; } @Override public String uri() { return "/gateleen/resources/otherResource"; } @Override public String path() { return "/gateleen/resources/otherResource"; } } class GETToGateleenOtherResourceRequest extends PUTToGateleenOtherResourceRequest { @Override public HttpMethod method() { return HttpMethod.GET; } } context.assertTrue(validationHandler.isToValidate(new GETToGateleenOtherResourceRequest()), "GET Requests to other resource should be validated"); context.assertFalse(validationHandler.isToValidate(new PUTToGateleenOtherResourceRequest()), "PUT Requests to other resource should not be validated"); }
@Test public void testIsToValidateHooks(TestContext context){ storage.putMockData(VALIDATION_URI, RESOURCE_GET_PUT_GENERIC); ValidationHandler validationHandler = new ValidationHandler(validationResourceManager, storage, httpClient, SCHEMA_ROOT); sendValidationResourcesUpdate(RESOURCE_GET_PUT_GENERIC); class GetToHooksResourceRequest extends HttpServerRequestMock{ @Override public HttpMethod method() { return HttpMethod.GET; } @Override public String uri() { return "/gateleen/trip/v1/destinations/current/_hooks/listeners/http/linti-06614431506022811"; } @Override public String path() { return "/gateleen/trip/v1/destinations/current/_hooks/listeners/http/linti-06614431506022811"; } } context.assertFalse(validationHandler.isToValidate(new GetToHooksResourceRequest()), "GET Requests to hooks should not be validated"); class GetToHooksRouteResourceRequest extends HttpServerRequestMock{ @Override public HttpMethod method() { return HttpMethod.PUT; } @Override public String uri() { return "/gateleen/trip/v1/destinations/current/_hooks/route/http/linti-06614431506022811"; } @Override public String path() { return "/gateleen/trip/v1/destinations/current/_hooks/route/http/linti-06614431506022811"; } } context.assertFalse(validationHandler.isToValidate(new GetToHooksRouteResourceRequest()), "GET Requests to hooks/routes should not be validated"); class GetGateleenResourceRequest extends HttpServerRequestMock{ @Override public HttpMethod method() { return HttpMethod.PUT; } @Override public String uri() { return "/gateleen/trip/v1/destinations/current/11"; } @Override public String path() { return "/gateleen/trip/v1/destinations/current/11"; } } context.assertTrue(validationHandler.isToValidate(new GetGateleenResourceRequest()), "GET Requests with wildcard should be validated"); }