Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

121 Zeilen
3.6 KiB

  1. import pytest
  2. from .._util import LocalProtocolError
  3. from .. import _events
  4. from .._events import *
  5. def test_event_bundle():
  6. class T(_events._EventBundle):
  7. _fields = ["a", "b"]
  8. _defaults = {"b": 1}
  9. def _validate(self):
  10. if self.a == 0:
  11. raise ValueError
  12. # basic construction and methods
  13. t = T(a=1, b=0)
  14. assert repr(t) == "T(a=1, b=0)"
  15. assert t == T(a=1, b=0)
  16. assert not (t == T(a=2, b=0))
  17. assert not (t != T(a=1, b=0))
  18. assert (t != T(a=2, b=0))
  19. with pytest.raises(TypeError):
  20. hash(t)
  21. # check defaults
  22. t = T(a=10)
  23. assert t.a == 10
  24. assert t.b == 1
  25. # no positional args
  26. with pytest.raises(TypeError):
  27. T(1)
  28. with pytest.raises(TypeError):
  29. T(1, a=1, b=0)
  30. # unknown field
  31. with pytest.raises(TypeError):
  32. T(a=1, b=0, c=10)
  33. # missing required field
  34. with pytest.raises(TypeError) as exc:
  35. T(b=0)
  36. # make sure we error on the right missing kwarg
  37. assert 'kwarg a' in str(exc)
  38. # _validate is called
  39. with pytest.raises(ValueError):
  40. T(a=0, b=0)
  41. def test_events():
  42. with pytest.raises(LocalProtocolError):
  43. # Missing Host:
  44. req = Request(method="GET", target="/", headers=[("a", "b")],
  45. http_version="1.1")
  46. # But this is okay (HTTP/1.0)
  47. req = Request(method="GET", target="/", headers=[("a", "b")],
  48. http_version="1.0")
  49. # fields are normalized
  50. assert req.method == b"GET"
  51. assert req.target == b"/"
  52. assert req.headers == [(b"a", b"b")]
  53. assert req.http_version == b"1.0"
  54. # This is also okay -- has a Host (with weird capitalization, which is ok)
  55. req = Request(method="GET", target="/",
  56. headers=[("a", "b"), ("hOSt", "example.com")],
  57. http_version="1.1")
  58. # we normalize header capitalization
  59. assert req.headers == [(b"a", b"b"), (b"host", b"example.com")]
  60. # Multiple host is bad too
  61. with pytest.raises(LocalProtocolError):
  62. req = Request(method="GET", target="/",
  63. headers=[("Host", "a"), ("Host", "a")],
  64. http_version="1.1")
  65. # Even for HTTP/1.0
  66. with pytest.raises(LocalProtocolError):
  67. req = Request(method="GET", target="/",
  68. headers=[("Host", "a"), ("Host", "a")],
  69. http_version="1.0")
  70. # Header values are validated
  71. with pytest.raises(LocalProtocolError):
  72. req = Request(method="GET", target="/",
  73. headers=[("Host", "a"), ("Foo", " asd\x00")],
  74. http_version="1.0")
  75. ir = InformationalResponse(status_code=100, headers=[("Host", "a")])
  76. assert ir.status_code == 100
  77. assert ir.headers == [(b"host", b"a")]
  78. assert ir.http_version == b"1.1"
  79. with pytest.raises(LocalProtocolError):
  80. InformationalResponse(status_code=200, headers=[("Host", "a")])
  81. resp = Response(status_code=204, headers=[], http_version="1.0")
  82. assert resp.status_code == 204
  83. assert resp.headers == []
  84. assert resp.http_version == b"1.0"
  85. with pytest.raises(LocalProtocolError):
  86. resp = Response(status_code=100, headers=[], http_version="1.0")
  87. with pytest.raises(LocalProtocolError):
  88. Response(status_code="100", headers=[], http_version="1.0")
  89. with pytest.raises(LocalProtocolError):
  90. InformationalResponse(status_code=b"100",
  91. headers=[], http_version="1.0")
  92. d = Data(data=b"asdf")
  93. assert d.data == b"asdf"
  94. eom = EndOfMessage()
  95. assert eom.headers == []
  96. cc = ConnectionClosed()
  97. assert repr(cc) == "ConnectionClosed()"