thttpcore.nim 3.5 KB

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