@Override public HttpStatus getStatusCode() { return getDelegate().getStatusCode(); }
private void logResponse(ServerWebExchange exchange) { LogFormatUtils.traceDebug(logger, traceOn -> { HttpStatus status = exchange.getResponse().getStatusCode(); return exchange.getLogPrefix() + "Completed " + (status != null ? status : "200 OK") + (traceOn ? ", headers=" + formatHeaders(exchange.getResponse().getHeaders()) : ""); }); }
@Test public void notFound() { TestHttpHandler handler1 = new TestHttpHandler(); TestHttpHandler handler2 = new TestHttpHandler(); Map<String, HttpHandler> map = new HashMap<>(); map.put("/path", handler1); map.put("/another/path", handler2); ServerHttpResponse response = testHandle("/yet/another/path", map); assertNotInvoked(handler1, handler2); assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); }
@Test public void webExceptionHandler() { ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/unknown-argument-type")); List<WebExceptionHandler> handlers = Collections.singletonList(new ServerError500ExceptionHandler()); WebHandler webHandler = new ExceptionHandlingWebHandler(this.dispatcherHandler, handlers); webHandler.handle(exchange).block(Duration.ofSeconds(5)); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, exchange.getResponse().getStatusCode()); }
@Test public void thrownExceptionBecomesErrorSignal() throws Exception { createWebHandler(new BadRequestExceptionHandler()).handle(this.exchange).block(); assertEquals(HttpStatus.BAD_REQUEST, this.exchange.getResponse().getStatusCode()); }
@Test public void handleErrorSignal() throws Exception { createWebHandler(new BadRequestExceptionHandler()).handle(this.exchange).block(); assertEquals(HttpStatus.BAD_REQUEST, this.exchange.getResponse().getStatusCode()); }
@Test public void handleErrorSignalWithMultipleHttpErrorHandlers() throws Exception { createWebHandler( new UnresolvedExceptionHandler(), new UnresolvedExceptionHandler(), new BadRequestExceptionHandler(), new UnresolvedExceptionHandler()).handle(this.exchange).block(); assertEquals(HttpStatus.BAD_REQUEST, this.exchange.getResponse().getStatusCode()); }
@Test public void unresolvedExceptionWithWebHttpHandlerAdapter() throws Exception { // HttpWebHandlerAdapter handles unresolved errors new HttpWebHandlerAdapter(createWebHandler(new UnresolvedExceptionHandler())) .handle(this.exchange.getRequest(), this.exchange.getResponse()).block(); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, this.exchange.getResponse().getStatusCode()); }
@Test public void preflightRequestWrongAllowedMethod() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from( preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "DELETE")); this.conf.addAllowedOrigin("*"); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); }
@Test public void actualRequestWithOriginHeader() throws Exception { ServerWebExchange exchange = actualRequest(); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); }
@Test public void preflightRequestAllOriginsAllowed() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from( preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET")); this.conf.addAllowedOrigin("*"); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertNull(response.getStatusCode()); }
@Test public void actualRequestWithOriginHeaderAndNullConfig() throws Exception { ServerWebExchange exchange = actualRequest(); this.processor.process(null, exchange); ServerHttpResponse response = exchange.getResponse(); assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertNull(response.getStatusCode()); }
@Test public void actualRequestCaseInsensitiveOriginMatch() throws Exception { ServerWebExchange exchange = actualRequest(); this.conf.addAllowedOrigin("http://DOMAIN2.com"); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertNull(response.getStatusCode()); }
@Test public void preflightRequestTestWithOriginButWithoutOtherHeaders() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from(preFlightRequest()); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); }
@Test public void unresolvedException() throws Exception { Mono<Void> mono = createWebHandler(new UnresolvedExceptionHandler()).handle(this.exchange); StepVerifier.create(mono).expectErrorMessage("boo").verify(); assertNull(this.exchange.getResponse().getStatusCode()); }
@Test public void preflightRequestWithoutRequestMethod() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from( preFlightRequest().header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1")); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); }
@Test public void preflightRequestMatchedAllowedMethod() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from( preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET")); this.conf.addAllowedOrigin("*"); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertNull(response.getStatusCode()); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertEquals("GET,HEAD", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)); }
@Test public void preflightRequestWithRequestAndMethodHeaderButNoConfig() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from(preFlightRequest() .header(ACCESS_CONTROL_REQUEST_METHOD, "GET") .header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1")); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); }
@Test public void preflightRequestWithNullConfig() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from( preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET")); this.conf.addAllowedOrigin("*"); this.processor.process(null, exchange); ServerHttpResponse response = exchange.getResponse(); assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); }
@Test public void actualRequestWithOriginHeaderAndAllowedOrigin() throws Exception { ServerWebExchange exchange = actualRequest(); this.conf.addAllowedOrigin("*"); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals("*", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN)); assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_MAX_AGE)); assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS)); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertNull(response.getStatusCode()); }