You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

78 line
2.0 KiB

  1. from .._receivebuffer import ReceiveBuffer
  2. def test_receivebuffer():
  3. b = ReceiveBuffer()
  4. assert not b
  5. assert len(b) == 0
  6. assert bytes(b) == b""
  7. b += b"123"
  8. assert b
  9. assert len(b) == 3
  10. assert bytes(b) == b"123"
  11. b.compress()
  12. assert bytes(b) == b"123"
  13. assert b.maybe_extract_at_most(2) == b"12"
  14. assert b
  15. assert len(b) == 1
  16. assert bytes(b) == b"3"
  17. b.compress()
  18. assert bytes(b) == b"3"
  19. assert b.maybe_extract_at_most(10) == b"3"
  20. assert bytes(b) == b""
  21. assert b.maybe_extract_at_most(10) is None
  22. assert not b
  23. ################################################################
  24. # maybe_extract_until_next
  25. ################################################################
  26. b += b"12345a6789aa"
  27. assert b.maybe_extract_until_next(b"a") == b"12345a"
  28. assert bytes(b) == b"6789aa"
  29. assert b.maybe_extract_until_next(b"aaa") is None
  30. assert bytes(b) == b"6789aa"
  31. b += b"a12"
  32. assert b.maybe_extract_until_next(b"aaa") == b"6789aaa"
  33. assert bytes(b) == b"12"
  34. # check repeated searches for the same needle, triggering the
  35. # pickup-where-we-left-off logic
  36. b += b"345"
  37. assert b.maybe_extract_until_next(b"aaa") is None
  38. b += b"6789aaa123"
  39. assert b.maybe_extract_until_next(b"aaa") == b"123456789aaa"
  40. assert bytes(b) == b"123"
  41. ################################################################
  42. # maybe_extract_lines
  43. ################################################################
  44. b += b"\r\na: b\r\nfoo:bar\r\n\r\ntrailing"
  45. lines = b.maybe_extract_lines()
  46. assert lines == [b"123", b"a: b", b"foo:bar"]
  47. assert bytes(b) == b"trailing"
  48. assert b.maybe_extract_lines() is None
  49. b += b"\r\n\r"
  50. assert b.maybe_extract_lines() is None
  51. assert b.maybe_extract_at_most(100) == b"trailing\r\n\r"
  52. assert not b
  53. # Empty body case (as happens at the end of chunked encoding if there are
  54. # no trailing headers, e.g.)
  55. b += b"\r\ntrailing"
  56. assert b.maybe_extract_lines() == []
  57. assert bytes(b) == b"trailing"