Skip to content

Commit

Permalink
Move closure test to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
nigredo-tori committed Aug 5, 2016
1 parent 309eb25 commit 60b042a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 108 deletions.
202 changes: 98 additions & 104 deletions tests/alltest.nim
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,109 +2,103 @@
# MIT License - Look at license.txt for details. # MIT License - Look at license.txt for details.
import jester, asyncdispatch, strutils, random, os, asyncnet, re import jester, asyncdispatch, strutils, random, os, asyncnet, re


proc configJester(closureVal: string) = settings:
settings: port = Port(5454)
port = Port(5454) appName = "/foo"
appName = "/foo" bindAddr = "127.0.0.1"
bindAddr = "127.0.0.1"

routes:
routes: get "/":
get "/": resp "Hello World"
resp "Hello World"

get "/resp":
get "/resp": if true:
if true: resp "This should be the response"
resp "This should be the response" resp "This should NOT be the response"
resp "This should NOT be the response"

get "/halt":
get "/halt": halt Http502, "I'm sorry, this page has been halted."
halt Http502, "I'm sorry, this page has been halted." resp "test"
resp "test"

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

get "/guess/@who":
get "/guess/@who": if @"who" != "Frank": pass()
if @"who" != "Frank": pass() resp "You've found me!"
resp "You've found me!"

get "/guess/@_":
get "/guess/@_": resp "Haha. You will never find me!"
resp "Haha. You will never find me!"

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

get "/win":
get "/win": cond random(5) < 3
cond random(5) < 3 resp "<b>You won!</b>"
resp "<b>You won!</b>"

get "/win":
get "/win": resp "<b>Try your luck again, loser.</b>"
resp "<b>Try your luck again, loser.</b>"

get "/profile/@id/@value?/?":
get "/profile/@id/@value?/?": var html = ""
var html = "" html.add "<b>Msg: </b>" & @"id" &
html.add "<b>Msg: </b>" & @"id" & "<br/><b>Name: </b>" & @"value"
"<br/><b>Name: </b>" & @"value" html.add "<br/>"
html.add "<br/>" html.add "<b>Params: </b>" & $request.params
html.add "<b>Params: </b>" & $request.params

resp html
resp html

get "/attachment":
get "/attachment": attachment "public/root/index.html"
attachment "public/root/index.html" resp "blah"
resp "blah"

get "/error":
get "/error": proc blah = raise newException(ESynch, "BLAH BLAH BLAH")
proc blah = raise newException(ESynch, "BLAH BLAH BLAH") blah()
blah()

get "/live":
get "/live": await response.sendHeaders()
await response.sendHeaders() for i in 0 .. 10:
for i in 0 .. 10: await response.send("The number is: " & $i & "</br>")
await response.send("The number is: " & $i & "</br>") await sleepAsync(1000)
await sleepAsync(1000) response.client.close()
response.client.close()

# curl -v -F file='blah' http://dom96.co.cc:5000
# curl -v -F file='blah' http://dom96.co.cc:5000 # curl -X POST -d 'test=56' localhost:5000/post
# curl -X POST -d 'test=56' localhost:5000/post

post "/post":
post "/post": body.add "Received: <br/>"
body.add "Received: <br/>" body.add($request.formData)
body.add($request.formData) body.add "<br/>\n"
body.add "<br/>\n" body.add($request.params)
body.add($request.params)

status = Http200
status = Http200

get "/post":
get "/post": resp """
resp """ <form name="input" action="$1" method="post">
<form name="input" action="$1" method="post"> First name: <input type="text" name="FirstName" value="Mickey" /><br />
First name: <input type="text" name="FirstName" value="Mickey" /><br /> Last name: <input type="text" name="LastName" value="Mouse" /><br />
Last name: <input type="text" name="LastName" value="Mouse" /><br /> <input type="submit" value="Submit" />
<input type="submit" value="Submit" /> </form>""" % [uri("/post", absolute = false)]
</form>""" % [uri("/post", absolute = false)]

get "/file":
get "/file": resp """
resp """ <form action="/post" method="post"
<form action="/post" method="post" enctype="multipart/form-data">
enctype="multipart/form-data"> <label for="file">Filename:</label>
<label for="file">Filename:</label> <input type="file" name="file" id="file" />
<input type="file" name="file" id="file" /> <br />
<br /> <input type="submit" name="submit" value="Submit" />
<input type="submit" name="submit" value="Submit" /> </form>"""
</form>"""

get re"^\/([0-9]{2})\.html$":
get re"^\/([0-9]{2})\.html$": resp request.matches[0]
resp request.matches[0]

patch "/patch":
patch "/patch": body.add "Received: "
body.add "Received: " body.add($request.body)
body.add($request.body) status = Http200
status = Http200

get "/closures":
resp closureVal

configJester("This value is in closure")


runForever() runForever()
12 changes: 12 additions & 0 deletions tests/closures.nim
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,12 @@
# Issue #63

import jester, asyncdispatch

proc configRoutes(closureVal: string) =
routes:
get "/":
# should respond "This value is in closure"
resp closureVal

configRoutes("This value is in closure")
runForever()
4 changes: 0 additions & 4 deletions tests/tester.nim
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,7 +34,3 @@ test "regex":
test "resp": test "resp":
let resp = waitFor client.get("http://localhost:" & $port & "/foo/resp") let resp = waitFor client.get("http://localhost:" & $port & "/foo/resp")
check resp.body == "This should be the response" check resp.body == "This should be the response"

test "closures":
let resp = waitFor client.get("http://localhost:" & $port & "/foo/closures")
check resp.body == "This value is in closure"

0 comments on commit 60b042a

Please sign in to comment.