tasynchttpserver.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 std/assertions
  10. import httpclient, asynchttpserver, asyncdispatch, asyncfutures
  11. template runTest(
  12. handler: proc (request: Request): Future[void] {.gcsafe.},
  13. request: proc (server: AsyncHttpServer): Future[AsyncResponse],
  14. test: proc (response: AsyncResponse, body: string): Future[void]) =
  15. let server = newAsyncHttpServer()
  16. discard server.serve(Port(64123), handler)
  17. let
  18. response = waitFor(request(server))
  19. body = waitFor(response.body)
  20. discard test(response, body)
  21. proc test200() {.async.} =
  22. proc handler(request: Request) {.async.} =
  23. await request.respond(Http200, "Hello World, 200")
  24. proc request(server: AsyncHttpServer): Future[AsyncResponse] {.async.} =
  25. let
  26. client = newAsyncHttpClient()
  27. clientResponse = await client.request("http://localhost:64123/")
  28. server.close()
  29. return clientResponse
  30. proc test(response: AsyncResponse, body: string) {.async.} =
  31. doAssert(response.status == $Http200)
  32. doAssert(body == "Hello World, 200")
  33. doAssert(response.headers.hasKey("Content-Length"))
  34. doAssert(response.headers["Content-Length"] == "16")
  35. runTest(handler, request, test)
  36. proc test404() {.async.} =
  37. proc handler(request: Request) {.async.} =
  38. await request.respond(Http404, "Hello World, 404")
  39. proc request(server: AsyncHttpServer): Future[AsyncResponse] {.async.} =
  40. let
  41. client = newAsyncHttpClient()
  42. clientResponse = await client.request("http://localhost:64123/")
  43. server.close()
  44. return clientResponse
  45. proc test(response: AsyncResponse, body: string) {.async.} =
  46. doAssert(response.status == $Http404)
  47. doAssert(body == "Hello World, 404")
  48. doAssert(response.headers.hasKey("Content-Length"))
  49. doAssert(response.headers["Content-Length"] == "16")
  50. runTest(handler, request, test)
  51. proc testCustomEmptyHeaders() {.async.} =
  52. proc handler(request: Request) {.async.} =
  53. await request.respond(Http200, "Hello World, 200", newHttpHeaders())
  54. proc request(server: AsyncHttpServer): Future[AsyncResponse] {.async.} =
  55. let
  56. client = newAsyncHttpClient()
  57. clientResponse = await client.request("http://localhost:64123/")
  58. server.close()
  59. return clientResponse
  60. proc test(response: AsyncResponse, body: string) {.async.} =
  61. doAssert(response.status == $Http200)
  62. doAssert(body == "Hello World, 200")
  63. doAssert(response.headers.hasKey("Content-Length"))
  64. doAssert(response.headers["Content-Length"] == "16")
  65. runTest(handler, request, test)
  66. proc testCustomContentLength() {.async.} =
  67. proc handler(request: Request) {.async.} =
  68. let headers = newHttpHeaders()
  69. headers["Content-Length"] = "0"
  70. await request.respond(Http200, "Hello World, 200", headers)
  71. proc request(server: AsyncHttpServer): Future[AsyncResponse] {.async.} =
  72. let
  73. client = newAsyncHttpClient()
  74. clientResponse = await client.request("http://localhost:64123/")
  75. server.close()
  76. return clientResponse
  77. proc test(response: AsyncResponse, body: string) {.async.} =
  78. doAssert(response.status == $Http200)
  79. doAssert(body == "")
  80. doAssert(response.headers.hasKey("Content-Length"))
  81. doAssert(response.headers["Content-Length"] == "0")
  82. doAssert contentLength(response) == 0 # bug #22778
  83. runTest(handler, request, test)
  84. waitFor(test200())
  85. waitFor(test404())
  86. waitFor(testCustomEmptyHeaders())
  87. waitFor(testCustomContentLength())
  88. echo "OK"