Skip to content

Commit

Permalink
Added permanent redirect support.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Jul 18, 2018
1 parent d03a796 commit ec2db6d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/main/java/io/sinistral/proteus/server/ServerResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ public ServerResponse<T> ok()
public ServerResponse<T> redirect(String location)
{
this.redirectLocation = location;
this.status = StatusCodes.FOUND;
return this;
}

public ServerResponse<T> redirectPermanently(String location)
{
this.redirectLocation = location;
this.status = StatusCodes.MOVED_PERMANENTLY;;
return this;
}

Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/java/io/sinistral/proteus/controllers/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,15 @@ public ServerResponse<Set<Long>> 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ec2db6d

Please sign in to comment.