From c9d5001632894d2508d7a250bf6227a06388e3cc Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Wed, 9 Sep 2026 11:27:53 -0400 Subject: [PATCH] test: add integration tests checking pattern matching is allowed --- tests/patterns.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 tests/patterns.rs diff --git a/tests/patterns.rs b/tests/patterns.rs new file mode 100644 index 00000000..18a0f9a7 --- /dev/null +++ b/tests/patterns.rs @@ -0,0 +1,80 @@ +//! Constant patterns are part of these types' public API. Equality assertions +//! alone do not catch a representation change that breaks pattern matching. + +use http::header::{HeaderName, ACCEPT, CONTENT_TYPE, HOST}; +use http::{Method, StatusCode, Version}; + +#[test] +fn method_constants() { + for name in &["GET", "POST", "CUSTOM"] { + let method = Method::from_bytes(name.as_bytes()).unwrap(); + let matched = match method { + Method::GET => "GET", + Method::POST => "POST", + _ => "CUSTOM", + }; + assert_eq!(matched, *name); + } + + assert!(matches!(&Method::GET, &Method::GET)); +} + +#[test] +fn status_code_constants() { + for code in &[100, 200, 301, 404, 500, 599] { + let status = StatusCode::from_u16(*code).unwrap(); + let matched = match status { + StatusCode::CONTINUE => 100, + StatusCode::OK => 200, + StatusCode::MOVED_PERMANENTLY => 301, + StatusCode::NOT_FOUND => 404, + StatusCode::INTERNAL_SERVER_ERROR => 500, + _ => 599, + }; + assert_eq!(matched, *code); + } + + assert!(matches!(&StatusCode::OK, &StatusCode::OK)); +} + +#[test] +fn version_constants() { + for (version, expected) in &[ + (Version::HTTP_09, "HTTP/0.9"), + (Version::HTTP_10, "HTTP/1.0"), + (Version::HTTP_11, "HTTP/1.1"), + (Version::HTTP_2, "HTTP/2.0"), + (Version::HTTP_3, "HTTP/3.0"), + ] { + let matched = match *version { + Version::HTTP_09 => "HTTP/0.9", + Version::HTTP_10 => "HTTP/1.0", + Version::HTTP_11 => "HTTP/1.1", + Version::HTTP_2 => "HTTP/2.0", + Version::HTTP_3 => "HTTP/3.0", + _ => panic!("unrecognized version"), + }; + assert_eq!(matched, *expected); + } + + assert!(matches!(&Version::HTTP_11, &Version::HTTP_11)); +} + +#[test] +fn header_name_constants() { + for name in &["accept", "content-type", "host", "x-custom"] { + // Standard names parsed with either casing must match the constants. + for input in &[name.to_string(), name.to_uppercase()] { + let header = HeaderName::from_bytes(input.as_bytes()).unwrap(); + let matched = match header { + ACCEPT => "accept", + CONTENT_TYPE => "content-type", + HOST => "host", + _ => "x-custom", + }; + assert_eq!(matched, *name); + } + } + + assert!(matches!(&CONTENT_TYPE, &CONTENT_TYPE)); +}