Skip to content
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

support compress request body #403

Merged
merged 11 commits into from
Feb 12, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
can not combine compress with multipart
zuisong committed Feb 4, 2025

Verified

This commit was signed with the committer’s verified signature.
zuisong zuisong
commit d9026f185092c3909245f0db619aec0e91b68d33
2 changes: 1 addition & 1 deletion completions/_xh
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ none\:"Disable both coloring and formatting"))' \
'--json[(default) Serialize data items from the command line as a JSON object]' \
'-f[Serialize data items from the command line as form fields]' \
'--form[Serialize data items from the command line as form fields]' \
'(--raw)--multipart[Like --form, but force a multipart/form-data request even without files]' \
'(--raw -x --compress)--multipart[Like --form, but force a multipart/form-data request even without files]' \
'-h[Print only the response headers. Shortcut for --print=h]' \
'--headers[Print only the response headers. Shortcut for --print=h]' \
'-b[Print only the response body. Shortcut for --print=b]' \
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ pub struct Cli {
/// Like --form, but force a multipart/form-data request even without files.
///
/// Overrides both --json and --form.
#[clap(long, conflicts_with = "raw", overrides_with_all = &["json", "form"])]
#[clap(long, conflicts_with_all = &["raw", "compress"], overrides_with_all = &["json", "form"])]
pub multipart: bool,

/// Pass raw request data without extra processing.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -512,7 +512,7 @@ fn run(args: Cli) -> Result<i32> {

if args.compress >= 1 && request.headers().get(CONTENT_ENCODING).is_none() {
if let Some(body) = request.body_mut() {
// TODO: Compress file body (Multipart and File) without buffering
// TODO: Compress file body (File) without buffering
let body_bytes = body.buffer()?;
let mut encoder = ZlibEncoder::new(Vec::new(), Default::default());
encoder.write_all(body_bytes)?;
14 changes: 13 additions & 1 deletion tests/cases/compress_request_body.rs
Original file line number Diff line number Diff line change
@@ -87,6 +87,7 @@ fn compress_request_body_form() {
key={c}
"#, c = "1".repeat(1000),});
}

#[test]
fn skip_compression_when_compression_ratio_is_negative() {
let server = server();
@@ -105,7 +106,7 @@ fn skip_compression_when_compression_ratio_is_negative() {
}

#[test]
fn compress_request_body_force() {
fn test_compress_force_with_negative_ratio() {
let server = server();
get_command()
.arg(format!("{}/deflate", server.base_url()))
@@ -195,3 +196,14 @@ fn compress_body_from_file_unless_compress_rate_less_1() {
.assert()
.success();
}
#[test]
fn test_cannot_combine_compress_with_multipart() {
get_command()
.arg(format!("{}/deflate", ""))
.args(["--multipart", "-x", "a=1"])
.assert()
.failure()
.stderr(predicates::str::contains(
"the argument '--multipart' cannot be used with '--compress...'",
));
}