From 9cca0d0399e838ecefde4dd3a3f4ec89bc53b17b Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Fri, 24 Aug 2018 23:38:03 +0100 Subject: [PATCH] Fixes #157. --- jester.nim | 19 +++++++++++-------- tests/alltest.nim | 5 ++++- tests/tester.nim | 5 +++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/jester.nim b/jester.nim index f5b065b..778d040 100644 --- a/jester.nim +++ b/jester.nim @@ -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): @@ -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. diff --git a/tests/alltest.nim b/tests/alltest.nim index e08a8e7..a973d36 100644 --- a/tests/alltest.nim +++ b/tests/alltest.nim @@ -200,4 +200,7 @@ routes: sendFile(getCurrentDir() / "tests/public/root/test_file.txt") get "/query": - resp $request.params \ No newline at end of file + resp $request.params + + get "/issue157": + resp(Http200, [("Content-Type","text/css")] , "foo") \ No newline at end of file diff --git a/tests/tester.nim b/tests/tester.nim index de08f4f..25bd2a0 100644 --- a/tests/tester.nim +++ b/tests/tester.nim @@ -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")