25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

117 lines
4.1 KiB

  1. import pytest
  2. from .._headers import *
  3. def test_normalize_and_validate():
  4. assert normalize_and_validate([("foo", "bar")]) == [(b"foo", b"bar")]
  5. assert normalize_and_validate([(b"foo", b"bar")]) == [(b"foo", b"bar")]
  6. # no leading/trailing whitespace in names
  7. with pytest.raises(LocalProtocolError):
  8. normalize_and_validate([(b"foo ", "bar")])
  9. with pytest.raises(LocalProtocolError):
  10. normalize_and_validate([(b" foo", "bar")])
  11. # no weird characters in names
  12. with pytest.raises(LocalProtocolError) as excinfo:
  13. normalize_and_validate([(b"foo bar", b"baz")])
  14. assert "foo bar" in str(excinfo.value)
  15. with pytest.raises(LocalProtocolError):
  16. normalize_and_validate([(b"foo\x00bar", b"baz")])
  17. # no return or NUL characters in values
  18. with pytest.raises(LocalProtocolError) as excinfo:
  19. normalize_and_validate([("foo", "bar\rbaz")])
  20. assert "bar\\rbaz" in str(excinfo.value)
  21. with pytest.raises(LocalProtocolError):
  22. normalize_and_validate([("foo", "bar\nbaz")])
  23. with pytest.raises(LocalProtocolError):
  24. normalize_and_validate([("foo", "bar\x00baz")])
  25. # no leading/trailing whitespace
  26. with pytest.raises(LocalProtocolError):
  27. normalize_and_validate([("foo", " barbaz ")])
  28. # content-length
  29. assert (normalize_and_validate([("Content-Length", "1")])
  30. == [(b"content-length", b"1")])
  31. with pytest.raises(LocalProtocolError):
  32. normalize_and_validate([("Content-Length", "asdf")])
  33. with pytest.raises(LocalProtocolError):
  34. normalize_and_validate([("Content-Length", "1x")])
  35. with pytest.raises(LocalProtocolError):
  36. normalize_and_validate([
  37. ("Content-Length", "1"),
  38. ("Content-Length", "2"),
  39. ])
  40. # transfer-encoding
  41. assert (normalize_and_validate([("Transfer-Encoding", "chunked")])
  42. == [(b"transfer-encoding", b"chunked")])
  43. assert (normalize_and_validate([("Transfer-Encoding", "cHuNkEd")])
  44. == [(b"transfer-encoding", b"chunked")])
  45. with pytest.raises(LocalProtocolError) as excinfo:
  46. normalize_and_validate([("Transfer-Encoding", "gzip")])
  47. assert excinfo.value.error_status_hint == 501 # Not Implemented
  48. with pytest.raises(LocalProtocolError) as excinfo:
  49. normalize_and_validate([
  50. ("Transfer-Encoding", "chunked"),
  51. ("Transfer-Encoding", "gzip"),
  52. ])
  53. assert excinfo.value.error_status_hint == 501 # Not Implemented
  54. def test_get_set_comma_header():
  55. headers = normalize_and_validate([
  56. ("Connection", "close"),
  57. ("whatever", "something"),
  58. ("connectiON", "fOo,, , BAR"),
  59. ])
  60. assert get_comma_header(headers, b"connection") == [
  61. b"close", b"foo", b"bar"]
  62. set_comma_header(headers, b"newthing", ["a", "b"])
  63. with pytest.raises(LocalProtocolError):
  64. set_comma_header(headers, b"newthing", [" a", "b"])
  65. assert headers == [
  66. (b"connection", b"close"),
  67. (b"whatever", b"something"),
  68. (b"connection", b"fOo,, , BAR"),
  69. (b"newthing", b"a"),
  70. (b"newthing", b"b"),
  71. ]
  72. set_comma_header(headers, b"whatever", ["different thing"])
  73. assert headers == [
  74. (b"connection", b"close"),
  75. (b"connection", b"fOo,, , BAR"),
  76. (b"newthing", b"a"),
  77. (b"newthing", b"b"),
  78. (b"whatever", b"different thing"),
  79. ]
  80. def test_has_100_continue():
  81. from .._events import Request
  82. assert has_expect_100_continue(Request(
  83. method="GET",
  84. target="/",
  85. headers=[("Host", "example.com"), ("Expect", "100-continue")]))
  86. assert not has_expect_100_continue(Request(
  87. method="GET",
  88. target="/",
  89. headers=[("Host", "example.com")]))
  90. # Case insensitive
  91. assert has_expect_100_continue(Request(
  92. method="GET",
  93. target="/",
  94. headers=[("Host", "example.com"), ("Expect", "100-Continue")]))
  95. # Doesn't work in HTTP/1.0
  96. assert not has_expect_100_continue(Request(
  97. method="GET",
  98. target="/",
  99. headers=[("Host", "example.com"), ("Expect", "100-continue")],
  100. http_version="1.0"))