thttpcore.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. """
  4. import httpcore, strutils
  5. import std/assertions
  6. block:
  7. block HttpCode:
  8. doAssert $Http418 == "418 I'm a teapot"
  9. doAssert Http418.is4xx() == true
  10. doAssert Http418.is2xx() == false
  11. block headers:
  12. var h = newHttpHeaders()
  13. doAssert h.len == 0
  14. h.add("Cookie", "foo")
  15. doAssert h.len == 1
  16. doAssert h.hasKey("cooKIE")
  17. doAssert h["Cookie"] == "foo"
  18. doAssert h["cookie"] == "foo"
  19. h["cookie"] = @["bar", "x"]
  20. doAssert h["Cookie"] == "bar"
  21. doAssert h["Cookie", 1] == "x"
  22. doAssert h["Cookie"].contains("BaR") == true
  23. doAssert h["Cookie"].contains("X") == true
  24. doAssert "baR" in h["cookiE"]
  25. h.del("coOKie")
  26. doAssert h.len == 0
  27. # Test that header constructor works with repeated values
  28. let h1 = newHttpHeaders({"a": "1", "a": "2", "A": "3"})
  29. doAssert seq[string](h1["a"]).join(",") == "1,2,3"
  30. block 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. block 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")
  63. block:
  64. var test = newHttpHeaders()
  65. test["Connection"] = @["Upgrade", "Close"]
  66. doAssert test["Connection", 0] == "Upgrade"
  67. doAssert test["Connection", 1] == "Close"
  68. test.add("Connection", "Test")
  69. doAssert test["Connection", 2] == "Test"
  70. doAssert "upgrade" in test["Connection"]
  71. # Bug #5344.
  72. doAssert parseHeader("foobar: ") == ("foobar", @[""])
  73. let (key, value) = parseHeader("foobar: ")
  74. test = newHttpHeaders()
  75. test[key] = value
  76. doAssert test["foobar"] == ""
  77. doAssert parseHeader("foobar:") == ("foobar", @[""])
  78. block: # test title case
  79. var testTitleCase = newHttpHeaders(titleCase=true)
  80. testTitleCase.add("content-length", "1")
  81. doAssert testTitleCase.hasKey("Content-Length")
  82. for key, val in testTitleCase:
  83. doAssert key == "Content-Length"