Change HTTP version enum from response/Version to core/HttpVersion.

The new enum implements now Copy, and has a HTTP3 variant.
This commit is contained in:
jcamiel 2023-10-01 19:48:16 +02:00 committed by hurl-bot
parent f4243b6b1f
commit fef53861ae
No known key found for this signature in database
GPG Key ID: 1283A2B4A0DCAF8D
6 changed files with 43 additions and 36 deletions

View File

@ -560,13 +560,15 @@ impl Client {
}
/// Parses HTTP response version.
fn parse_response_version(&mut self, line: &str) -> Result<Version, HttpError> {
fn parse_response_version(&mut self, line: &str) -> Result<HttpVersion, HttpError> {
if line.starts_with("HTTP/1.0") {
Ok(Version::Http10)
Ok(HttpVersion::Http10)
} else if line.starts_with("HTTP/1.1") {
Ok(Version::Http11)
Ok(HttpVersion::Http11)
} else if line.starts_with("HTTP/2") {
Ok(Version::Http2)
Ok(HttpVersion::Http2)
} else if line.starts_with("HTTP/3") {
Ok(HttpVersion::Http3)
} else {
Err(HttpError::CouldNotParseResponse)
}

View File

@ -42,6 +42,28 @@ pub struct Param {
pub value: String,
}
/// Represents the HTTP version of a HTTP transaction.
/// See <https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Evolution_of_HTTP>
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum HttpVersion {
Http10,
Http11,
Http2,
Http3,
}
impl fmt::Display for HttpVersion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let value = match self {
HttpVersion::Http10 => "HTTP/1.0",
HttpVersion::Http11 => "HTTP/1.1",
HttpVersion::Http2 => "HTTP/2",
HttpVersion::Http3 => "HTTP/3",
};
write!(f, "{value}")
}
}
impl fmt::Display for Cookie {
/// Formats this cookie using Netscape cookie format.
///

View File

@ -19,13 +19,14 @@ pub use self::call::Call;
pub use self::certificate::Certificate;
pub(crate) use self::client::Client;
pub use self::cookie::{CookieAttribute, ResponseCookie};
pub use self::core::HttpVersion;
pub(crate) use self::core::{Cookie, Param, RequestCookie};
pub(crate) use self::error::HttpError;
pub use self::header::Header;
pub(crate) use self::options::{ClientOptions, Verbosity};
pub use self::request::Request;
pub(crate) use self::request_spec::{Body, FileParam, Method, MultipartParam, RequestSpec};
pub use self::response::{Response, Version};
pub use self::response::Response;
#[cfg(test)]
pub use self::tests::*;
pub use self::timings::Timings;

View File

@ -15,16 +15,15 @@
* limitations under the License.
*
*/
use core::fmt;
use std::time::Duration;
use crate::http::certificate::Certificate;
use crate::http::{header, Header};
use crate::http::{header, Header, HttpVersion};
/// Represents an HTTP response.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Response {
pub version: Version,
pub version: HttpVersion,
pub status: u32,
pub headers: Vec<Header>,
pub body: Vec<u8>,
@ -37,7 +36,7 @@ pub struct Response {
impl Default for Response {
fn default() -> Self {
Response {
version: Version::Http10,
version: HttpVersion::Http10,
status: 200,
headers: vec![],
body: vec![],
@ -48,24 +47,6 @@ impl Default for Response {
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Version {
Http10,
Http11,
Http2,
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let value = match self {
Version::Http10 => "HTTP/1.0",
Version::Http11 => "HTTP/1.1",
Version::Http2 => "HTTP/2",
};
write!(f, "{value}")
}
}
impl Response {
/// Returns all header values.
pub fn get_header_values(&self, name: &str) -> Vec<String> {

View File

@ -19,8 +19,8 @@ use chrono::{DateTime, Utc};
use serde_json::Number;
use crate::http::{
Call, Certificate, Cookie, Header, Param, Request, RequestCookie, Response, ResponseCookie,
Timings, Version,
Call, Certificate, Cookie, Header, HttpVersion, Param, Request, RequestCookie, Response,
ResponseCookie, Timings,
};
use crate::runner::{AssertResult, CaptureResult, EntryResult, HurlResult};
use crate::util::logger;
@ -152,12 +152,13 @@ impl Header {
}
}
impl Version {
fn to_json(&self) -> serde_json::Value {
impl HttpVersion {
fn to_json(self) -> serde_json::Value {
let value = match self {
Version::Http10 => "HTTP/1.0",
Version::Http11 => "HTTP/1.1",
Version::Http2 => "HTTP/2",
HttpVersion::Http10 => "HTTP/1.0",
HttpVersion::Http11 => "HTTP/1.1",
HttpVersion::Http2 => "HTTP/2",
HttpVersion::Http3 => "HTTP/3",
};
serde_json::Value::String(value.to_string())
}

View File

@ -15,7 +15,7 @@
* limitations under the License.
*
*/
use hurl::http::{Call, Request, Response, Version};
use hurl::http::{Call, HttpVersion, Request, Response};
use hurl::runner;
use hurl::runner::{EntryResult, HurlResult, RunnerOptionsBuilder};
use hurl::util::logger::LoggerOptionsBuilder;
@ -63,7 +63,7 @@ fn simple_sample() {
}
fn check_response(response: &Response) {
assert_eq!(response.version, Version::Http11);
assert_eq!(response.version, HttpVersion::Http11);
assert_eq!(response.status, 200);
assert_eq!(response.headers.len(), 6);
let header_names = response