Skip to content

Commit

Permalink
Fixes #157.
Browse files Browse the repository at this point in the history
  • Loading branch information
dom96 committed Aug 24, 2018
1 parent e3cd4a2 commit 9cca0d0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
19 changes: 11 additions & 8 deletions jester.nim
Original file line number Diff line number Diff line change
Expand Up @@ -488,14 +488,6 @@ proc serve*(
asyncCheck serveFut
runForever()

template resp*(code: HttpCode,
headers: openarray[tuple[key, value: string]],
content: string): typed =
## Sets ``(code, headers, content)`` as the response.
bind TCActionSend, newHttpHeaders
result = (TCActionSend, code, headers.newHttpHeaders.some(), content, true)
break route

template setHeader(headers: var Option[RawHeaders], key, value: string): typed =
bind isNone
if isNone(headers):
Expand All @@ -513,6 +505,17 @@ template setHeader(headers: var Option[RawHeaders], key, value: string): typed =
# Add key if it doesn't exist.
headers = some(h & @({key: value}))

template resp*(code: HttpCode,
headers: openarray[tuple[key, value: string]],
content: string): typed =
## Sets ``(code, headers, content)`` as the response.
bind TCActionSend
result = (TCActionSend, code, none[RawHeaders](), content, true)
for header in headers:
setHeader(result[2], header[0], header[1])
break route


template resp*(content: string, contentType = "text/html;charset=utf-8"): typed =
## Sets ``content`` as the response; ``Http200`` as the status code
## and ``contentType`` as the Content-Type.
Expand Down
5 changes: 4 additions & 1 deletion tests/alltest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,7 @@ routes:
sendFile(getCurrentDir() / "tests/public/root/test_file.txt")

get "/query":
resp $request.params
resp $request.params

get "/issue157":
resp(Http200, [("Content-Type","text/css")] , "foo")
5 changes: 5 additions & 0 deletions tests/tester.nim
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ proc allTest(useStdLib: bool) =
let resp = waitFor client.get(address & "/foo/query?q=test")
check (waitFor resp.body) == """{"q": "test"}"""

test "issue 157":
let resp = waitFor client.get(address & "/foo/issue157")
let headers = resp.headers
check headers["Content-Type"] == "text/css"

suite "static":
test "index.html":
let resp = waitFor client.get(address & "/foo/root")
Expand Down

0 comments on commit 9cca0d0

Please sign in to comment.