tasynchttpserver.nim 3.4 KB

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