Skip to content

Commit

Permalink
http_interop: Test that non-UTF-8 sequences in header values are (wro…
Browse files Browse the repository at this point in the history
…ngly) skipped during conversion
  • Loading branch information
MarijnS95 authored and algesten committed Oct 23, 2023
1 parent 3343981 commit 6eeb933
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/http_interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ impl From<Request> for http::request::Builder {

#[cfg(test)]
mod tests {
use crate::header::{add_header, HeaderLine};

#[test]
fn convert_http_response() {
Expand Down Expand Up @@ -393,6 +394,34 @@ mod tests {
assert_eq!(buf, vec![0xde, 0xad, 0xbe, 0xef]);
}

#[test]
fn convert_http_response_builder_invalid_utf8_header_is_skipped() {
use http::Response;

let http_response = Response::builder()
.header("Some-Key", b"some\xff\xffvalue".as_slice())
.body(b"hello")
.unwrap();
let response: super::Response = http_response.into();

assert_eq!(response.header("some-key"), None);
}

#[test]
fn convert_to_http_response_builder_invalid_utf8_header_is_skipped() {
let mut response = super::Response::new(200, "OK", "tbr").unwrap();
add_header(
&mut response.headers,
HeaderLine::from(b"Some-Key: some\xff\xffvalue".to_vec())
.into_header()
.unwrap(),
);
dbg!(&response);
let http_response: http::Response<Vec<u8>> = response.into();

assert_eq!(http_response.headers().get("some-key"), None);
}

#[test]
fn convert_http_request_builder() {
use http::Request;
Expand Down Expand Up @@ -428,4 +457,34 @@ mod tests {
assert_eq!(http_request.uri(), "http://some-website.com");
assert_eq!(http_request.version(), http::Version::HTTP_11);
}

#[test]
fn convert_http_request_builder_invalid_utf8_header_is_skipped() {
use http::Request;

let http_request = Request::builder()
.method("PUT")
.header("Some-Key", "some\u{ffff}value")
.uri("https://google.com/?some=query");
let request: super::Request = http_request.into();

assert_eq!(request.header("some-key"), None);
assert_eq!(request.method(), "PUT");
assert_eq!(request.url(), "https://google.com/?some=query");
}

#[test]
fn convert_to_http_request_builder_invalid_utf8_header_is_skipped() {
use http::request::Builder;

let request = crate::agent()
.head("http://some-website.com")
.set("Some-Key", "some\u{ffff}value");
let http_request_builder: Builder = request.into();
let http_request = http_request_builder.body(()).unwrap();

assert_eq!(http_request.headers().get("some-key"), None);
assert_eq!(http_request.uri(), "http://some-website.com");
assert_eq!(http_request.version(), http::Version::HTTP_11);
}
}

0 comments on commit 6eeb933

Please sign in to comment.