1use std::{future::Future, time::Duration};
5
6use async_stream::try_stream;
7use bytes::Bytes;
8use futures::Stream;
9use reqwest::{RequestBuilder, Response};
10use serde::de::DeserializeOwned;
11use tokio::time::{interval, sleep, timeout, MissedTickBehavior};
12use tracing::{debug_span, error, info, warn, Instrument};
13
14use super::{errors::FeedError, failures::FailureTracker, publisher::Publisher};
15
16#[derive(Clone, Debug)]
21pub struct HttpFeedConfig {
22 pub poll_interval: Duration,
26 pub request_timeout: Duration,
28 pub max_backoff_exp: u32,
34 pub max_consecutive_failures: Option<u32>,
37 pub max_snapshot_age: Option<Duration>,
45}
46
47pub fn default_http_feed_config() -> HttpFeedConfig {
51 HttpFeedConfig {
52 poll_interval: Duration::from_secs(5),
53 request_timeout: Duration::from_secs(10),
54 max_backoff_exp: 3,
55 max_consecutive_failures: None,
56 max_snapshot_age: Some(Duration::from_secs(30)),
57 }
58}
59
60pub async fn run_http_poll_feed<S: HttpSource>(
73 config: HttpFeedConfig,
74 publisher: Publisher<S::Snapshot>,
75 source: S,
76) -> Result<(), FeedError> {
77 let HttpFeedConfig {
81 poll_interval,
82 request_timeout,
83 max_backoff_exp,
84 max_consecutive_failures,
85 max_snapshot_age,
86 } = config;
87
88 if poll_interval.is_zero() {
89 return Err(FeedError::InvalidInput("poll_interval must not be zero".to_string()));
90 }
91 if let Some(age) = max_snapshot_age {
92 if age.is_zero() {
93 return Err(FeedError::InvalidInput("max_snapshot_age must not be zero".to_string()));
94 }
95 if age <= poll_interval {
96 warn!(
99 max_snapshot_age = ?age,
100 ?poll_interval,
101 "max_snapshot_age does not exceed poll_interval: this feed will serve each \
102 snapshot for max_snapshot_age and nothing until the next poll"
103 );
104 }
105 }
106
107 let snapshots = polled_snapshots(
108 poll_interval,
109 request_timeout,
110 max_consecutive_failures,
111 max_backoff_exp,
112 source,
113 );
114 publisher
115 .publishing(max_snapshot_age, snapshots)
116 .await
117}
118
119pub trait HttpSource: Send + Sync {
122 type Snapshot: Send + Sync + 'static;
125
126 fn fetch(&self) -> impl Future<Output = Result<Self::Snapshot, FeedError>> + Send;
135}
136
137fn polled_snapshots<S: HttpSource>(
147 poll_interval: Duration,
148 request_timeout: Duration,
149 max_consecutive_failures: Option<u32>,
150 max_backoff_exp: u32,
151 source: S,
152) -> impl Stream<Item = Result<S::Snapshot, FeedError>> {
153 let mut failures =
156 FailureTracker::new(max_consecutive_failures, poll_interval, max_backoff_exp);
157
158 let mut ticker = interval(poll_interval);
159 ticker.set_missed_tick_behavior(MissedTickBehavior::Delay);
165
166 let mut backoff = None;
169
170 info!(?poll_interval, "starting polling");
171
172 try_stream! {
173 loop {
174 match backoff.take() {
177 Some(backoff) => {
178 sleep(backoff).await;
179 ticker.reset();
183 }
184 None => {
185 ticker.tick().await;
186 }
187 }
188 let polled = timeout(request_timeout, source.fetch())
189 .instrument(debug_span!("poll"))
190 .await;
191
192 let error = match polled {
193 Ok(Ok(snapshot)) => {
194 let cleared = failures.record_success();
195 if cleared > 0 {
196 info!(after_failures = cleared, "poll succeeded again");
197 }
198 yield snapshot;
199 continue;
200 }
201 Ok(Err(e)) if e.is_fatal() => Err(e)?,
202 Ok(Err(e)) => e,
203 Err(_) => FeedError::Connection(format!(
204 "poll timed out after {request_timeout:?}"
205 )),
206 };
207
208 let retry = failures
209 .record_failure()
210 .inspect(|retry| {
211 warn!(
212 consecutive = retry.consecutive,
213 backoff = ?retry.backoff,
214 %error,
215 "poll failed, backing off"
216 )
217 })
218 .map_err(|consecutive| {
223 error!(consecutive, "giving up on the feed");
224 error
225 })?;
226 backoff = Some(retry.backoff);
227 }
228 }
229}
230
231pub type RefusalCheck = fn(&str) -> bool;
238
239pub fn nothing_refused(_body: &str) -> bool {
241 false
242}
243
244pub(crate) async fn fetch_bytes(
251 request: RequestBuilder,
252 what: &str,
253 refused: RefusalCheck,
254) -> Result<Bytes, FeedError> {
255 let response = request
256 .send()
257 .await
258 .map_err(|e| FeedError::Connection(format!("Failed to fetch {what}: {e}")))?;
259 read_bytes(response, what, refused).await
260}
261
262async fn read_bytes(
265 response: Response,
266 what: &str,
267 refused: RefusalCheck,
268) -> Result<Bytes, FeedError> {
269 let status = response.status();
272 let body = response
273 .bytes()
274 .await
275 .map_err(|e| FeedError::Connection(format!("Failed to read {what} response: {e}")))?;
276
277 if !status.is_success() {
278 let said = String::from_utf8_lossy(&body);
279 let message = format!("{what} HTTP error {status}: {said}");
280 return Err(if refused(&said) {
281 FeedError::Fatal(message)
282 } else {
283 FeedError::Connection(message)
284 });
285 }
286 Ok(body)
287}
288
289pub async fn fetch_json<T: DeserializeOwned>(
292 request: RequestBuilder,
293 what: &str,
294 refused: RefusalCheck,
295) -> Result<T, FeedError> {
296 parse_json(&fetch_bytes(request, what, refused).await?, what)
297}
298
299pub(crate) async fn read_json<T: DeserializeOwned>(
302 response: Response,
303 what: &str,
304 refused: RefusalCheck,
305) -> Result<T, FeedError> {
306 parse_json(&read_bytes(response, what, refused).await?, what)
307}
308
309fn parse_json<T: DeserializeOwned>(body: &[u8], what: &str) -> Result<T, FeedError> {
310 serde_json::from_slice(body)
311 .map_err(|e| FeedError::Parsing(format!("Failed to parse {what} response: {e}")))
312}
313
314#[cfg(test)]
317pub(crate) mod test_support {
318 use std::{
319 net::SocketAddr,
320 sync::{
321 atomic::{AtomicUsize, Ordering},
322 Arc,
323 },
324 time::Duration,
325 };
326
327 use tokio::{
328 io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader},
329 net::TcpListener,
330 };
331
332 pub struct MockResponse {
334 pub status: &'static str,
335 pub body: String,
336 pub delay: Duration,
338 }
339
340 impl From<(&'static str, String)> for MockResponse {
341 fn from((status, body): (&'static str, String)) -> Self {
342 MockResponse { status, body, delay: Duration::ZERO }
343 }
344 }
345
346 pub struct MockRequest {
348 pub target: String,
350 pub body: String,
353 }
354
355 pub struct MockHttpServer {
357 pub address: SocketAddr,
358 pub requests: Arc<AtomicUsize>,
359 }
360
361 impl MockHttpServer {
362 pub fn url(&self) -> String {
363 format!("http://{}", self.address)
364 }
365
366 pub fn request_count(&self) -> usize {
367 self.requests.load(Ordering::SeqCst)
368 }
369 }
370
371 async fn read_body(reader: &mut BufReader<tokio::net::TcpStream>) -> String {
374 let mut length = 0;
375 loop {
376 let mut header = String::new();
377 match reader.read_line(&mut header).await {
378 Ok(0) => break,
379 Err(_) => break,
380 Ok(_) if header.trim().is_empty() => break,
381 Ok(_) => {}
382 }
383 if let Some((name, value)) = header.split_once(':') {
384 if name.eq_ignore_ascii_case("content-length") {
385 length = value.trim().parse().unwrap_or(0);
386 }
387 }
388 }
389 let mut body = vec![0; length];
390 if length > 0 {
391 let _ = reader.read_exact(&mut body).await;
392 }
393 String::from_utf8_lossy(&body).into_owned()
394 }
395
396 pub async fn spawn_http_server<R: Into<MockResponse>>(
404 route: impl Fn(&MockRequest) -> Option<R> + Send + Sync + 'static,
405 ) -> MockHttpServer {
406 let listener = TcpListener::bind("127.0.0.1:0")
407 .await
408 .unwrap();
409 let address = listener.local_addr().unwrap();
410 let requests = Arc::new(AtomicUsize::new(0));
411 let served = Arc::clone(&requests);
412 let route = Arc::new(route);
413 tokio::spawn(async move {
414 while let Ok((stream, _)) = listener.accept().await {
415 let (served, route) = (Arc::clone(&served), Arc::clone(&route));
416 tokio::spawn(async move {
417 let mut reader = BufReader::new(stream);
418 let mut request_line = String::new();
419 if reader
420 .read_line(&mut request_line)
421 .await
422 .is_err()
423 {
424 return;
425 }
426 served.fetch_add(1, Ordering::SeqCst);
427 let request = MockRequest {
428 target: request_line
429 .split_whitespace()
430 .nth(1)
431 .unwrap_or_default()
432 .to_string(),
433 body: read_body(&mut reader).await,
434 };
435 let response = route(&request)
436 .map(Into::into)
437 .unwrap_or_else(|| {
438 MockResponse::from((
439 "404 Not Found",
440 format!("no route for {}", request.target),
441 ))
442 });
443
444 tokio::time::sleep(response.delay).await;
445
446 let MockResponse { status, body, .. } = response;
447 let payload = format!(
448 "HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
449 body.len()
450 );
451 let mut stream = reader.into_inner();
452 let _ = stream
453 .write_all(payload.as_bytes())
454 .await;
455 let _ = stream.shutdown().await;
456 });
457 }
458 });
459 MockHttpServer { address, requests }
460 }
461}
462
463#[cfg(test)]
464mod tests {
465 use std::{
466 future::Future,
467 sync::{
468 atomic::{AtomicBool, AtomicU32, Ordering},
469 Arc,
470 },
471 };
472
473 use futures::StreamExt as _;
474 use tokio::sync::watch;
475 use tokio_stream::wrappers::WatchStream;
476
477 use super::{super::expect_to_finish, *};
478
479 impl<F, Fut, T> HttpSource for F
481 where
482 F: Fn() -> Fut + Send + Sync,
483 Fut: Future<Output = Result<T, FeedError>> + Send,
484 T: Send + Sync + 'static,
485 {
486 type Snapshot = T;
487
488 async fn fetch(&self) -> Result<T, FeedError> {
489 self().await
490 }
491 }
492
493 fn snapshot() -> u32 {
495 1
496 }
497
498 fn config(max_consecutive_failures: Option<u32>) -> HttpFeedConfig {
499 HttpFeedConfig {
500 poll_interval: Duration::from_millis(5),
501 request_timeout: Duration::from_millis(50),
502 max_backoff_exp: 0,
505 max_consecutive_failures,
506 max_snapshot_age: None,
507 }
508 }
509
510 #[tokio::test]
511 async fn publishes_a_snapshot_and_success_resets_failures() {
512 let polls = Arc::new(AtomicU32::new(0));
515 let polls_clone = Arc::clone(&polls);
516 let (publisher, mut rx) = Publisher::channel();
517 let feed = run_http_poll_feed(config(Some(2)), publisher, move || {
518 let n = polls_clone.fetch_add(1, Ordering::SeqCst);
519 async move {
520 if n == 0 {
521 Err(FeedError::Connection("boom".to_string()))
522 } else {
523 Ok(snapshot())
524 }
525 }
526 });
527 let feed = tokio::spawn(feed);
528
529 expect_to_finish("no snapshot published", async {
530 loop {
531 rx.changed().await.expect("feed ended");
532 if rx.borrow_and_update().is_some() {
533 return;
534 }
535 }
536 })
537 .await;
538
539 tokio::time::sleep(Duration::from_millis(50)).await;
541 assert!(!feed.is_finished());
542 }
543
544 #[tokio::test]
545 async fn withdraws_a_snapshot_that_goes_unrefreshed_and_restores_on_next_success() {
546 let polls = Arc::new(AtomicU32::new(0));
550 let polls_clone = Arc::clone(&polls);
551 let recovered = Arc::new(AtomicBool::new(false));
552 let recovered_clone = Arc::clone(&recovered);
553 let (publisher, mut rx) = Publisher::channel();
554 let config =
555 HttpFeedConfig { max_snapshot_age: Some(Duration::from_millis(20)), ..config(None) };
556 let feed = run_http_poll_feed(config, publisher, move || {
557 let n = polls_clone.fetch_add(1, Ordering::SeqCst);
558 let recovered = recovered_clone.load(Ordering::SeqCst);
559 async move {
560 if n == 0 || recovered {
561 Ok(snapshot())
562 } else {
563 Err(FeedError::Connection("boom".to_string()))
564 }
565 }
566 });
567 let _feed = tokio::spawn(feed);
568
569 let wait_for = |rx: &mut watch::Receiver<Option<u32>>, want_some: bool| {
570 let mut rx = rx.clone();
571 async move {
572 expect_to_finish("watch did not reach the expected state", async {
573 loop {
574 if rx.borrow_and_update().is_some() == want_some {
575 return;
576 }
577 rx.changed().await.expect("feed ended");
578 }
579 })
580 .await;
581 }
582 };
583 wait_for(&mut rx, true).await;
584 wait_for(&mut rx, false).await;
585 recovered.store(true, Ordering::SeqCst);
586 wait_for(&mut rx, true).await;
587 }
588
589 #[tokio::test]
590 async fn rejects_zero_poll_interval() {
591 let (publisher, _rx) = Publisher::<u32>::channel();
592 let config = HttpFeedConfig { poll_interval: Duration::ZERO, ..config(None) };
593 let result = run_http_poll_feed(config, publisher, || async { Ok(snapshot()) }).await;
594 assert!(matches!(result, Err(FeedError::InvalidInput(_))));
595 }
596
597 #[tokio::test]
598 async fn rejects_a_zero_max_snapshot_age() {
599 let (publisher, _rx) = Publisher::<u32>::channel();
600 let config = HttpFeedConfig { max_snapshot_age: Some(Duration::ZERO), ..config(None) };
601 let result = run_http_poll_feed(config, publisher, || async { Ok(snapshot()) }).await;
602 assert!(matches!(result, Err(FeedError::InvalidInput(_))));
603 }
604
605 #[tokio::test(start_paused = true)]
610 async fn a_max_snapshot_age_below_the_poll_interval_is_a_duty_cycle() {
611 let (publisher, rx) = Publisher::<u32>::channel();
612 let mut changes = WatchStream::from_changes(rx);
613 let config = HttpFeedConfig {
614 poll_interval: Duration::from_millis(100),
615 max_snapshot_age: Some(Duration::from_millis(20)),
616 ..config(None)
617 };
618 let _feed =
619 tokio::spawn(run_http_poll_feed(config, publisher, || async { Ok(snapshot()) }));
620
621 let start = tokio::time::Instant::now();
622 let mut seen = Vec::new();
623 for _ in 0..4 {
624 let change = expect_to_finish("the feed stopped changing", changes.next()).await;
625 seen.push((change.expect("the feed is still running"), start.elapsed()));
626 }
627
628 assert_eq!(
629 seen,
630 [
631 (Some(snapshot()), Duration::ZERO),
634 (None, Duration::from_millis(20)),
635 (Some(snapshot()), Duration::from_millis(100)),
636 (None, Duration::from_millis(120)),
637 ]
638 );
639 }
640
641 #[tokio::test]
642 async fn gives_up_after_max_consecutive_failed_polls() {
643 let (publisher, _rx) = Publisher::<u32>::channel();
646 let feed = run_http_poll_feed(config(Some(3)), publisher, || async {
647 Err(FeedError::Parsing("not a snapshot".to_string()))
648 });
649 let feed = tokio::spawn(feed);
650
651 let result = expect_to_finish("feed did not give up", feed)
652 .await
653 .unwrap();
654 assert!(matches!(result, Err(FeedError::Parsing(msg)) if msg == "not a snapshot"));
655 }
656
657 #[tokio::test]
658 async fn gives_up_on_a_fatal_poll_error_despite_an_unlimited_failure_budget() {
659 let (publisher, mut rx) = Publisher::channel();
662 let polls = Arc::new(AtomicU32::new(0));
663 let polls_clone = Arc::clone(&polls);
664 let feed = run_http_poll_feed(config(None), publisher, move || {
665 let n = polls_clone.fetch_add(1, Ordering::SeqCst);
666 async move {
667 if n == 0 {
668 Ok(snapshot())
669 } else {
670 Err(FeedError::Fatal("api key rejected".to_string()))
671 }
672 }
673 });
674 let feed = tokio::spawn(feed);
675
676 let result = expect_to_finish("feed did not give up", feed)
677 .await
678 .unwrap();
679
680 assert!(matches!(result, Err(FeedError::Fatal(_))));
681 assert_eq!(polls.load(Ordering::SeqCst), 2, "the feed polled again after the fatal");
682 assert!(rx.borrow_and_update().is_none(), "the snapshot must be withdrawn");
683 }
684
685 #[tokio::test]
686 async fn withdraws_the_published_snapshot_when_it_gives_up() {
687 let polls = Arc::new(AtomicU32::new(0));
690 let polls_clone = Arc::clone(&polls);
691 let (publisher, mut rx) = Publisher::channel();
692 let feed = run_http_poll_feed(config(Some(2)), publisher, move || {
693 let n = polls_clone.fetch_add(1, Ordering::SeqCst);
694 async move {
695 if n == 0 {
696 Ok(snapshot())
697 } else {
698 Err(FeedError::Connection("boom".to_string()))
699 }
700 }
701 });
702 let feed = tokio::spawn(feed);
703
704 expect_to_finish("no snapshot published", async {
705 loop {
706 rx.changed()
707 .await
708 .expect("feed ended before publishing");
709 if rx.borrow_and_update().is_some() {
710 return;
711 }
712 }
713 })
714 .await;
715
716 let result = expect_to_finish("feed did not give up", feed)
717 .await
718 .unwrap();
719 assert!(matches!(result, Err(FeedError::Connection(_))));
720 assert!(rx.borrow().is_none(), "the snapshot must be withdrawn when the feed gives up");
721 }
722
723 #[tokio::test]
724 async fn withdraws_the_published_snapshot_when_the_feed_is_dropped() {
725 let (publisher, mut rx) = Publisher::channel();
728 let feed = run_http_poll_feed(config(None), publisher, || async { Ok(snapshot()) });
729 let feed = tokio::spawn(feed);
730
731 expect_to_finish("no snapshot published", async {
732 loop {
733 rx.changed()
734 .await
735 .expect("feed ended before publishing");
736 if rx.borrow_and_update().is_some() {
737 return;
738 }
739 }
740 })
741 .await;
742
743 feed.abort();
744 expect_to_finish("the snapshot was not withdrawn", rx.changed())
745 .await
746 .expect("the withdrawal must reach the receiver before the sender drops");
747 assert!(rx.borrow().is_none());
748 }
749
750 #[tokio::test(start_paused = true)]
751 async fn failing_polls_stretch_the_polling_cadence() {
752 let attempts = Arc::new(std::sync::Mutex::new(Vec::new()));
756 let attempts_clone = Arc::clone(&attempts);
757 let start = tokio::time::Instant::now();
758 let (publisher, _rx) = Publisher::<u32>::channel();
759 let feed = tokio::spawn(run_http_poll_feed(
760 HttpFeedConfig {
761 poll_interval: Duration::from_millis(50),
762 max_backoff_exp: 3,
763 ..config(None)
764 },
765 publisher,
766 move || {
767 attempts_clone
768 .lock()
769 .unwrap()
770 .push(start.elapsed().as_millis());
771 async { Err(FeedError::Connection("boom".to_string())) }
772 },
773 ));
774
775 tokio::time::sleep(Duration::from_secs(1)).await;
776 feed.abort();
777
778 assert_eq!(*attempts.lock().unwrap(), vec![0, 50, 150, 350, 750]);
779 }
780
781 #[tokio::test(start_paused = true)]
782 async fn a_recovered_poll_resumes_the_cadence_instead_of_catching_up() {
783 let attempts = Arc::new(std::sync::Mutex::new(Vec::new()));
787 let attempts_clone = Arc::clone(&attempts);
788 let start = tokio::time::Instant::now();
789 let (publisher, _rx) = Publisher::<u32>::channel();
790 let feed = tokio::spawn(run_http_poll_feed(
791 HttpFeedConfig {
792 poll_interval: Duration::from_millis(50),
793 max_backoff_exp: 3,
794 ..config(None)
795 },
796 publisher,
797 move || {
798 let mut attempts = attempts_clone.lock().unwrap();
799 attempts.push(start.elapsed().as_millis());
800 let failing = attempts.len() <= 2;
801 async move {
802 if failing {
803 Err(FeedError::Connection("boom".to_string()))
804 } else {
805 Ok(snapshot())
806 }
807 }
808 },
809 ));
810
811 tokio::time::sleep(Duration::from_millis(300)).await;
812 feed.abort();
813
814 assert_eq!(*attempts.lock().unwrap(), vec![0, 50, 150, 200, 250]);
815 }
816
817 #[tokio::test(start_paused = true)]
818 async fn a_slow_poll_never_shortens_the_gap_to_the_next_one() {
819 let attempts = Arc::new(std::sync::Mutex::new(Vec::new()));
824 let attempts_clone = Arc::clone(&attempts);
825 let start = tokio::time::Instant::now();
826 let (publisher, _rx) = Publisher::<u32>::channel();
827 let feed = tokio::spawn(run_http_poll_feed(
828 HttpFeedConfig {
829 poll_interval: Duration::from_millis(50),
830 request_timeout: Duration::from_millis(500),
831 ..config(None)
832 },
833 publisher,
834 move || {
835 let mut attempts = attempts_clone.lock().unwrap();
836 attempts.push(start.elapsed().as_millis());
837 let slow = attempts.len() == 3;
838 async move {
839 if slow {
840 tokio::time::sleep(Duration::from_millis(120)).await;
841 }
842 Ok(snapshot())
843 }
844 },
845 ));
846
847 tokio::time::sleep(Duration::from_millis(350)).await;
848 feed.abort();
849
850 assert_eq!(*attempts.lock().unwrap(), vec![0, 50, 100, 220, 270, 320]);
851 }
852
853 #[tokio::test]
854 async fn retries_forever_without_limit() {
855 let polls = Arc::new(AtomicU32::new(0));
856 let polls_clone = Arc::clone(&polls);
857 let (publisher, _rx) = Publisher::<u32>::channel();
858 let feed = run_http_poll_feed(config(None), publisher, move || {
859 polls_clone.fetch_add(1, Ordering::SeqCst);
860 async { Err(FeedError::Connection("boom".to_string())) }
861 });
862 let feed = tokio::spawn(feed);
863
864 tokio::time::sleep(Duration::from_millis(100)).await;
865 assert!(polls.load(Ordering::SeqCst) > 5, "should keep polling");
866 assert!(!feed.is_finished());
867 }
868
869 #[tokio::test]
870 async fn hung_poll_counts_as_failure() {
871 let (publisher, _rx) = Publisher::<u32>::channel();
872 let feed = run_http_poll_feed(
873 HttpFeedConfig { request_timeout: Duration::from_millis(5), ..config(Some(2)) },
874 publisher,
875 || async {
876 tokio::time::sleep(Duration::from_secs(3600)).await;
877 Ok(snapshot())
878 },
879 );
880 let feed = tokio::spawn(feed);
881
882 let result = expect_to_finish("feed did not give up", feed)
883 .await
884 .unwrap();
885 assert!(matches!(result, Err(FeedError::Connection(msg)) if msg.contains("timed out")));
886 }
887
888 mod fetch_json {
889
890 use rstest::rstest;
891 use serde::Deserialize;
892
893 use super::{test_support::spawn_http_server, *};
894
895 #[derive(Debug, Deserialize, PartialEq)]
896 struct Payload {
897 value: u32,
898 }
899
900 #[tokio::test]
901 async fn parses_a_successful_json_body() {
902 let server =
903 spawn_http_server(|_| Some(("200 OK", r#"{"value":7}"#.to_string()))).await;
904
905 let payload: Payload = fetch_json(
906 reqwest::Client::new().get(format!("{}/x", server.url())),
907 "thing",
908 nothing_refused,
909 )
910 .await
911 .unwrap();
912
913 assert_eq!(payload, Payload { value: 7 });
914 }
915
916 #[rstest]
920 #[case::non_success_status_with_body("503 Service Unavailable", "maintenance", false)]
921 #[case::unparseable_success_body("200 OK", "<html>", true)]
922 #[tokio::test]
923 async fn classifies_status_and_body_failures(
924 #[case] status: &'static str,
925 #[case] body: &'static str,
926 #[case] expect_parsing_error: bool,
927 ) {
928 let server = spawn_http_server(move |_| Some((status, body.to_string()))).await;
929
930 let result: Result<Payload, FeedError> = fetch_json(
931 reqwest::Client::new().get(format!("{}/x", server.url())),
932 "thing",
933 nothing_refused,
934 )
935 .await;
936
937 match result {
938 Err(FeedError::Parsing(msg)) if expect_parsing_error => {
939 assert!(msg.contains("thing"), "{msg}")
940 }
941 Err(FeedError::Connection(msg)) if !expect_parsing_error => {
942 assert!(
943 msg.contains("thing") && msg.contains("503") && msg.contains(body),
944 "{msg}"
945 )
946 }
947 other => panic!("unexpected result: {other:?}"),
948 }
949 }
950
951 #[tokio::test]
952 async fn refused_connection_is_a_connection_error() {
953 let address = tokio::net::TcpListener::bind("127.0.0.1:0")
955 .await
956 .unwrap()
957 .local_addr()
958 .unwrap();
959
960 let result: Result<Payload, FeedError> = fetch_json(
961 reqwest::Client::new().get(format!("http://{address}/x")),
962 "thing",
963 nothing_refused,
964 )
965 .await;
966
967 assert!(matches!(result, Err(FeedError::Connection(msg)) if msg.contains("thing")));
968 }
969 }
970}