Skip to content

feat(error): add predicates for too-large and status parse errors #2538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ impl Error {
matches!(self.inner.kind, Kind::Parse(_))
}

/// Returns true if this was an HTTP parse error caused by a message that was too large.
pub fn is_parse_too_large(&self) -> bool {
matches!(self.inner.kind, Kind::Parse(Parse::TooLarge))
}

/// Returns true if this was an HTTP parse error caused by an invalid response status code or
/// reason phrase.
pub fn is_parse_status(&self) -> bool {
matches!(self.inner.kind, Kind::Parse(Parse::Status))
}

/// Returns true if this error was caused by user code.
pub fn is_user(&self) -> bool {
matches!(self.inner.kind, Kind::User(_))
Expand Down
77 changes: 77 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,83 @@ test! {

}

test! {
name: client_error_parse_too_large,

server:
expected: "\
GET /err HTTP/1.1\r\n\
host: {addr}\r\n\
\r\n\
",
reply: {
let long_header = std::iter::repeat("A").take(500_000).collect::<String>();
format!("\
HTTP/1.1 200 OK\r\n\
{}: {}\r\n\
\r\n\
",
long_header,
long_header,
)
},

client:
request: {
method: GET,
url: "http://{addr}/err",
},
// should get a Parse(TooLarge) error
error: |err| err.is_parse() && err.is_parse_too_large(),

}

test! {
name: client_error_parse_status_out_of_range,

server:
expected: "\
GET /err HTTP/1.1\r\n\
host: {addr}\r\n\
\r\n\
",
reply: "\
HTTP/1.1 001 OK\r\n\
\r\n\
",

client:
request: {
method: GET,
url: "http://{addr}/err",
},
// should get a Parse(Status) error
error: |err| err.is_parse() && err.is_parse_status(),
}

test! {
name: client_error_parse_status_syntactically_invalid,

server:
expected: "\
GET /err HTTP/1.1\r\n\
host: {addr}\r\n\
\r\n\
",
reply: "\
HTTP/1.1 1 OK\r\n\
\r\n\
",

client:
request: {
method: GET,
url: "http://{addr}/err",
},
// should get a Parse(Status) error
error: |err| err.is_parse() && err.is_parse_status(),
}

test! {
name: client_100_continue,

Expand Down