|
| 1 | +//! Test for crates.io API health |
| 2 | +
|
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use async_trait::async_trait; |
| 6 | + |
| 7 | +use crate::http_client::custom_http_client; |
| 8 | +use crate::test::{Test, TestResult}; |
| 9 | + |
| 10 | +use super::config::Config; |
| 11 | + |
| 12 | +/// The name of the test |
| 13 | +const NAME: &str = "crates.io API"; |
| 14 | + |
| 15 | +/// Test that the crates.io API is accessible |
| 16 | +pub struct ApiHealth { |
| 17 | + /// Configuration for this test |
| 18 | + config: Arc<Config>, |
| 19 | +} |
| 20 | + |
| 21 | +impl ApiHealth { |
| 22 | + /// Create a new instance of the test |
| 23 | + pub fn new(config: Arc<Config>) -> Self { |
| 24 | + Self { config } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[async_trait] |
| 29 | +impl Test for ApiHealth { |
| 30 | + async fn run(&self) -> TestResult { |
| 31 | + let response = match custom_http_client() |
| 32 | + .build() |
| 33 | + .expect("failed to build reqwest client") |
| 34 | + .get(self.config.api_url()) |
| 35 | + .send() |
| 36 | + .await |
| 37 | + { |
| 38 | + Ok(response) => response, |
| 39 | + Err(error) => { |
| 40 | + return TestResult::builder() |
| 41 | + .name(NAME) |
| 42 | + .success(false) |
| 43 | + .message(Some(error.to_string())) |
| 44 | + .build() |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + if response.status().is_success() { |
| 49 | + TestResult::builder().name(NAME).success(true).build() |
| 50 | + } else { |
| 51 | + TestResult::builder() |
| 52 | + .name(NAME) |
| 53 | + .success(false) |
| 54 | + .message(Some(format!( |
| 55 | + "Expected HTTP 200, got HTTP {}", |
| 56 | + response.status() |
| 57 | + ))) |
| 58 | + .build() |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[cfg(test)] |
| 64 | +mod tests { |
| 65 | + use mockito::ServerGuard; |
| 66 | + |
| 67 | + use crate::test_utils::*; |
| 68 | + |
| 69 | + use super::*; |
| 70 | + |
| 71 | + pub async fn setup() -> (ServerGuard, Config) { |
| 72 | + let server = mockito::Server::new_async().await; |
| 73 | + |
| 74 | + let config = Config::builder().api_url(server.url()).build(); |
| 75 | + |
| 76 | + (server, config) |
| 77 | + } |
| 78 | + |
| 79 | + #[tokio::test] |
| 80 | + async fn succeeds_with_http_200_response() { |
| 81 | + let (mut server, config) = setup().await; |
| 82 | + |
| 83 | + let mock = server.mock("GET", "/").with_status(200).create(); |
| 84 | + |
| 85 | + let result = ApiHealth::new(Arc::new(config)).run().await; |
| 86 | + |
| 87 | + // Assert that the mock was called |
| 88 | + mock.assert(); |
| 89 | + |
| 90 | + assert!(result.success()); |
| 91 | + } |
| 92 | + |
| 93 | + #[tokio::test] |
| 94 | + async fn fails_with_other_http_responses() { |
| 95 | + let (mut server, config) = setup().await; |
| 96 | + |
| 97 | + let mock = server.mock("GET", "/").with_status(500).create(); |
| 98 | + |
| 99 | + let result = ApiHealth::new(Arc::new(config)).run().await; |
| 100 | + |
| 101 | + // Assert that the mock was called |
| 102 | + mock.assert(); |
| 103 | + |
| 104 | + assert!(!result.success()); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn trait_send() { |
| 109 | + assert_send::<ApiHealth>(); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn trait_sync() { |
| 114 | + assert_sync::<ApiHealth>(); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn trait_unpin() { |
| 119 | + assert_unpin::<ApiHealth>(); |
| 120 | + } |
| 121 | +} |
0 commit comments