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 #[expect(
392 clippy::let_underscore_must_use,
393 reason = "the fake peer may already be gone; that is not a test failure"
394 )]
395 let _ = reader.read_exact(&mut body).await;
396 }
397 String::from_utf8_lossy(&body).into_owned()
398 }
399
400 pub async fn spawn_http_server<R: Into<MockResponse>>(
408 route: impl Fn(&MockRequest) -> Option<R> + Send + Sync + 'static,
409 ) -> MockHttpServer {
410 let listener = TcpListener::bind("127.0.0.1:0")
411 .await
412 .unwrap();
413 let address = listener.local_addr().unwrap();
414 let requests = Arc::new(AtomicUsize::new(0));
415 let served = Arc::clone(&requests);
416 let route = Arc::new(route);
417 tokio::spawn(async move {
418 while let Ok((stream, _)) = listener.accept().await {
419 let (served, route) = (Arc::clone(&served), Arc::clone(&route));
420 tokio::spawn(async move {
421 let mut reader = BufReader::new(stream);
422 let mut request_line = String::new();
423 if reader
424 .read_line(&mut request_line)
425 .await
426 .is_err()
427 {
428 return;
429 }
430 served.fetch_add(1, Ordering::SeqCst);
431 let request = MockRequest {
432 target: request_line
433 .split_whitespace()
434 .nth(1)
435 .unwrap_or_default()
436 .to_string(),
437 body: read_body(&mut reader).await,
438 };
439 let response = route(&request)
440 .map(Into::into)
441 .unwrap_or_else(|| {
442 MockResponse::from((
443 "404 Not Found",
444 format!("no route for {}", request.target),
445 ))
446 });
447
448 tokio::time::sleep(response.delay).await;
449
450 let MockResponse { status, body, .. } = response;
451 let payload = format!(
452 "HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
453 body.len()
454 );
455 let mut stream = reader.into_inner();
456 #[expect(
457 clippy::let_underscore_must_use,
458 reason = "the fake peer may already be gone; that is not a test failure"
459 )]
460 let _ = stream
461 .write_all(payload.as_bytes())
462 .await;
463 #[expect(
464 clippy::let_underscore_must_use,
465 reason = "the fake peer may already be gone; that is not a test failure"
466 )]
467 let _ = stream.shutdown().await;
468 });
469 }
470 });
471 MockHttpServer { address, requests }
472 }
473}
474
475#[cfg(test)]
476mod tests {
477 use std::{
478 future::Future,
479 sync::{
480 atomic::{AtomicBool, AtomicU32, Ordering},
481 Arc,
482 },
483 };
484
485 use futures::StreamExt as _;
486 use tokio::sync::watch;
487 use tokio_stream::wrappers::WatchStream;
488
489 use super::{super::expect_to_finish, *};
490
491 impl<F, Fut, T> HttpSource for F
493 where
494 F: Fn() -> Fut + Send + Sync,
495 Fut: Future<Output = Result<T, FeedError>> + Send,
496 T: Send + Sync + 'static,
497 {
498 type Snapshot = T;
499
500 async fn fetch(&self) -> Result<T, FeedError> {
501 self().await
502 }
503 }
504
505 fn snapshot() -> u32 {
507 1
508 }
509
510 fn config(max_consecutive_failures: Option<u32>) -> HttpFeedConfig {
511 HttpFeedConfig {
512 poll_interval: Duration::from_millis(5),
513 request_timeout: Duration::from_millis(50),
514 max_backoff_exp: 0,
517 max_consecutive_failures,
518 max_snapshot_age: None,
519 }
520 }
521
522 #[tokio::test]
523 async fn publishes_a_snapshot_and_success_resets_failures() {
524 let polls = Arc::new(AtomicU32::new(0));
527 let polls_clone = Arc::clone(&polls);
528 let (publisher, mut rx) = Publisher::channel();
529 let feed = run_http_poll_feed(config(Some(2)), publisher, move || {
530 let n = polls_clone.fetch_add(1, Ordering::SeqCst);
531 async move {
532 if n == 0 {
533 Err(FeedError::Connection("boom".to_string()))
534 } else {
535 Ok(snapshot())
536 }
537 }
538 });
539 let feed = tokio::spawn(feed);
540
541 expect_to_finish("no snapshot published", async {
542 loop {
543 rx.changed().await.expect("feed ended");
544 if rx.borrow_and_update().is_some() {
545 return;
546 }
547 }
548 })
549 .await;
550
551 tokio::time::sleep(Duration::from_millis(50)).await;
553 assert!(!feed.is_finished());
554 }
555
556 #[tokio::test]
557 async fn withdraws_a_snapshot_that_goes_unrefreshed_and_restores_on_next_success() {
558 let polls = Arc::new(AtomicU32::new(0));
562 let polls_clone = Arc::clone(&polls);
563 let recovered = Arc::new(AtomicBool::new(false));
564 let recovered_clone = Arc::clone(&recovered);
565 let (publisher, mut rx) = Publisher::channel();
566 let config =
567 HttpFeedConfig { max_snapshot_age: Some(Duration::from_millis(20)), ..config(None) };
568 let feed = run_http_poll_feed(config, publisher, move || {
569 let n = polls_clone.fetch_add(1, Ordering::SeqCst);
570 let recovered = recovered_clone.load(Ordering::SeqCst);
571 async move {
572 if n == 0 || recovered {
573 Ok(snapshot())
574 } else {
575 Err(FeedError::Connection("boom".to_string()))
576 }
577 }
578 });
579 let _feed = tokio::spawn(feed);
580
581 let wait_for = |rx: &mut watch::Receiver<Option<u32>>, want_some: bool| {
582 let mut rx = rx.clone();
583 async move {
584 expect_to_finish("watch did not reach the expected state", async {
585 loop {
586 if rx.borrow_and_update().is_some() == want_some {
587 return;
588 }
589 rx.changed().await.expect("feed ended");
590 }
591 })
592 .await;
593 }
594 };
595 wait_for(&mut rx, true).await;
596 wait_for(&mut rx, false).await;
597 recovered.store(true, Ordering::SeqCst);
598 wait_for(&mut rx, true).await;
599 }
600
601 #[tokio::test]
602 async fn rejects_zero_poll_interval() {
603 let (publisher, _rx) = Publisher::<u32>::channel();
604 let config = HttpFeedConfig { poll_interval: Duration::ZERO, ..config(None) };
605 let result = run_http_poll_feed(config, publisher, || async { Ok(snapshot()) }).await;
606 assert!(matches!(result, Err(FeedError::InvalidInput(_))));
607 }
608
609 #[tokio::test]
610 async fn rejects_a_zero_max_snapshot_age() {
611 let (publisher, _rx) = Publisher::<u32>::channel();
612 let config = HttpFeedConfig { max_snapshot_age: Some(Duration::ZERO), ..config(None) };
613 let result = run_http_poll_feed(config, publisher, || async { Ok(snapshot()) }).await;
614 assert!(matches!(result, Err(FeedError::InvalidInput(_))));
615 }
616
617 #[tokio::test(start_paused = true)]
622 async fn a_max_snapshot_age_below_the_poll_interval_is_a_duty_cycle() {
623 let (publisher, rx) = Publisher::<u32>::channel();
624 let mut changes = WatchStream::from_changes(rx);
625 let config = HttpFeedConfig {
626 poll_interval: Duration::from_millis(100),
627 max_snapshot_age: Some(Duration::from_millis(20)),
628 ..config(None)
629 };
630 let _feed =
631 tokio::spawn(run_http_poll_feed(config, publisher, || async { Ok(snapshot()) }));
632
633 let start = tokio::time::Instant::now();
634 let mut seen = Vec::new();
635 for _ in 0..4 {
636 let change = expect_to_finish("the feed stopped changing", changes.next()).await;
637 seen.push((change.expect("the feed is still running"), start.elapsed()));
638 }
639
640 assert_eq!(
641 seen,
642 [
643 (Some(snapshot()), Duration::ZERO),
646 (None, Duration::from_millis(20)),
647 (Some(snapshot()), Duration::from_millis(100)),
648 (None, Duration::from_millis(120)),
649 ]
650 );
651 }
652
653 #[tokio::test]
654 async fn gives_up_after_max_consecutive_failed_polls() {
655 let (publisher, _rx) = Publisher::<u32>::channel();
658 let feed = run_http_poll_feed(config(Some(3)), publisher, || async {
659 Err(FeedError::Parsing("not a snapshot".to_string()))
660 });
661 let feed = tokio::spawn(feed);
662
663 let result = expect_to_finish("feed did not give up", feed)
664 .await
665 .unwrap();
666 assert!(matches!(result, Err(FeedError::Parsing(msg)) if msg == "not a snapshot"));
667 }
668
669 #[tokio::test]
670 async fn gives_up_on_a_fatal_poll_error_despite_an_unlimited_failure_budget() {
671 let (publisher, mut rx) = Publisher::channel();
674 let polls = Arc::new(AtomicU32::new(0));
675 let polls_clone = Arc::clone(&polls);
676 let feed = run_http_poll_feed(config(None), publisher, move || {
677 let n = polls_clone.fetch_add(1, Ordering::SeqCst);
678 async move {
679 if n == 0 {
680 Ok(snapshot())
681 } else {
682 Err(FeedError::Fatal("api key rejected".to_string()))
683 }
684 }
685 });
686 let feed = tokio::spawn(feed);
687
688 let result = expect_to_finish("feed did not give up", feed)
689 .await
690 .unwrap();
691
692 assert!(matches!(result, Err(FeedError::Fatal(_))));
693 assert_eq!(polls.load(Ordering::SeqCst), 2, "the feed polled again after the fatal");
694 assert!(rx.borrow_and_update().is_none(), "the snapshot must be withdrawn");
695 }
696
697 #[tokio::test]
698 async fn withdraws_the_published_snapshot_when_it_gives_up() {
699 let polls = Arc::new(AtomicU32::new(0));
702 let polls_clone = Arc::clone(&polls);
703 let (publisher, mut rx) = Publisher::channel();
704 let feed = run_http_poll_feed(config(Some(2)), publisher, move || {
705 let n = polls_clone.fetch_add(1, Ordering::SeqCst);
706 async move {
707 if n == 0 {
708 Ok(snapshot())
709 } else {
710 Err(FeedError::Connection("boom".to_string()))
711 }
712 }
713 });
714 let feed = tokio::spawn(feed);
715
716 expect_to_finish("no snapshot published", async {
717 loop {
718 rx.changed()
719 .await
720 .expect("feed ended before publishing");
721 if rx.borrow_and_update().is_some() {
722 return;
723 }
724 }
725 })
726 .await;
727
728 let result = expect_to_finish("feed did not give up", feed)
729 .await
730 .unwrap();
731 assert!(matches!(result, Err(FeedError::Connection(_))));
732 assert!(rx.borrow().is_none(), "the snapshot must be withdrawn when the feed gives up");
733 }
734
735 #[tokio::test]
736 async fn withdraws_the_published_snapshot_when_the_feed_is_dropped() {
737 let (publisher, mut rx) = Publisher::channel();
740 let feed = run_http_poll_feed(config(None), publisher, || async { Ok(snapshot()) });
741 let feed = tokio::spawn(feed);
742
743 expect_to_finish("no snapshot published", async {
744 loop {
745 rx.changed()
746 .await
747 .expect("feed ended before publishing");
748 if rx.borrow_and_update().is_some() {
749 return;
750 }
751 }
752 })
753 .await;
754
755 feed.abort();
756 expect_to_finish("the snapshot was not withdrawn", rx.changed())
757 .await
758 .expect("the withdrawal must reach the receiver before the sender drops");
759 assert!(rx.borrow().is_none());
760 }
761
762 #[tokio::test(start_paused = true)]
763 async fn failing_polls_stretch_the_polling_cadence() {
764 let attempts = Arc::new(std::sync::Mutex::new(Vec::new()));
768 let attempts_clone = Arc::clone(&attempts);
769 let start = tokio::time::Instant::now();
770 let (publisher, _rx) = Publisher::<u32>::channel();
771 let feed = tokio::spawn(run_http_poll_feed(
772 HttpFeedConfig {
773 poll_interval: Duration::from_millis(50),
774 max_backoff_exp: 3,
775 ..config(None)
776 },
777 publisher,
778 move || {
779 attempts_clone
780 .lock()
781 .unwrap()
782 .push(start.elapsed().as_millis());
783 async { Err(FeedError::Connection("boom".to_string())) }
784 },
785 ));
786
787 tokio::time::sleep(Duration::from_secs(1)).await;
788 feed.abort();
789
790 assert_eq!(*attempts.lock().unwrap(), vec![0, 50, 150, 350, 750]);
791 }
792
793 #[tokio::test(start_paused = true)]
794 async fn a_recovered_poll_resumes_the_cadence_instead_of_catching_up() {
795 let attempts = Arc::new(std::sync::Mutex::new(Vec::new()));
799 let attempts_clone = Arc::clone(&attempts);
800 let start = tokio::time::Instant::now();
801 let (publisher, _rx) = Publisher::<u32>::channel();
802 let feed = tokio::spawn(run_http_poll_feed(
803 HttpFeedConfig {
804 poll_interval: Duration::from_millis(50),
805 max_backoff_exp: 3,
806 ..config(None)
807 },
808 publisher,
809 move || {
810 let mut attempts = attempts_clone.lock().unwrap();
811 attempts.push(start.elapsed().as_millis());
812 let failing = attempts.len() <= 2;
813 async move {
814 if failing {
815 Err(FeedError::Connection("boom".to_string()))
816 } else {
817 Ok(snapshot())
818 }
819 }
820 },
821 ));
822
823 tokio::time::sleep(Duration::from_millis(300)).await;
824 feed.abort();
825
826 assert_eq!(*attempts.lock().unwrap(), vec![0, 50, 150, 200, 250]);
827 }
828
829 #[tokio::test(start_paused = true)]
830 async fn a_slow_poll_never_shortens_the_gap_to_the_next_one() {
831 let attempts = Arc::new(std::sync::Mutex::new(Vec::new()));
836 let attempts_clone = Arc::clone(&attempts);
837 let start = tokio::time::Instant::now();
838 let (publisher, _rx) = Publisher::<u32>::channel();
839 let feed = tokio::spawn(run_http_poll_feed(
840 HttpFeedConfig {
841 poll_interval: Duration::from_millis(50),
842 request_timeout: Duration::from_millis(500),
843 ..config(None)
844 },
845 publisher,
846 move || {
847 let mut attempts = attempts_clone.lock().unwrap();
848 attempts.push(start.elapsed().as_millis());
849 let slow = attempts.len() == 3;
850 async move {
851 if slow {
852 tokio::time::sleep(Duration::from_millis(120)).await;
853 }
854 Ok(snapshot())
855 }
856 },
857 ));
858
859 tokio::time::sleep(Duration::from_millis(350)).await;
860 feed.abort();
861
862 assert_eq!(*attempts.lock().unwrap(), vec![0, 50, 100, 220, 270, 320]);
863 }
864
865 #[tokio::test]
866 async fn retries_forever_without_limit() {
867 let polls = Arc::new(AtomicU32::new(0));
868 let polls_clone = Arc::clone(&polls);
869 let (publisher, _rx) = Publisher::<u32>::channel();
870 let feed = run_http_poll_feed(config(None), publisher, move || {
871 polls_clone.fetch_add(1, Ordering::SeqCst);
872 async { Err(FeedError::Connection("boom".to_string())) }
873 });
874 let feed = tokio::spawn(feed);
875
876 tokio::time::sleep(Duration::from_millis(100)).await;
877 assert!(polls.load(Ordering::SeqCst) > 5, "should keep polling");
878 assert!(!feed.is_finished());
879 }
880
881 #[tokio::test]
882 async fn hung_poll_counts_as_failure() {
883 let (publisher, _rx) = Publisher::<u32>::channel();
884 let feed = run_http_poll_feed(
885 HttpFeedConfig { request_timeout: Duration::from_millis(5), ..config(Some(2)) },
886 publisher,
887 || async {
888 tokio::time::sleep(Duration::from_secs(3600)).await;
889 Ok(snapshot())
890 },
891 );
892 let feed = tokio::spawn(feed);
893
894 let result = expect_to_finish("feed did not give up", feed)
895 .await
896 .unwrap();
897 assert!(matches!(result, Err(FeedError::Connection(msg)) if msg.contains("timed out")));
898 }
899
900 mod fetch_json {
901
902 use rstest::rstest;
903 use serde::Deserialize;
904
905 use super::{test_support::spawn_http_server, *};
906
907 #[derive(Debug, Deserialize, PartialEq)]
908 struct Payload {
909 value: u32,
910 }
911
912 #[tokio::test]
913 async fn parses_a_successful_json_body() {
914 let server =
915 spawn_http_server(|_| Some(("200 OK", r#"{"value":7}"#.to_string()))).await;
916
917 let payload: Payload = fetch_json(
918 reqwest::Client::new().get(format!("{}/x", server.url())),
919 "thing",
920 nothing_refused,
921 )
922 .await
923 .unwrap();
924
925 assert_eq!(payload, Payload { value: 7 });
926 }
927
928 #[rstest]
932 #[case::non_success_status_with_body("503 Service Unavailable", "maintenance", false)]
933 #[case::unparseable_success_body("200 OK", "<html>", true)]
934 #[tokio::test]
935 async fn classifies_status_and_body_failures(
936 #[case] status: &'static str,
937 #[case] body: &'static str,
938 #[case] expect_parsing_error: bool,
939 ) {
940 let server = spawn_http_server(move |_| Some((status, body.to_string()))).await;
941
942 let result: Result<Payload, FeedError> = fetch_json(
943 reqwest::Client::new().get(format!("{}/x", server.url())),
944 "thing",
945 nothing_refused,
946 )
947 .await;
948
949 match result {
950 Err(FeedError::Parsing(msg)) if expect_parsing_error => {
951 assert!(msg.contains("thing"), "{msg}")
952 }
953 Err(FeedError::Connection(msg)) if !expect_parsing_error => {
954 assert!(
955 msg.contains("thing") && msg.contains("503") && msg.contains(body),
956 "{msg}"
957 )
958 }
959 other => panic!("unexpected result: {other:?}"),
960 }
961 }
962
963 #[tokio::test]
964 async fn refused_connection_is_a_connection_error() {
965 let address = tokio::net::TcpListener::bind("127.0.0.1:0")
967 .await
968 .unwrap()
969 .local_addr()
970 .unwrap();
971
972 let result: Result<Payload, FeedError> = fetch_json(
973 reqwest::Client::new().get(format!("http://{address}/x")),
974 "thing",
975 nothing_refused,
976 )
977 .await;
978
979 assert!(matches!(result, Err(FeedError::Connection(msg)) if msg.contains("thing")));
980 }
981 }
982}