chrome_for_testing/api/
known_good_versions.rs1use crate::api::version::Version;
2use crate::api::{Download, API_BASE_URL};
3use crate::error::Result;
4use serde::Deserialize;
5
6const KNOWN_GOOD_VERSIONS_WITH_DOWNLOADS_JSON_PATH: &str =
30 "/chrome-for-testing/known-good-versions-with-downloads.json";
31
32#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
34pub struct Downloads {
35 pub chrome: Vec<Download>,
37
38 pub chromedriver: Option<Vec<Download>>,
41}
42
43#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
47pub struct VersionWithoutChannel {
48 pub version: Version,
50
51 pub revision: String,
53
54 pub downloads: Downloads,
56}
57
58#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
64pub struct KnownGoodVersions {
65 #[serde(with = "time::serde::rfc3339")]
67 pub timestamp: time::OffsetDateTime,
68
69 pub versions: Vec<VersionWithoutChannel>,
71}
72
73impl KnownGoodVersions {
74 pub async fn fetch(client: reqwest::Client) -> Result<Self> {
80 Self::fetch_with_base_url(client, API_BASE_URL.clone()).await
81 }
82
83 pub async fn fetch_with_base_url(
84 client: reqwest::Client,
85 base_url: reqwest::Url,
86 ) -> Result<Self> {
87 let result = client
88 .get(base_url.join(KNOWN_GOOD_VERSIONS_WITH_DOWNLOADS_JSON_PATH)?)
89 .send()
90 .await?
91 .json::<Self>()
92 .await?;
93 Ok(result)
94 }
95}
96
97#[cfg(test)]
98mod tests {
99 use super::*;
100 use crate::api::known_good_versions::KnownGoodVersions;
101 use crate::api::platform::Platform;
102 use crate::api::version::Version;
103 use crate::api::Download;
104 use assertr::prelude::*;
105 use time::macros::datetime;
106 use url::Url;
107
108 #[tokio::test]
109 async fn can_request_from_real_world_endpoint() {
110 let result = KnownGoodVersions::fetch(reqwest::Client::new()).await;
111 assert_that(result).is_ok();
112 }
113
114 #[tokio::test]
116 async fn can_query_known_good_versions_api_endpoint_and_deserialize_response() {
117 let mut server = mockito::Server::new_async().await;
118
119 let _mock = server
120 .mock("GET", KNOWN_GOOD_VERSIONS_WITH_DOWNLOADS_JSON_PATH)
121 .with_status(200)
122 .with_header("content-type", "application/json")
123 .with_body(include_str!(
124 "./../../test-data/known_good_versions_test_response.json"
125 ))
126 .create();
127
128 let url: Url = server.url().parse().unwrap();
129
130 let data = KnownGoodVersions::fetch_with_base_url(reqwest::Client::new(), url)
131 .await
132 .unwrap();
133
134 assert_that(data).is_equal_to(KnownGoodVersions {
135 timestamp: datetime!(2025-01-17 10:09:31.689 UTC),
136 versions: vec![
137 VersionWithoutChannel {
138 version: Version { major: 113, minor: 0, patch: 5672, build: 0 },
139 revision: String::from("1121455"),
140 downloads: Downloads {
141 chrome: vec![
142 Download { platform: Platform::Linux64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/113.0.5672.0/linux64/chrome-linux64.zip") },
143 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") },
144 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") },
145 Download { platform: Platform::Win32, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/113.0.5672.0/win32/chrome-win32.zip") },
146 Download { platform: Platform::Win64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/113.0.5672.0/win64/chrome-win64.zip") },
147 ],
148 chromedriver: None,
149 },
150 },
151 VersionWithoutChannel {
152 version: Version { major: 134, minor: 0, patch: 6962, build: 0 },
153 revision: String::from("1407692"),
154 downloads: Downloads {
155 chrome: vec![
156 Download { platform: Platform::Linux64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/134.0.6962.0/linux64/chrome-linux64.zip") },
157 Download { platform: Platform::MacArm64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/134.0.6962.0/mac-arm64/chrome-mac-arm64.zip") },
158 Download { platform: Platform::MacX64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/134.0.6962.0/mac-x64/chrome-mac-x64.zip") },
159 Download { platform: Platform::Win32, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/134.0.6962.0/win32/chrome-win32.zip") },
160 Download { platform: Platform::Win64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/134.0.6962.0/win64/chrome-win64.zip") },
161 ],
162 chromedriver: Some(vec![
163 Download { platform: Platform::Linux64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/134.0.6962.0/linux64/chromedriver-linux64.zip") },
164 Download { platform: Platform::MacArm64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/134.0.6962.0/mac-arm64/chromedriver-mac-arm64.zip") },
165 Download { platform: Platform::MacX64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/134.0.6962.0/mac-x64/chromedriver-mac-x64.zip") },
166 Download { platform: Platform::Win32, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/134.0.6962.0/win32/chromedriver-win32.zip") },
167 Download { platform: Platform::Win64, url: String::from("https://storage.googleapis.com/chrome-for-testing-public/134.0.6962.0/win64/chromedriver-win64.zip") },
168 ]),
169 },
170 },
171 ],
172 });
173 }
174}