-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This should abstract away from the actual implementation and allow for reproducible, local tests with a DummyApi. Tests have been rewritten to use the DummyApi. Prep for #11.
- Loading branch information
Showing
10 changed files
with
220 additions
and
573 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//! This contains the [`DummyApi`] used for testing purposes. | ||
use reqwest::StatusCode; | ||
|
||
use crate::error::Result; | ||
|
||
use super::{Api, Headers, Response}; | ||
|
||
/// A dummy API, serving local, deterministic Responses | ||
#[derive(Debug)] | ||
pub struct DummyApi; | ||
|
||
impl Api for DummyApi { | ||
fn create() -> Result<Self> { | ||
Ok(DummyApi) | ||
} | ||
|
||
fn get<'url, S>(&self, url: &'url str, etag: Option<S>) -> Result<Response<'url>> | ||
where | ||
S: AsRef<str>, | ||
{ | ||
if url == "http://invalid.local/test" { | ||
get_test_page(etag) | ||
} else { | ||
panic!("BUG: Invalid url in dummy api: {:?}", url) | ||
} | ||
} | ||
} | ||
|
||
/// GET http://invalid.local/test | ||
fn get_test_page<S: AsRef<str>>(etag: Option<S>) -> Result<Response<'static>> { | ||
let etag = etag.map(|etag| etag.as_ref().to_owned()); | ||
Ok(Response { | ||
url: "http://invalid.local/test", | ||
status: if etag == Some("static".into()) { | ||
StatusCode::NOT_MODIFIED | ||
} else { | ||
StatusCode::OK | ||
}, | ||
headers: Headers { | ||
etag: Some("static".into()), | ||
this_page: Some(1), | ||
next_page: None, | ||
last_page: Some(1), | ||
}, | ||
body: "It works".to_owned(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use ::reqwest::StatusCode; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::error::Result; | ||
|
||
#[cfg(not(test))] | ||
mod reqwest; | ||
#[cfg(not(test))] | ||
pub use self::reqwest::ReqwestApi as DefaultApi; | ||
|
||
#[cfg(test)] | ||
mod dummy; | ||
#[cfg(test)] | ||
pub use self::dummy::DummyApi as DefaultApi; | ||
|
||
/// Assortment of headers relevant to the program. | ||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
#[non_exhaustive] | ||
pub struct Headers { | ||
pub etag: Option<String>, | ||
pub this_page: Option<usize>, | ||
pub next_page: Option<String>, | ||
pub last_page: Option<usize>, | ||
} | ||
|
||
/// A subset of a Response, derived from [`reqwest::Response`]. | ||
pub struct Response<'url> { | ||
pub url: &'url str, | ||
pub status: StatusCode, | ||
pub headers: Headers, | ||
pub body: String, | ||
} | ||
|
||
/// Generalized API endpoint. | ||
/// | ||
/// This abstracts away from the real thing to allow for deterministic local | ||
/// tests with a DummyApi. | ||
pub trait Api | ||
where | ||
Self: Sized, | ||
{ | ||
/// Create the Api. | ||
fn create() -> Result<Self>; | ||
|
||
/// Send a get request. | ||
/// | ||
/// Optionally attach an `If-None-Match` header, if `etag` is `Some`. | ||
fn get<'url, S>(&self, url: &'url str, etag: Option<S>) -> Result<Response<'url>> | ||
where | ||
S: AsRef<str>; | ||
} |
Oops, something went wrong.