is_end_stream.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. use http::HeaderMap;
  2. use http_body::{Body, SizeHint};
  3. use std::pin::Pin;
  4. use std::task::{Context, Poll};
  5. struct Mock {
  6. size_hint: SizeHint,
  7. }
  8. impl Body for Mock {
  9. type Data = ::std::io::Cursor<Vec<u8>>;
  10. type Error = ();
  11. fn poll_data(
  12. self: Pin<&mut Self>,
  13. _cx: &mut Context<'_>,
  14. ) -> Poll<Option<Result<Self::Data, Self::Error>>> {
  15. Poll::Ready(None)
  16. }
  17. fn poll_trailers(
  18. self: Pin<&mut Self>,
  19. _cx: &mut Context<'_>,
  20. ) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
  21. Poll::Ready(Ok(None))
  22. }
  23. fn size_hint(&self) -> SizeHint {
  24. self.size_hint.clone()
  25. }
  26. }
  27. #[test]
  28. fn is_end_stream_true() {
  29. let combos = [
  30. (None, None, false),
  31. (Some(123), None, false),
  32. (Some(0), Some(123), false),
  33. (Some(123), Some(123), false),
  34. (Some(0), Some(0), false),
  35. ];
  36. for &(lower, upper, is_end_stream) in &combos {
  37. let mut size_hint = SizeHint::new();
  38. assert_eq!(0, size_hint.lower());
  39. assert!(size_hint.upper().is_none());
  40. if let Some(lower) = lower {
  41. size_hint.set_lower(lower);
  42. }
  43. if let Some(upper) = upper {
  44. size_hint.set_upper(upper);
  45. }
  46. let mut mock = Mock { size_hint };
  47. assert_eq!(
  48. is_end_stream,
  49. Pin::new(&mut mock).is_end_stream(),
  50. "size_hint = {:?}",
  51. mock.size_hint.clone()
  52. );
  53. }
  54. }
  55. #[test]
  56. fn is_end_stream_default_false() {
  57. let mut mock = Mock {
  58. size_hint: SizeHint::default(),
  59. };
  60. assert_eq!(
  61. false,
  62. Pin::new(&mut mock).is_end_stream(),
  63. "size_hint = {:?}",
  64. mock.size_hint.clone()
  65. );
  66. }