Skip to content

Commit 8f89f0c

Browse files
authoredSep 15, 2020
Add halt option to redirect; default to halting (#265)
* Fix #264 - add halt option to redirect; default to halting * Doc umentation fix * Add breaking change note to changelog
1 parent ca5303d commit 8f89f0c

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed
 

‎changelog.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Jester changelog
22

3+
## x.x.x - xx/xx/xxxx
4+
5+
- **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)`)
6+
37
## 0.4.3 - 12/08/2019
48

59
Minor release correcting a few packaging issues and includes some other

‎jester.nim

+8-2
Original file line numberDiff line numberDiff line change
@@ -576,16 +576,22 @@ template resp*(code: HttpCode): typed =
576576
result.matched = true
577577
break route
578578

579-
template redirect*(url: string): typed =
579+
template redirect*(url: string, halt = true): typed =
580580
## Redirects to ``url``. Returns from this request handler immediately.
581+
##
582+
## If ``halt`` is true, skips executing future handlers, too.
583+
##
581584
## Any set response headers are preserved for this request.
582585
bind TCActionSend, newHttpHeaders
583586
result[0] = TCActionSend
584587
result[1] = Http303
585588
setHeader(result[2], "Location", url)
586589
result[3] = ""
587590
result.matched = true
588-
break route
591+
if halt:
592+
break allRoutes
593+
else:
594+
break route
589595

590596
template pass*(): typed =
591597
## Skips this request handler.

‎tests/alltest.nim

+16
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ routes:
4545

4646
get "/halt":
4747
resp "<h1>Not halted!</h1>"
48+
49+
before re"/halt-before/.*?":
50+
halt Http502, "Halted!"
51+
52+
get "/halt-before/@something":
53+
resp "Should never reach this"
4854

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

5662
get "/redirect/@url/?":
5763
redirect(uri(@"url"))
64+
65+
get "/redirect-halt/@url/?":
66+
redirect(uri(@"url"))
67+
resp "ok"
68+
69+
before re"/redirect-before/.*?":
70+
redirect(uri("/nowhere"))
71+
72+
get "/redirect-before/@url/?":
73+
resp "should not get here"
5874

5975
get "/win":
6076
cond rand(5) < 3

‎tests/tester.nim

+16
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ proc allTest(useStdLib: bool) =
8282
let resp = waitFor client.get(address & "/foo/halt")
8383
check resp.status.startsWith("502")
8484
check (waitFor resp.body) == "I'm sorry, this page has been halted."
85+
86+
test "/halt-before":
87+
let resp = waitFor client.request(address & "/foo/halt-before/something", HttpGet)
88+
let body = waitFor resp.body
89+
check body == "Halted!"
8590

8691
test "/guess":
8792
let resp = waitFor client.get(address & "/foo/guess/foo")
@@ -92,6 +97,17 @@ proc allTest(useStdLib: bool) =
9297
test "/redirect":
9398
let resp = waitFor client.request(address & "/foo/redirect/halt", HttpGet)
9499
check resp.headers["location"] == "http://localhost:5454/foo/halt"
100+
101+
test "/redirect-halt":
102+
let resp = waitFor client.request(address & "/foo/redirect-halt/halt", HttpGet)
103+
check resp.headers["location"] == "http://localhost:5454/foo/halt"
104+
check (waitFor resp.body) == ""
105+
106+
test "/redirect-before":
107+
let resp = waitFor client.request(address & "/foo/redirect-before/anywhere", HttpGet)
108+
check resp.headers["location"] == "http://localhost:5454/foo/nowhere"
109+
let body = waitFor resp.body
110+
check body == ""
95111

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

0 commit comments

Comments
 (0)