CorsHandler implements HttpHandler, HandlerWrapper { boolean validateOrigin(HttpServerExchange exchange) { HeaderMap responseHeaders = exchange.getResponseHeaders(); responseHeaders.put(VARY, "origin"); String origin = exchange.getRequestHeaders().getFirst(ORIGIN); if (origin == null) return true; return setOrigin(origin, responseHeaders); } CorsHandler(String allowedOrigins); @Override void handleRequest(HttpServerExchange exchange); @Override HttpHandler wrap(HttpHandler handler);  }
@Test public void validateOrigin_defaultsToVaryOrigin() { boolean valid = validateOrigin( "get", ImmutableMap.of(), ImmutableMap.of("vary", "origin") ); assertThat(valid).isTrue(); }
@Test public void validateOrigin_returnsWildcard() { boolean valid = validateOrigin( "get", ImmutableMap.of("Origin", "http: ImmutableMap.of( "vary", "origin", "access-control-allow-origin", "*" ) ); assertThat(valid).isTrue(); }
@Test public void validateOrigin_configuredOrigin() { handler = new CorsHandler("http: boolean valid = validateOrigin( "get", ImmutableMap.of("Origin", "http: ImmutableMap.of( "vary", "origin", "access-control-allow-origin", "http: ) ); assertThat(valid).isTrue(); }
@Test public void validateOrigin_disallowedOrigin() { handler = new CorsHandler("http: boolean valid = validateOrigin( "get", ImmutableMap.of("Origin", "http: ImmutableMap.of("vary", "origin") ); assertThat(valid).isFalse(); }