1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
discard """
cmd: "nim c --threads:on $file"
exitcode: 0
output: "OK"
disabled: false
"""
import strutils
from net import TimeoutError
import httpclient, asynchttpserver, asyncdispatch, asyncfutures
template runTest(
handler: proc (request: Request): Future[void] {.gcsafe.},
request: proc (server: AsyncHttpServer): Future[AsyncResponse],
test: proc (response: AsyncResponse, body: string): Future[void]) =
let server = newAsyncHttpServer()
discard server.serve(Port(64123), handler)
let
response = waitFor(request(server))
body = waitFor(response.body)
discard test(response, body)
proc test200() {.async.} =
proc handler(request: Request) {.async.} =
await request.respond(Http200, "Hello World, 200")
proc request(server: AsyncHttpServer): Future[AsyncResponse] {.async.} =
let
client = newAsyncHttpClient()
clientResponse = await client.request("http://localhost:64123/")
server.close()
return clientResponse
proc test(response: AsyncResponse, body: string) {.async.} =
doAssert(response.status == Http200)
doAssert(body == "Hello World, 200")
doAssert(response.headers.hasKey("Content-Length"))
doAssert(response.headers["Content-Length"] == "16")
runTest(handler, request, test)
proc test404() {.async.} =
proc handler(request: Request) {.async.} =
await request.respond(Http404, "Hello World, 404")
proc request(server: AsyncHttpServer): Future[AsyncResponse] {.async.} =
let
client = newAsyncHttpClient()
clientResponse = await client.request("http://localhost:64123/")
server.close()
return clientResponse
proc test(response: AsyncResponse, body: string) {.async.} =
doAssert(response.status == Http404)
doAssert(body == "Hello World, 404")
doAssert(response.headers.hasKey("Content-Length"))
doAssert(response.headers["Content-Length"] == "16")
runTest(handler, request, test)
proc testCustomEmptyHeaders() {.async.} =
proc handler(request: Request) {.async.} =
await request.respond(Http200, "Hello World, 200", newHttpHeaders())
proc request(server: AsyncHttpServer): Future[AsyncResponse] {.async.} =
let
client = newAsyncHttpClient()
clientResponse = await client.request("http://localhost:64123/")
server.close()
return clientResponse
proc test(response: AsyncResponse, body: string) {.async.} =
doAssert(response.status == Http200)
doAssert(body == "Hello World, 200")
doAssert(response.headers.hasKey("Content-Length"))
doAssert(response.headers["Content-Length"] == "16")
runTest(handler, request, test)
proc testCustomContentLength() {.async.} =
proc handler(request: Request) {.async.} =
let headers = newHttpHeaders()
headers["Content-Length"] = "0"
await request.respond(Http200, "Hello World, 200", headers)
proc request(server: AsyncHttpServer): Future[AsyncResponse] {.async.} =
let
client = newAsyncHttpClient()
clientResponse = await client.request("http://localhost:64123/")
server.close()
return clientResponse
proc test(response: AsyncResponse, body: string) {.async.} =
doAssert(response.status == Http200)
doAssert(body == "")
doAssert(response.headers.hasKey("Content-Length"))
doAssert(response.headers["Content-Length"] == "0")
runTest(handler, request, test)
waitfor(test200())
waitfor(test404())
waitfor(testCustomEmptyHeaders())
waitfor(testCustomContentLength())
echo "OK"
|