Skip to content

Commit b3fc86f

Browse files
committed
upgrade mockito
1 parent 84edb41 commit b3fc86f

File tree

6 files changed

+140
-48
lines changed

6 files changed

+140
-48
lines changed

Cargo.lock

Lines changed: 32 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ procfs = "0.15.1"
126126
criterion = "0.5.1"
127127
kuchiki = "0.8"
128128
rand = "0.8"
129-
mockito = "0.31.0"
129+
mockito = "1.0.2"
130130
test-case = "3.0.0"
131131
aws-smithy-client = { version = "0.55.1", features = ["test-util"]}
132132
aws-smithy-http = "0.55.1"

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub struct Config {
88
pub prefix: PathBuf,
99
pub registry_index_path: PathBuf,
1010
pub registry_url: Option<String>,
11+
pub registry_api_host: String,
1112

1213
// Database connection params
1314
pub(crate) database_url: String,
@@ -135,6 +136,7 @@ impl Config {
135136

136137
registry_index_path: env("REGISTRY_INDEX_PATH", prefix.join("crates.io-index"))?,
137138
registry_url: maybe_env("REGISTRY_URL")?,
139+
registry_api_host: env("DOCSRS_REGISTRY_API_HOST", "https://crates.io".into())?,
138140
prefix: prefix.clone(),
139141

140142
database_url: require_env("DOCSRS_DATABASE_URL")?,

src/repositories/github.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const GRAPHQL_SINGLE: &str = "query($owner: String!, $repo: String!) {
4343
}";
4444

4545
pub struct GitHub {
46+
endpoint: String,
4647
client: HttpClient,
4748
github_updater_min_rate_limit: u32,
4849
}
@@ -51,6 +52,13 @@ impl GitHub {
5152
/// Returns `Err` if the access token has invalid syntax (but *not* if it isn't authorized).
5253
/// Returns `Ok(None)` if there is no access token.
5354
pub fn new(config: &Config) -> Result<Option<Self>> {
55+
Self::with_custom_endpoint(config, "https://api.github.com/graphql")
56+
}
57+
58+
pub fn with_custom_endpoint<E: AsRef<str>>(
59+
config: &Config,
60+
endpoint: E,
61+
) -> Result<Option<Self>> {
5462
let mut headers = HeaderMap::new();
5563
headers.insert(USER_AGENT, HeaderValue::from_static(APP_USER_AGENT));
5664
headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
@@ -69,6 +77,7 @@ impl GitHub {
6977

7078
Ok(Some(GitHub {
7179
client,
80+
endpoint: endpoint.as_ref().to_owned(),
7281
github_updater_min_rate_limit: config.github_updater_min_rate_limit,
7382
}))
7483
}
@@ -176,16 +185,9 @@ impl GitHub {
176185
query: &str,
177186
variables: impl serde::Serialize,
178187
) -> Result<GraphResponse<T>> {
179-
#[cfg(not(test))]
180-
let host = "https://api.github.com/graphql";
181-
#[cfg(test)]
182-
let host = format!("{}/graphql", mockito::server_url());
183-
#[cfg(test)]
184-
let host = &host;
185-
186188
Ok(self
187189
.client
188-
.post(host)
190+
.post(&self.endpoint)
189191
.json(&serde_json::json!({
190192
"query": query,
191193
"variables": variables,
@@ -256,19 +258,28 @@ struct GraphIssues {
256258

257259
#[cfg(test)]
258260
mod tests {
259-
use super::GitHub;
261+
use super::{Config, GitHub};
260262
use crate::repositories::updater::{repository_name, RepositoryForge};
261263
use crate::repositories::RateLimitReached;
262-
use mockito::mock;
264+
265+
fn mock_server_and_github(config: &Config) -> (mockito::ServerGuard, GitHub) {
266+
let server = mockito::Server::new();
267+
let updater = GitHub::with_custom_endpoint(config, format!("{}/graphql", server.url()))
268+
.expect("GitHub::new failed")
269+
.unwrap();
270+
271+
(server, updater)
272+
}
263273

264274
#[test]
265275
fn test_rate_limit_fail() {
266276
crate::test::wrapper(|env| {
267277
let mut config = env.base_config();
268278
config.github_accesstoken = Some("qsjdnfqdq".to_owned());
269-
let updater = GitHub::new(&config).expect("GitHub::new failed").unwrap();
279+
let (mut server, updater) = mock_server_and_github(&config);
270280

271-
let _m1 = mock("POST", "/graphql")
281+
let _m1 = server
282+
.mock("POST", "/graphql")
272283
.with_header("content-type", "application/json")
273284
.with_body(
274285
r#"{"errors":[{"type":"RATE_LIMITED","message":"API rate limit exceeded"}]}"#,
@@ -288,9 +299,10 @@ mod tests {
288299
crate::test::wrapper(|env| {
289300
let mut config = env.base_config();
290301
config.github_accesstoken = Some("qsjdnfqdq".to_owned());
291-
let updater = GitHub::new(&config).expect("GitHub::new failed").unwrap();
302+
let (mut server, updater) = mock_server_and_github(&config);
292303

293-
let _m1 = mock("POST", "/graphql")
304+
let _m1 = server
305+
.mock("POST", "/graphql")
294306
.with_header("content-type", "application/json")
295307
.with_body(r#"{"data": {"nodes": [], "rateLimit": {"remaining": 0}}}"#)
296308
.create();
@@ -308,9 +320,10 @@ mod tests {
308320
crate::test::wrapper(|env| {
309321
let mut config = env.base_config();
310322
config.github_accesstoken = Some("qsjdnfqdq".to_owned());
311-
let updater = GitHub::new(&config).expect("GitHub::new failed").unwrap();
323+
let (mut server, updater) = mock_server_and_github(&config);
312324

313-
let _m1 = mock("POST", "/graphql")
325+
let _m1 = server
326+
.mock("POST", "/graphql")
314327
.with_header("content-type", "application/json")
315328
.with_body(
316329
r#"{"data": {"nodes": [], "rateLimit": {"remaining": 100000}}, "errors":
@@ -334,9 +347,10 @@ mod tests {
334347
crate::test::wrapper(|env| {
335348
let mut config = env.base_config();
336349
config.github_accesstoken = Some("qsjdnfqdq".to_owned());
337-
let updater = GitHub::new(&config).expect("GitHub::new failed").unwrap();
350+
let (mut server, updater) = mock_server_and_github(&config);
338351

339-
let _m1 = mock("POST", "/graphql")
352+
let _m1 = server
353+
.mock("POST", "/graphql")
340354
.with_header("content-type", "application/json")
341355
.with_body(
342356
r#"{"data": {"repository": {"id": "hello", "nameWithOwner": "foo/bar",

src/repositories/gitlab.rs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,19 @@ const GRAPHQL_SINGLE: &str = "query($fullPath: ID!) {
4343
pub struct GitLab {
4444
client: HttpClient,
4545
host: &'static str,
46+
endpoint: String,
4647
}
4748

4849
impl GitLab {
4950
pub fn new(host: &'static str, access_token: &Option<String>) -> Result<Self> {
51+
Self::with_custom_endpoint(host, access_token, format!("https://{}/api/graphql", host))
52+
}
53+
54+
pub fn with_custom_endpoint<E: AsRef<str>>(
55+
host: &'static str,
56+
access_token: &Option<String>,
57+
endpoint: E,
58+
) -> Result<Self> {
5059
let mut headers = HeaderMap::new();
5160
headers.insert(USER_AGENT, HeaderValue::from_static(APP_USER_AGENT));
5261
headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
@@ -64,7 +73,11 @@ impl GitLab {
6473
}
6574

6675
let client = HttpClient::builder().default_headers(headers).build()?;
67-
Ok(GitLab { client, host })
76+
Ok(GitLab {
77+
client,
78+
host,
79+
endpoint: endpoint.as_ref().to_string(),
80+
})
6881
}
6982
}
7083

@@ -165,14 +178,9 @@ impl GitLab {
165178
query: &str,
166179
variables: impl serde::Serialize,
167180
) -> Result<(GraphResponse<T>, Option<usize>)> {
168-
#[cfg(not(test))]
169-
let host = format!("https://{}/api/graphql", self.host);
170-
#[cfg(test)]
171-
let host = format!("{}/api/graphql", mockito::server_url());
172-
173181
let res = self
174182
.client
175-
.post(host)
183+
.post(&self.endpoint)
176184
.json(&serde_json::json!({
177185
"query": query,
178186
"variables": variables,
@@ -252,13 +260,25 @@ mod tests {
252260
use super::GitLab;
253261
use crate::repositories::updater::{repository_name, RepositoryForge};
254262
use crate::repositories::RateLimitReached;
255-
use mockito::mock;
263+
264+
fn mock_server_and_gitlab() -> (mockito::ServerGuard, GitLab) {
265+
let server = mockito::Server::new();
266+
let updater = GitLab::with_custom_endpoint(
267+
"gitlab.com",
268+
&None,
269+
format!("{}/api/graphql", server.url()),
270+
)
271+
.expect("GitLab::new failed");
272+
273+
(server, updater)
274+
}
256275

257276
#[test]
258277
fn test_rate_limit() {
259-
let updater = GitLab::new("gitlab.com", &None).expect("GitLab::new failed");
278+
let (mut server, updater) = mock_server_and_gitlab();
260279

261-
let _m1 = mock("POST", "/api/graphql")
280+
let _m1 = server
281+
.mock("POST", "/api/graphql")
262282
.with_header("content-type", "application/json")
263283
.with_header("RateLimit-Remaining", "0")
264284
.with_body("{}")
@@ -278,9 +298,10 @@ mod tests {
278298

279299
#[test]
280300
fn not_found() {
281-
let updater = GitLab::new("gitlab.com", &None).expect("GitLab::new failed");
301+
let (mut server, updater) = mock_server_and_gitlab();
282302

283-
let _m1 = mock("POST", "/api/graphql")
303+
let _m1 = server
304+
.mock("POST", "/api/graphql")
284305
.with_header("content-type", "application/json")
285306
.with_body(r#"{"data": {"projects": {"nodes": []}}}"#)
286307
.create();
@@ -296,9 +317,10 @@ mod tests {
296317

297318
#[test]
298319
fn get_repository_info() {
299-
let updater = GitLab::new("gitlab.com", &None).expect("GitLab::new failed");
320+
let (mut server, updater) = mock_server_and_gitlab();
300321

301-
let _m1 = mock("POST", "/api/graphql")
322+
let _m1 = server
323+
.mock("POST", "/api/graphql")
302324
.with_header("content-type", "application/json")
303325
.with_body(
304326
r#"{"data": {"project": {"id": "hello", "fullPath": "foo/bar",

0 commit comments

Comments
 (0)