proptest_http_message/request_line/
version.rs

1//! HTTP request line HTTP version strategies.
2
3use std::fmt;
4
5use proptest::{
6  prelude::{Just, Strategy},
7  prop_oneof,
8};
9
10const HTTP_1_0: &str = "HTTP/1.0";
11const HTTP_1_1: &str = "HTTP/1.1";
12const HTTP_2: &str = "HTTP/2";
13const HTTP_3: &str = "HTTP/3";
14
15/// All valid HTTP versions.
16#[derive(Debug, Clone)]
17pub enum HttpVersion {
18  Http10,
19  Http11,
20  Http2,
21  Http3,
22}
23
24impl fmt::Display for HttpVersion {
25  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26    match self {
27      HttpVersion::Http10 => write!(f, "{HTTP_1_0}"),
28      HttpVersion::Http11 => write!(f, "{HTTP_1_1}"),
29      HttpVersion::Http2 => write!(f, "{HTTP_2}"),
30      HttpVersion::Http3 => write!(f, "{HTTP_3}"),
31    }
32  }
33}
34
35/// strategy for generating HTTP version.
36///
37/// # Returns
38/// [`HttpVersion`] and it representation.
39pub fn version() -> impl Strategy<Value = (HttpVersion, String)> {
40  prop_oneof![
41    Just((HttpVersion::Http10, HttpVersion::Http10.to_string())),
42    Just((HttpVersion::Http11, HttpVersion::Http11.to_string())),
43    Just((HttpVersion::Http2, HttpVersion::Http2.to_string())),
44    Just((HttpVersion::Http3, HttpVersion::Http3.to_string())),
45  ]
46}
47
48#[cfg(test)]
49pub(super) mod tests {
50  use proptest::proptest;
51
52  use super::*;
53
54  pub(in super::super) fn version_asserts(version: &HttpVersion, repr: &str) {
55    match version {
56      HttpVersion::Http10 => assert_eq!(repr, HTTP_1_0, "expected HTTP version 1.0 but got {repr}"),
57      HttpVersion::Http11 => assert_eq!(repr, HTTP_1_1, "expected HTTP version 1.1 but got {repr}"),
58      HttpVersion::Http2 => assert_eq!(repr, HTTP_2, "expected HTTP version 2 but got {repr}"),
59      HttpVersion::Http3 => assert_eq!(repr, HTTP_3, "expected HTTP version 3 but got {repr}"),
60    }
61  }
62
63  proptest! {
64    #[test]
65    fn version_works((version, repr) in version()) {
66      version_asserts(&version, &repr);
67    }
68  }
69}