mirror of
https://github.com/ducaale/xh.git
synced 2025-05-05 15:32:50 +00:00
apply suggestion
This commit is contained in:
parent
2a1fba66f7
commit
1ec0267a18
25
src/main.rs
25
src/main.rs
@ -511,24 +511,19 @@ fn run(args: Cli) -> Result<i32> {
|
||||
let mut request = request_builder.headers(headers).build()?;
|
||||
|
||||
if args.compress >= 1 && request.headers().get(CONTENT_ENCODING).is_none() {
|
||||
let mut compressed = false;
|
||||
if let Some(body) = request.body_mut() {
|
||||
if let Ok(body_bytes) = body.buffer() {
|
||||
let mut encoder = ZlibEncoder::new(Vec::new(), Default::default());
|
||||
encoder.write_all(body_bytes)?;
|
||||
let output = encoder.finish()?;
|
||||
if output.len() < body_bytes.len() || args.compress >= 2 {
|
||||
let _ = std::mem::replace(body, ReqwestBody::from(output));
|
||||
compressed = true;
|
||||
}
|
||||
let body_bytes = body.buffer()?;
|
||||
let mut encoder = ZlibEncoder::new(Vec::new(), Default::default());
|
||||
encoder.write_all(body_bytes)?;
|
||||
let output = encoder.finish()?;
|
||||
if output.len() < body_bytes.len() || args.compress >= 2 {
|
||||
let _ = std::mem::replace(body, ReqwestBody::from(output));
|
||||
request
|
||||
.headers_mut()
|
||||
.entry(CONTENT_ENCODING)
|
||||
.or_insert(HeaderValue::from_static("deflate"));
|
||||
}
|
||||
}
|
||||
if compressed {
|
||||
request
|
||||
.headers_mut()
|
||||
.entry(CONTENT_ENCODING)
|
||||
.or_insert(HeaderValue::from_static("deflate"));
|
||||
}
|
||||
}
|
||||
|
||||
for header in &headers_to_unset {
|
||||
|
28
tests/cli.rs
28
tests/cli.rs
@ -785,15 +785,33 @@ fn successful_digest_auth() {
|
||||
fn compress_request_body_online() {
|
||||
get_command()
|
||||
.arg("https://postman-echo.com/post")
|
||||
.args(["-xx", "--body", &format!("a={}", "1".repeat(1000))])
|
||||
.args(["--body", "-f", &format!("a={}", "1".repeat(1000))])
|
||||
.assert()
|
||||
.stdout(function(|body: &str| {
|
||||
let json: Value = serde_json::from_str(body).unwrap();
|
||||
assert_eq!(json["json"]["a"], Value::String("1".repeat(1000)));
|
||||
if let Some(request_body_length) = json["headers"]["content-length"].as_str() {
|
||||
let length: i32 = request_body_length.parse().unwrap();
|
||||
assert!(length < 1000)
|
||||
}
|
||||
let length: i32 = json["headers"]["content-length"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.parse()
|
||||
.unwrap();
|
||||
assert_eq!(length, 1002);
|
||||
|
||||
true
|
||||
}));
|
||||
get_command()
|
||||
.arg("https://postman-echo.com/post")
|
||||
.args(["-x", "--body", "-f", &format!("a={}", "1".repeat(1000))])
|
||||
.assert()
|
||||
.stdout(function(|body: &str| {
|
||||
let json: Value = serde_json::from_str(body).unwrap();
|
||||
assert_eq!(json["json"]["a"], Value::String("1".repeat(1000)));
|
||||
let length: i32 = json["headers"]["content-length"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.parse()
|
||||
.unwrap();
|
||||
assert!(length < 1000);
|
||||
|
||||
true
|
||||
}));
|
||||
|
Loading…
x
Reference in New Issue
Block a user