thttpcore.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import httpcore, strutils
  2. import std/assertions
  3. block:
  4. block HttpCode:
  5. doAssert $Http418 == "418 I'm a teapot"
  6. doAssert Http418.is4xx() == true
  7. doAssert Http418.is2xx() == false
  8. block headers:
  9. var h = newHttpHeaders()
  10. doAssert h.len == 0
  11. h.add("Cookie", "foo")
  12. doAssert h.len == 1
  13. doAssert h.hasKey("cooKIE")
  14. doAssert h["Cookie"] == "foo"
  15. doAssert h["cookie"] == "foo"
  16. h["cookie"] = @["bar", "x"]
  17. doAssert h["Cookie"] == "bar"
  18. doAssert h["Cookie", 1] == "x"
  19. doAssert h["Cookie"].contains("BaR") == true
  20. doAssert h["Cookie"].contains("X") == true
  21. doAssert "baR" in h["cookiE"]
  22. h.del("coOKie")
  23. doAssert h.len == 0
  24. # Test that header constructor works with repeated values
  25. let h1 = newHttpHeaders({"a": "1", "a": "2", "A": "3"})
  26. doAssert seq[string](h1["a"]).join(",") == "1,2,3"
  27. block test_cookies_with_comma:
  28. doAssert parseHeader("cookie: foo, bar") == ("cookie", @["foo, bar"])
  29. doAssert parseHeader("cookie: foo, bar, prologue") == ("cookie", @["foo, bar, prologue"])
  30. doAssert parseHeader("cookie: foo, bar, prologue, starlight") == ("cookie", @["foo, bar, prologue, starlight"])
  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") == (key: "Cookie", value: @["foo, bar"])
  35. doAssert parseHeader("Cookie: foo, bar, prologue") == (key: "Cookie", value: @["foo, bar, prologue"])
  36. doAssert parseHeader("Cookie: foo, bar, prologue, starlight") == (key: "Cookie", value: @["foo, bar, prologue, starlight"])
  37. doAssert parseHeader("Accept: foo, bar") == (key: "Accept", value: @["foo", "bar"])
  38. doAssert parseHeader("Accept: foo, bar, prologue") == (key: "Accept", value: @["foo", "bar", "prologue"])
  39. doAssert parseHeader("Accept: foo, bar, prologue, starlight") == (key: "Accept", value: @["foo", "bar", "prologue", "starlight"])
  40. block add_empty_sequence_to_HTTP_headers:
  41. block:
  42. var headers = newHttpHeaders()
  43. headers["empty"] = @[]
  44. doAssert not headers.hasKey("empty")
  45. block:
  46. var headers = newHttpHeaders()
  47. headers["existing"] = "true"
  48. headers["existing"] = @[]
  49. doAssert not headers.hasKey("existing")
  50. block:
  51. var headers = newHttpHeaders()
  52. headers["existing"] = @["true"]
  53. headers["existing"] = @[]
  54. doAssert not headers.hasKey("existing")
  55. block:
  56. var headers = newHttpHeaders()
  57. headers["existing"] = @[]
  58. headers["existing"] = @["true"]
  59. doAssert headers.hasKey("existing")
  60. block:
  61. var test = newHttpHeaders()
  62. test["Connection"] = @["Upgrade", "Close"]
  63. doAssert test["Connection", 0] == "Upgrade"
  64. doAssert test["Connection", 1] == "Close"
  65. test.add("Connection", "Test")
  66. doAssert test["Connection", 2] == "Test"
  67. doAssert "upgrade" in test["Connection"]
  68. # Bug #5344.
  69. doAssert parseHeader("foobar: ") == ("foobar", @[""])
  70. let (key, value) = parseHeader("foobar: ")
  71. test = newHttpHeaders()
  72. test[key] = value
  73. doAssert test["foobar"] == ""
  74. doAssert parseHeader("foobar:") == ("foobar", @[""])
  75. block: # test title case
  76. var testTitleCase = newHttpHeaders(titleCase=true)
  77. testTitleCase.add("content-length", "1")
  78. doAssert testTitleCase.hasKey("Content-Length")
  79. for key, val in testTitleCase:
  80. doAssert key == "Content-Length"