thttpcore.nim 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. discard """
  2. output: "[Suite] httpcore"
  3. """
  4. import unittest
  5. import httpcore, strutils
  6. suite "httpcore":
  7. test "HttpCode":
  8. assert $Http418 == "418 I'm a teapot"
  9. assert Http418.is4xx() == true
  10. assert Http418.is2xx() == false
  11. test "headers":
  12. var h = newHttpHeaders()
  13. assert h.len == 0
  14. h.add("Cookie", "foo")
  15. assert h.len == 1
  16. assert h.hasKey("cooKIE")
  17. assert h["Cookie"] == "foo"
  18. assert h["cookie"] == "foo"
  19. h["cookie"] = @["bar", "x"]
  20. assert h["Cookie"] == "bar"
  21. assert h["Cookie", 1] == "x"
  22. assert h["Cookie"].contains("BaR") == true
  23. assert h["Cookie"].contains("X") == true
  24. assert "baR" in h["cookiE"]
  25. h.del("coOKie")
  26. assert h.len == 0
  27. # Test that header constructor works with repeated values
  28. let h1 = newHttpHeaders({"a": "1", "a": "2", "A": "3"})
  29. assert seq[string](h1["a"]).join(",") == "1,2,3"
  30. test "test cookies with comma":
  31. doAssert parseHeader("cookie: foo, bar") == ("cookie", @["foo, bar"])
  32. doAssert parseHeader("cookie: foo, bar, prologue") == ("cookie", @["foo, bar, prologue"])
  33. doAssert parseHeader("cookie: foo, bar, prologue, starlight") == ("cookie", @["foo, bar, prologue, starlight"])
  34. doAssert parseHeader("cookie: foo, bar") == ("cookie", @["foo, bar"])
  35. doAssert parseHeader("cookie: foo, bar, prologue") == ("cookie", @["foo, bar, prologue"])
  36. doAssert parseHeader("cookie: foo, bar, prologue, starlight") == ("cookie", @["foo, bar, prologue, starlight"])
  37. doAssert parseHeader("Cookie: foo, bar") == (key: "Cookie", value: @["foo, bar"])
  38. doAssert parseHeader("Cookie: foo, bar, prologue") == (key: "Cookie", value: @["foo, bar, prologue"])
  39. doAssert parseHeader("Cookie: foo, bar, prologue, starlight") == (key: "Cookie", value: @["foo, bar, prologue, starlight"])
  40. doAssert parseHeader("Accept: foo, bar") == (key: "Accept", value: @["foo", "bar"])
  41. doAssert parseHeader("Accept: foo, bar, prologue") == (key: "Accept", value: @["foo", "bar", "prologue"])
  42. doAssert parseHeader("Accept: foo, bar, prologue, starlight") == (key: "Accept", value: @["foo", "bar", "prologue", "starlight"])
  43. test "add empty sequence to HTTP headers":
  44. block:
  45. var headers = newHttpHeaders()
  46. headers["empty"] = @[]
  47. doAssert not headers.hasKey("empty")
  48. block:
  49. var headers = newHttpHeaders()
  50. headers["existing"] = "true"
  51. headers["existing"] = @[]
  52. doAssert not headers.hasKey("existing")
  53. block:
  54. var headers = newHttpHeaders()
  55. headers["existing"] = @["true"]
  56. headers["existing"] = @[]
  57. doAssert not headers.hasKey("existing")
  58. block:
  59. var headers = newHttpHeaders()
  60. headers["existing"] = @[]
  61. headers["existing"] = @["true"]
  62. doAssert headers.hasKey("existing")