diff --git a/src/main/java/io/sinistral/proteus/server/ServerResponse.java b/src/main/java/io/sinistral/proteus/server/ServerResponse.java index a56b55f..e1a3b61 100644 --- a/src/main/java/io/sinistral/proteus/server/ServerResponse.java +++ b/src/main/java/io/sinistral/proteus/server/ServerResponse.java @@ -271,6 +271,14 @@ public ServerResponse ok() public ServerResponse redirect(String location) { this.redirectLocation = location; + this.status = StatusCodes.FOUND; + return this; + } + + public ServerResponse redirectPermanently(String location) + { + this.redirectLocation = location; + this.status = StatusCodes.MOVED_PERMANENTLY;; return this; } @@ -400,13 +408,9 @@ public void send(final HttpHandler handler, final HttpServerExchange exchange) t { if(this.redirectLocation != null) { - try - { - Handlers.redirect(redirectLocation).handleRequest(exchange); - } catch (Exception e) - { - throw new RuntimeException(e); - } + exchange.setStatusCode(this.status); + exchange.getResponseHeaders().put(Headers.LOCATION, this.redirectLocation); + exchange.endExchange(); return; } diff --git a/src/test/java/io/sinistral/proteus/controllers/Tests.java b/src/test/java/io/sinistral/proteus/controllers/Tests.java index ee21c1a..2683496 100644 --- a/src/test/java/io/sinistral/proteus/controllers/Tests.java +++ b/src/test/java/io/sinistral/proteus/controllers/Tests.java @@ -224,7 +224,15 @@ public ServerResponse> genericOptionalSet( ServerRequest request, @Qu return response( ids.get() ).applicationJson(); } + + @GET + @Path("/redirect/permanent") + @ApiOperation(value = "Permanent redirect endpoint", httpMethod = "GET" ) + public ServerResponse testPermanentRedirect() + { + return response().redirectPermanently("https://google.com"); + } @GET @Path("/redirect") diff --git a/src/test/java/io/sinistral/proteus/server/TestControllerEndpoints.java b/src/test/java/io/sinistral/proteus/server/TestControllerEndpoints.java index bfb8484..73a22fa 100644 --- a/src/test/java/io/sinistral/proteus/server/TestControllerEndpoints.java +++ b/src/test/java/io/sinistral/proteus/server/TestControllerEndpoints.java @@ -174,6 +174,18 @@ public void testRedirect() { given().log().uri().when().get("tests/redirect").then().statusCode(200).and().header("Server", "gws"); } + + @Test + public void testRedirectFoundCode() + { + given().log().uri().when().redirects().follow(false).get("tests/redirect").then().statusCode(302); + } + + @Test + public void testRedirectMovedPermanentlyCode() + { + given().log().uri().when().redirects().follow(false).get("tests/redirect/permanent").then().statusCode(301); + } @SuppressWarnings("resource") @Test