From ae6ebd9afc6b927bd4d50d94ce6e0fedcef38644 Mon Sep 17 00:00:00 2001 From: John Howard Date: Thu, 10 Jul 2025 10:29:16 -0700 Subject: [PATCH] feature: capture HTTP version in Request This allows responding with info about the HTTP version. Additionally, expanded a test to cover this. Note: in the test, we assert resp.version(). However, for my use case I have `test client --> reverse proxy under test --> wiremock`, so I do not have direct access to the response from wiremock. Hence, I want to put more info about the request into the response body/headers. --- src/request.rs | 4 +++- tests/tokio.rs | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/request.rs b/src/request.rs index 14d5aa7..55da356 100644 --- a/src/request.rs +++ b/src/request.rs @@ -1,6 +1,6 @@ use std::fmt; -use http::{HeaderMap, Method}; +use http::{HeaderMap, Method, Version}; use http_body_util::BodyExt; use serde::de::DeserializeOwned; use url::Url; @@ -41,6 +41,7 @@ pub struct Request { pub method: Method, pub headers: HeaderMap, pub body: Vec, + pub version: Version, } impl Request { @@ -67,6 +68,7 @@ impl Request { url, method: parts.method, headers: parts.headers, + version: parts.version, body: body.to_vec(), } } diff --git a/tests/tokio.rs b/tests/tokio.rs index f92de5b..52a560c 100644 --- a/tests/tokio.rs +++ b/tests/tokio.rs @@ -1,6 +1,6 @@ use reqwest::Client; use wiremock::matchers::{method, path}; -use wiremock::{Mock, MockServer, ResponseTemplate}; +use wiremock::{Mock, MockServer, Request, ResponseTemplate}; // regression tests for https://github.com/LukeMathWalker/wiremock-rs/issues/7 // running both tests will _sometimes_ trigger a hang if the runtimes aren't separated correctly @@ -41,7 +41,9 @@ async fn hello_reqwest_http2() { Mock::given(method("GET")) .and(path("/")) - .respond_with(ResponseTemplate::new(200)) + .respond_with(|req: &Request| { + ResponseTemplate::new(200).insert_header("x-version", format!("{:?}", req.version)) + }) .mount(&mock_server) .await; @@ -56,4 +58,5 @@ async fn hello_reqwest_http2() { assert_eq!(resp.status(), 200); assert_eq!(resp.version(), reqwest::Version::HTTP_2); + assert_eq!(resp.headers().get("x-version").unwrap().to_str().unwrap(), "HTTP/2.0"); }