Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
zuisong committed Jun 21, 2024
1 parent 697422a commit cb44ff5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn get_next_request(mut request: Request, response: &Response) -> Option<Request
.headers()
.get(LOCATION)
.and_then(|location| location.to_utf8_str().ok())
.and_then(|location| request.url().join(&location).ok())
.and_then(|location| request.url().join(location).ok())
};

match response.status() {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::path::{Path, PathBuf};
use std::str::Utf8Error;

use anyhow::Result;
use reqwest::header::HeaderValue;
use reqwest::blocking::Request;
use reqwest::header::HeaderValue;
use url::Url;

pub fn unescape(text: &str, special_chars: &'static str) -> String {
Expand Down
84 changes: 84 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,90 @@ fn body_from_raw() {
.success();
}

#[test]
fn support_utf8_header_value() {
let server = server::http(|req| async move {
assert_eq!(req.headers()["hello"].as_bytes(), "你好".as_bytes());
hyper::Response::builder()
// Valid JSON, but not declared as text
.header("hello", "你好呀")
.body("".into())
.unwrap()
});

get_command()
.args([&server.base_url(), "hello:你好"])
.assert()
.stdout(contains("Hello: 你好呀"))
.success();
}

#[test]
fn redirect_support_utf8_location() {
let server = server::http(|req| async move {
match req.uri().path() {
"/first_page" => hyper::Response::builder()
.status(302)
.header("Date", "N/A")
.header("Location", "/page二")
.body("redirecting...".into())
.unwrap(),
"/page%E4%BA%8C" => hyper::Response::builder()
.header("Date", "N/A")
.body("final destination".into())
.unwrap(),
_ => panic!("unknown path"),
}
});

get_command()
.args([&server.url("/first_page"), "--follow", "--verbose", "--all"])
.assert()
.stdout(indoc! {r#"
GET /first_page HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, br, zstd
Connection: keep-alive
Host: http.mock
User-Agent: xh/0.0.0 (test mode)
HTTP/1.1 302 Found
Content-Length: 14
Date: N/A
Location: /page二
redirecting...
GET /page%E4%BA%8C HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, br, zstd
Connection: keep-alive
Host: http.mock
User-Agent: xh/0.0.0 (test mode)
HTTP/1.1 200 OK
Content-Length: 17
Date: N/A
final destination
"#});
}

#[test]
fn to_curl_support_utf8_header_value() {
get_command()
.args(["https://exmaple.com/", "hello:你好", "--curl"])
.assert()
.stdout(contains("curl https://exmaple.com/ -H 'hello: 你好'"))
.success();

get_command()
.args(["https://exmaple.com/", "hello:你好", "--curl-long"])
.assert()
.stdout(contains("curl https://exmaple.com/ --header 'hello: 你好'"))
.success();
}

#[test]
fn mixed_stdin_request_items() {
redirecting_command()
Expand Down

0 comments on commit cb44ff5

Please sign in to comment.