Skip to content

Commit

Permalink
Redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Aug 9, 2023
1 parent bb36d29 commit 645e008
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/wisp.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,38 @@ pub fn accepted() -> Response {
HttpResponse(202, [], Empty)
}

/// Create an empty response with status code 303: See Other, and the `location`
/// header set to the given URL. Used to redirect the client to another page.
///
/// # Examples
///
/// ```gleam
/// redirect(to: "https://example.com")
/// // -> Response(303, [#("location", "https://example.com")], Empty)
/// ```
///
pub fn redirect(to url: String) -> Response {
HttpResponse(303, [#("location", url)], Empty)
}

/// Create an empty response with status code 308: Moved Permanently, and the
/// `location` header set to the given URL. Used to redirect the client to
/// another page.
///
/// This redirect is permanent and the client is expected to cache the new
/// location, using it for future requests.
///
/// # Examples
///
/// ```gleam
/// moved_permanently(to: "https://example.com")
/// // -> Response(308, [#("location", "https://example.com")], Empty)
/// ```
///
pub fn moved_permanently(to url: String) -> Response {
HttpResponse(308, [#("location", url)], Empty)
}

/// Create an empty response with status code 204: No content.
///
/// # Examples
Expand Down
19 changes: 19 additions & 0 deletions test/wisp_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ pub fn no_content_test() {
|> should.equal(Response(204, [], wisp.Empty))
}

pub fn redirect_test() {
wisp.redirect(to: "https://example.com/wibble")
|> should.equal(Response(
303,
[#("location", "https://example.com/wibble")],
wisp.Empty,
))
}

pub fn moved_permanently_test() {
wisp.moved_permanently(to: "https://example.com/wobble")
|> should.equal(Response(
308,
[#("location", "https://example.com/wobble")],
wisp.Empty,
))
}

pub fn internal_server_error_test() {
wisp.internal_server_error()
|> should.equal(Response(500, [], wisp.Empty))
Expand Down Expand Up @@ -245,6 +263,7 @@ pub fn require_string_body_invalid_test() {
}

pub fn rescue_crashes_error_test() {
// TODO: Determine how to silence the logger for this test.
{
use <- wisp.rescue_crashes
panic as "we need to crash to test the middleware"
Expand Down

0 comments on commit 645e008

Please sign in to comment.