Add hermetic coverage for service boundaries
This commit is contained in:
parent
19ac156f4b
commit
de4f990492
1 changed files with 137 additions and 0 deletions
|
|
@ -250,3 +250,69 @@ fn generated_id(prefix: char) -> OpaqueId {
|
|||
}
|
||||
OpaqueId::parse(value).expect("generated identifier is valid")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn response(status: StatusCode) -> Response {
|
||||
http::Response::builder()
|
||||
.status(status)
|
||||
.body(String::new())
|
||||
.unwrap()
|
||||
.into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_statuses() {
|
||||
assert!(classify(response(StatusCode::NO_CONTENT)).is_ok());
|
||||
assert!(matches!(
|
||||
classify(response(StatusCode::BAD_REQUEST)),
|
||||
Err(WorkerError::InvalidRequest)
|
||||
));
|
||||
for status in [StatusCode::UNAUTHORIZED, StatusCode::FORBIDDEN] {
|
||||
assert!(matches!(
|
||||
classify(response(status)),
|
||||
Err(WorkerError::Unauthenticated)
|
||||
));
|
||||
}
|
||||
assert!(matches!(
|
||||
classify(response(StatusCode::CONFLICT)),
|
||||
Err(WorkerError::PreconditionFailed)
|
||||
));
|
||||
for status in [
|
||||
StatusCode::NOT_FOUND,
|
||||
StatusCode::TOO_MANY_REQUESTS,
|
||||
StatusCode::SERVICE_UNAVAILABLE,
|
||||
] {
|
||||
assert!(matches!(
|
||||
classify(response(status)),
|
||||
Err(WorkerError::ApiUnavailable)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validates_base_url() {
|
||||
let client = Client::new();
|
||||
let valid = WorkerApiClient::new("https://api.example.test/", client.clone()).unwrap();
|
||||
assert_eq!(valid.base_url, "https://api.example.test");
|
||||
for url in ["http://api.example.test", "https://", "not-a-url"] {
|
||||
assert!(matches!(
|
||||
WorkerApiClient::new(url, client.clone()),
|
||||
Err(WorkerError::InvalidConfiguration)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ids_are_unique() {
|
||||
let operation = operation_id();
|
||||
let second = operation_id();
|
||||
let incarnation = incarnation_id();
|
||||
assert!(operation.as_str().starts_with('o'));
|
||||
assert!(incarnation.as_str().starts_with('w'));
|
||||
assert_ne!(operation, second);
|
||||
assert_ne!(operation, incarnation);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue