From 23abb7706e24e60fa689f7efb428cdb9d47a2bb0 Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 19 Oct 2024 22:02:05 +0200 Subject: [PATCH] feat: allow expecting status codes --- postwoman.toml | 4 ++++ src/errors.rs | 3 +++ src/model/endpoint.rs | 9 +++++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/postwoman.toml b/postwoman.toml index 17bb9b2..4e857ea 100644 --- a/postwoman.toml +++ b/postwoman.toml @@ -11,6 +11,10 @@ url = "https://api.alemi.dev/" url = "https://api.alemi.dev/look/into/the/void" extract = { type = "discard" } +[route.notfound] +url = "https://api.alemi.dev/not-found" +expect = 404 + [route.debug] url = "https://api.alemi.dev/debug" method = "PUT" diff --git a/src/errors.rs b/src/errors.rs index 925a463..62a5403 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -37,6 +37,9 @@ pub enum PostWomanError { #[error("invalid regex: {0:?}")] InvalidRegex(#[from] regex::Error), + #[error("request didn't match expected status code: {0:?}")] + UnexpectedStatusCode(reqwest::Response), + #[error("invalid Json Query: {0}")] JQError(String), diff --git a/src/model/endpoint.rs b/src/model/endpoint.rs index df99528..94b587e 100644 --- a/src/model/endpoint.rs +++ b/src/model/endpoint.rs @@ -21,6 +21,8 @@ pub struct Endpoint { pub headers: Option>, /// body, optional string pub body: Option>, + /// expected error code, will fail if different + pub expect: Option, /// response extractor pub extract: Option>, } @@ -135,8 +137,11 @@ impl Endpoint { .headers(headers) .body(body) .send() - .await? - .error_for_status()?; + .await?; + + if res.status().as_u16() != self.expect.unwrap_or(200) { + return Err(PostWomanError::UnexpectedStatusCode(res)); + } Ok(match self.extract.unwrap_or_default() { StringOr::T(Extractor::Discard) => "".to_string(),