Skip to content

Commit

Permalink
Add halt option to redirect; default to halting (#265)
Browse files Browse the repository at this point in the history
* Fix #264 - add halt option to redirect; default to halting

* Doc umentation fix

* Add breaking change note to changelog
  • Loading branch information
iffy committed Sep 15, 2020
1 parent ca5303d commit 8f89f0c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions changelog.markdown
@@ -1,5 +1,9 @@
# Jester changelog

## x.x.x - xx/xx/xxxx

- **Breaking change:** By default `redirect` now skips future handlers, including when used in a `before` route. To retain the old behavior, set the parameter `halt=false` (e.g. `redirect("/somewhere", halt=false)`)

## 0.4.3 - 12/08/2019

Minor release correcting a few packaging issues and includes some other
Expand Down
10 changes: 8 additions & 2 deletions jester.nim
Expand Up @@ -576,16 +576,22 @@ template resp*(code: HttpCode): typed =
result.matched = true
break route

template redirect*(url: string): typed =
template redirect*(url: string, halt = true): typed =
## Redirects to ``url``. Returns from this request handler immediately.
##
## If ``halt`` is true, skips executing future handlers, too.
##
## Any set response headers are preserved for this request.
bind TCActionSend, newHttpHeaders
result[0] = TCActionSend
result[1] = Http303
setHeader(result[2], "Location", url)
result[3] = ""
result.matched = true
break route
if halt:
break allRoutes
else:
break route

template pass*(): typed =
## Skips this request handler.
Expand Down
16 changes: 16 additions & 0 deletions tests/alltest.nim
Expand Up @@ -45,6 +45,12 @@ routes:

get "/halt":
resp "<h1>Not halted!</h1>"

before re"/halt-before/.*?":
halt Http502, "Halted!"

get "/halt-before/@something":
resp "Should never reach this"

get "/guess/@who":
if @"who" != "Frank": pass()
Expand All @@ -55,6 +61,16 @@ routes:

get "/redirect/@url/?":
redirect(uri(@"url"))

get "/redirect-halt/@url/?":
redirect(uri(@"url"))
resp "ok"

before re"/redirect-before/.*?":
redirect(uri("/nowhere"))

get "/redirect-before/@url/?":
resp "should not get here"

get "/win":
cond rand(5) < 3
Expand Down
16 changes: 16 additions & 0 deletions tests/tester.nim
Expand Up @@ -82,6 +82,11 @@ proc allTest(useStdLib: bool) =
let resp = waitFor client.get(address & "/foo/halt")
check resp.status.startsWith("502")
check (waitFor resp.body) == "I'm sorry, this page has been halted."

test "/halt-before":
let resp = waitFor client.request(address & "/foo/halt-before/something", HttpGet)
let body = waitFor resp.body
check body == "Halted!"

test "/guess":
let resp = waitFor client.get(address & "/foo/guess/foo")
Expand All @@ -92,6 +97,17 @@ proc allTest(useStdLib: bool) =
test "/redirect":
let resp = waitFor client.request(address & "/foo/redirect/halt", HttpGet)
check resp.headers["location"] == "http://localhost:5454/foo/halt"

test "/redirect-halt":
let resp = waitFor client.request(address & "/foo/redirect-halt/halt", HttpGet)
check resp.headers["location"] == "http://localhost:5454/foo/halt"
check (waitFor resp.body) == ""

test "/redirect-before":
let resp = waitFor client.request(address & "/foo/redirect-before/anywhere", HttpGet)
check resp.headers["location"] == "http://localhost:5454/foo/nowhere"
let body = waitFor resp.body
check body == ""

test "regex":
let resp = waitFor client.get(address & "/foo/02.html")
Expand Down

0 comments on commit 8f89f0c

Please sign in to comment.