use crate::api::platform::Platform;
use crate::api::version::Version;
use crate::api::{API_BASE_URL, Download, DownloadsByPlatform, fetch_endpoint};
use serde::{Deserialize, Serialize};
const KNOWN_GOOD_VERSIONS_WITH_DOWNLOADS_JSON_PATH: &str =
"/chrome-for-testing/known-good-versions-with-downloads.json";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Downloads {
pub chrome: Vec<Download>,
pub chromedriver: Option<Vec<Download>>,
#[serde(rename = "chrome-headless-shell")]
pub chrome_headless_shell: Option<Vec<Download>>,
}
impl Downloads {
#[must_use]
pub fn chrome_for_platform(&self, platform: Platform) -> Option<&Download> {
self.chrome.for_platform(platform)
}
#[must_use]
pub fn chromedriver_for_platform(&self, platform: Platform) -> Option<&Download> {
self.chromedriver.as_deref()?.for_platform(platform)
}
#[must_use]
pub fn chrome_headless_shell_for_platform(&self, platform: Platform) -> Option<&Download> {
self.chrome_headless_shell
.as_deref()?
.for_platform(platform)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct VersionWithoutChannel {
pub version: Version,
pub revision: String,
pub downloads: Downloads,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct KnownGoodVersions {
#[serde(with = "time::serde::rfc3339")]
pub timestamp: time::OffsetDateTime,
pub versions: Vec<VersionWithoutChannel>,
}
impl KnownGoodVersions {
pub async fn fetch(client: &reqwest::Client) -> crate::Result<Self> {
Self::fetch_with_base_url(client, &API_BASE_URL).await
}
pub async fn fetch_with_base_url(
client: &reqwest::Client,
base_url: &reqwest::Url,
) -> crate::Result<Self> {
fetch_endpoint::<Self>(
client,
base_url,
KNOWN_GOOD_VERSIONS_WITH_DOWNLOADS_JSON_PATH,
"KnownGoodVersions",
)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::api::Download;
use crate::api::known_good_versions::KnownGoodVersions;
use crate::api::platform::Platform;
use crate::api::version::Version;
use crate::error::Error;
use assertr::prelude::*;
use time::macros::datetime;
use url::Url;
#[tokio::test]
async fn can_request_from_real_world_endpoint() {
let result = KnownGoodVersions::fetch(&reqwest::Client::new()).await;
assert_that!(result).is_ok();
}
#[tokio::test]
async fn can_query_known_good_versions_api_endpoint_and_deserialize_response() {
let mut server = mockito::Server::new_async().await;
let _mock = server
.mock("GET", KNOWN_GOOD_VERSIONS_WITH_DOWNLOADS_JSON_PATH)
.with_status(200)
.with_header("content-type", "application/json")
.with_body(include_str!(
"./../../test-data/known_good_versions_with_downloads_test_response.json"
))
.create();
let mock_url: Url = server.url().parse().unwrap();
let data = KnownGoodVersions::fetch_with_base_url(&reqwest::Client::new(), &mock_url)
.await
.unwrap();
assert_that!(data).is_equal_to(KnownGoodVersions {
timestamp: datetime!(2026-04-13 08:53:52.847 UTC),
versions: vec![
VersionWithoutChannel {
version: Version { major: 113, minor: 0, patch: 5672, build: 0 },
revision: String::from("1121455"),
downloads: Downloads {
chrome: vec![
Download { platform: Platform::Linux64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/113.0.5672.0/linux64/chrome-linux64.zip") },
Download { platform: Platform::MacArm64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/113.0.5672.0/mac-arm64/chrome-mac-arm64.zip") },
Download { platform: Platform::MacX64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/113.0.5672.0/mac-x64/chrome-mac-x64.zip") },
Download { platform: Platform::Win32, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/113.0.5672.0/win32/chrome-win32.zip") },
Download { platform: Platform::Win64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/113.0.5672.0/win64/chrome-win64.zip") },
],
chromedriver: None,
chrome_headless_shell: None,
},
},
VersionWithoutChannel {
version: Version { major: 149, minor: 0, patch: 7789, build: 0 },
revision: String::from("1613465"),
downloads: Downloads {
chrome: vec![
Download { platform: Platform::Linux64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/linux64/chrome-linux64.zip") },
Download { platform: Platform::MacArm64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/mac-arm64/chrome-mac-arm64.zip") },
Download { platform: Platform::MacX64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/mac-x64/chrome-mac-x64.zip") },
Download { platform: Platform::Win32, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/win32/chrome-win32.zip") },
Download { platform: Platform::Win64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/win64/chrome-win64.zip") },
],
chromedriver: Some(vec![
Download { platform: Platform::Linux64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/linux64/chromedriver-linux64.zip") },
Download { platform: Platform::MacArm64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/mac-arm64/chromedriver-mac-arm64.zip") },
Download { platform: Platform::MacX64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/mac-x64/chromedriver-mac-x64.zip") },
Download { platform: Platform::Win32, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/win32/chromedriver-win32.zip") },
Download { platform: Platform::Win64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/win64/chromedriver-win64.zip") },
]),
chrome_headless_shell: Some(vec![
Download { platform: Platform::Linux64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/linux64/chrome-headless-shell-linux64.zip") },
Download { platform: Platform::MacArm64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/mac-arm64/chrome-headless-shell-mac-arm64.zip") },
Download { platform: Platform::MacX64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/mac-x64/chrome-headless-shell-mac-x64.zip") },
Download { platform: Platform::Win32, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/win32/chrome-headless-shell-win32.zip") },
Download { platform: Platform::Win64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/149.0.7789.0/win64/chrome-headless-shell-win64.zip") },
]),
},
},
],
});
}
#[tokio::test]
async fn unsuccessful_http_status_is_reported_as_request_error() {
let mut server = mockito::Server::new_async().await;
let _mock = server
.mock("GET", KNOWN_GOOD_VERSIONS_WITH_DOWNLOADS_JSON_PATH)
.with_status(500)
.with_header("content-type", "application/json")
.with_body(include_str!(
"./../../test-data/known_good_versions_with_downloads_test_response.json"
))
.create();
let url: Url = server.url().parse().unwrap();
let err = KnownGoodVersions::fetch_with_base_url(&reqwest::Client::new(), &url)
.await
.unwrap_err();
let Error::Request(request_error) = err.current_context() else {
panic!("expected request error, got: {:?}", err.current_context());
};
assert_that!(request_error.status())
.is_equal_to(Some(reqwest::StatusCode::INTERNAL_SERVER_ERROR));
}
}