Skip to content
Open
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
12 changes: 12 additions & 0 deletions docs/spec/options/hurl/discard_body.option
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: discard_body
long: discard-body
help: Discard response body
help_heading: HTTP options
config_file: true
env_var: HURL_DISCARD_BODY
---
Discard response body.

If there are no asserts and reports relying on the response body, this option can free the memory consumed by the response body earlier.

Without this option, Hurl keeps the response body in memory until the program ends. This can consume a large amount of memory if the payload is large, particularly when using parallel or repeated execution modes.
8 changes: 8 additions & 0 deletions docs/spec/options/hurl/truncate_body.option
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: truncate_body
long: truncate-body
help: Truncate response body
help_heading: HTTP options
config_file: true
env_var: HURL_TRUNCATE_BODY
---
Truncate the response body.
27 changes: 27 additions & 0 deletions integration/hurl/tests_ok/discard_body/discard_body.err.pattern
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
* ------------------------------------------------------------------------------
* Executing entry 1
*
* Cookie store:
*
* Request:
* GET http://localhost:8000/discard-body
*
* Request can be run with the following curl command:
* curl 'http://localhost:8000/discard-body'
*
> GET /discard-body HTTP/1.1
> Host: localhost:8000
> Accept: */*
> User-Agent: hurl/8.1.0
>
* Response:
*
< HTTP/1.1 200 OK
< Content-Length: 12
< Content-Type: text/html; charset=utf-8
< Date: <<<.*?>>>
< Server: Flask Server
< Via: waitress
<
* Received 0 bytes in <<<\d+>>> ms
*
2 changes: 2 additions & 0 deletions integration/hurl/tests_ok/discard_body/discard_body.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GET http://localhost:8000/discard-body
HTTP 200
Empty file.
4 changes: 4 additions & 0 deletions integration/hurl/tests_ok/discard_body/discard_body.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'

hurl --verbose --discard-body Accept tests_ok/discard_body/discard_body.hurl
6 changes: 6 additions & 0 deletions integration/hurl/tests_ok/discard_body/discard_body.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from app import app
from flask import request

@app.route("/discard-body")
def discard_body():
return "Hello World!"
4 changes: 4 additions & 0 deletions integration/hurl/tests_ok/discard_body/discard_body.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -Eeuo pipefail

hurl --verbose --discard-body tests_ok/discard_body/discard_body.hurl
1 change: 1 addition & 0 deletions integration/hurl/tests_ok/discard_body/hurl/config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--discard-body
4 changes: 4 additions & 0 deletions integration/hurl/tests_pty/help/help.out
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ HTTP options:
Add basic Authentication header to each request
-A, --user-agent <NAME>
Specify the User-Agent string to send to the HTTP server
--discard-body
Discard the response body
--truncate-body <SIZE>
Truncate the response body

Output options:
--color Colorize output
Expand Down
18 changes: 18 additions & 0 deletions packages/hurl/src/cli/options/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ pub fn parse_cli_args(
.arg(commands::verbose())
.arg(commands::very_verbose())
.arg(commands::verbosity())
.arg(commands::discard_body())
.arg(commands::truncate_body())
// Run options
.arg(commands::continue_on_error())
.arg(commands::delay())
Expand Down Expand Up @@ -223,6 +225,7 @@ fn parse_arg_matches(
let no_cookie_store = no_cookie_store(arg_matches, default_options.no_cookie_store);
let no_headers = no_headers(arg_matches, default_options.no_headers);
let no_proxy = no_proxy(arg_matches, default_options.no_proxy);
let discard_body = discard_body(arg_matches, default_options.discard_body);
let ntlm = ntlm(arg_matches, default_options.ntlm);
let parallel = parallel(arg_matches, default_options.parallel);
let path_as_is = path_as_is(arg_matches, default_options.path_as_is);
Expand All @@ -248,6 +251,7 @@ fn parse_arg_matches(
let user_agent = user_agent(arg_matches, default_options.user_agent);
let variables = variables(arg_matches, default_options.variables)?;
let verbosity = verbosity(arg_matches, default_options.verbosity);
let truncate_body = truncate_body(arg_matches, default_options.truncate_body);

Ok(CliOptions {
aws_sigv4,
Expand Down Expand Up @@ -292,6 +296,7 @@ fn parse_arg_matches(
no_cookie_store,
no_headers,
no_proxy,
discard_body,
ntlm,
path_as_is,
pinned_pub_key,
Expand All @@ -318,6 +323,7 @@ fn parse_arg_matches(
variables,
verbosity,
jobs,
truncate_body,
})
}

Expand Down Expand Up @@ -772,6 +778,14 @@ fn no_proxy(arg_matches: &ArgMatches, default_value: Option<String>) -> Option<S
get::<String>(arg_matches, "no_proxy").or(default_value)
}

fn discard_body(arg_matches: &ArgMatches, default_value: bool) -> bool {
if has_flag(arg_matches, "discard_body") {
true
} else {
default_value
}
}

fn ntlm(arg_matches: &ArgMatches, default_value: bool) -> bool {
if has_flag(arg_matches, "ntlm") {
true
Expand Down Expand Up @@ -841,6 +855,10 @@ fn proxy(arg_matches: &ArgMatches, default_value: Option<String>) -> Option<Stri
get::<String>(arg_matches, "proxy").or(default_value)
}

fn truncate_body(arg_matches: &ArgMatches, default_value: Option<u64>) -> Option<u64> {
get::<u64>(arg_matches, "truncate_body").or(default_value)
}

fn proxy_headers(arg_matches: &ArgMatches, default_value: Vec<String>) -> Vec<String> {
let mut proxy_headers = default_value;
if let Some(proxy_header) = get_strings(arg_matches, "proxy_header") {
Expand Down
19 changes: 19 additions & 0 deletions packages/hurl/src/cli/options/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,25 @@ pub fn report_json() -> clap::Arg {
.num_args(1)
}

pub fn discard_body() -> clap::Arg {
clap::Arg::new("discard_body")
.long("discard-body")
.help("Discard the response body")
.help_heading("HTTP options")
.action(clap::ArgAction::SetTrue)
}

pub fn truncate_body() -> clap::Arg {
clap::Arg::new("truncate_body")
.long("truncate-body")
.help("Truncate the response body")
.help_heading("HTTP options")
.value_parser(clap::value_parser!(i64))
.value_name("SIZE")
.num_args(1)
.allow_negative_numbers(true)
}

pub fn report_junit() -> clap::Arg {
clap::Arg::new("report_junit")
.long("report-junit")
Expand Down
11 changes: 10 additions & 1 deletion packages/hurl/src/cli/options/config_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ fn parse_option(reader: &mut Reader, options: &mut CliOptions) -> Result<(), Con
options.insecure = true;
Ok(())
}
"discard_body" => {
expect_no_value(reader)?;
options.discard_body = false;
Ok(())
}
"truncate_body" => {
expect_no_value(reader)?;
options.truncate_body = None;
Ok(())
}
"http1.0" => {
expect_no_value(reader)?;
options.http_version = Some(HttpVersion::V10);
Expand Down Expand Up @@ -439,7 +449,6 @@ fn parse_option(reader: &mut Reader, options: &mut CliOptions) -> Result<(), Con

#[cfg(test)]
mod tests {

use super::*;
use crate::cli::options::Verbosity;
use hurl_core::reader::Pos;
Expand Down
8 changes: 8 additions & 0 deletions packages/hurl/src/cli/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub struct CliOptions {
pub no_cookie_store: bool,
pub no_headers: Vec<String>,
pub no_proxy: Option<String>,
pub discard_body: bool,
pub ntlm: bool,
pub output: Option<Output>,
pub output_type: OutputType,
Expand All @@ -117,6 +118,7 @@ pub struct CliOptions {
pub user_agent: Option<String>,
pub variables: HashMap<String, Value>,
pub verbosity: Option<Verbosity>,
pub truncate_body: Option<u64>,
}

/// Log verbosity level
Expand Down Expand Up @@ -294,6 +296,7 @@ impl Default for CliOptions {
no_cookie_store: false,
no_headers: Vec::new(),
no_proxy: None,
discard_body: false,
ntlm: false,
output: None,
output_type: OutputType::ResponseBody,
Expand All @@ -319,6 +322,7 @@ impl Default for CliOptions {
user_agent: None,
variables: HashMap::new(),
verbosity: None,
truncate_body: None,
}
}
}
Expand Down Expand Up @@ -432,6 +436,8 @@ impl CliOptions {
let use_cookie_store = !self.no_cookie_store;
let user = self.user.clone();
let user_agent = self.user_agent.clone();
let discard_body = self.discard_body;
let truncate_body = self.truncate_body;

Ok(RunnerOptionsBuilder::new()
.aws_sigv4(aws_sigv4)
Expand Down Expand Up @@ -466,6 +472,7 @@ impl CliOptions {
.use_jsonpath_coercion(use_jsonpath_coercion)
.no_proxy(no_proxy)
.no_headers(no_headers)
.discard_body(discard_body)
.ntlm(ntlm)
.output(output)
.path_as_is(path_as_is)
Expand All @@ -483,6 +490,7 @@ impl CliOptions {
.use_cookie_store(use_cookie_store)
.user(user)
.user_agent(user_agent)
.truncate_body(truncate_body)
.build())
}

Expand Down
24 changes: 20 additions & 4 deletions packages/hurl/src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,26 @@ impl Client {
}
_ => {}
})?;
transfer.write_function(|data| {
response_body.extend(data);
Ok(data.len())
})?;

if !options.discard_body {
transfer.write_function(|data| {
if let Some(limit) = options.truncate_body {
let limit = limit as usize;
let current_len = response_body.len();

if current_len < limit {
let remaining = limit - current_len;
let bytes_to_take = data.len().min(remaining);

response_body.extend_from_slice(&data[..bytes_to_take]);
}
} else {
response_body.extend(data);
}

Ok(data.len())
})?;
}

if let Err(e) = transfer.perform() {
let code = e.code() as i32; // due to windows build
Expand Down
2 changes: 2 additions & 0 deletions packages/hurl/src/http/curl_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,8 @@ mod tests {
user: Some("user:password".to_string()),
user_agent: Some("my-useragent".to_string()),
verbosity: None,
discard_body: false,
truncate_body: None,
};

let cmd = CurlCmd::new(&request, &cookie_store, &context_dir, None, &options);
Expand Down
4 changes: 4 additions & 0 deletions packages/hurl/src/http/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub struct ClientOptions {
pub user: Option<String>,
pub user_agent: Option<String>,
pub verbosity: Option<Verbosity>,
pub discard_body: bool,
pub truncate_body: Option<u64>,
}

// FIXME/ we could implement copy here
Expand Down Expand Up @@ -117,6 +119,8 @@ impl Default for ClientOptions {
user: None,
user_agent: None,
verbosity: None,
discard_body: false,
truncate_body: None,
}
}
}
2 changes: 2 additions & 0 deletions packages/hurl/src/runner/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ impl ClientOptions {
Verbosity::Verbose => http::Verbosity::Verbose,
Verbosity::VeryVerbose => http::Verbosity::VeryVerbose,
}),
discard_body: runner_options.discard_body,
truncate_body: runner_options.truncate_body,
}
}
}
Expand Down
23 changes: 21 additions & 2 deletions packages/hurl/src/runner/runner_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
* limitations under the License.
*
*/
use std::time::Duration;

use hurl_core::types::{BytesPerSec, Count};
use std::time::Duration;

use crate::http::{FollowLocation, HeaderVec, IpResolve, RequestedHttpVersion};
use crate::pretty::PrettyMode;
Expand Down Expand Up @@ -79,6 +78,8 @@ pub struct RunnerOptionsBuilder {
use_jsonpath_coercion: bool,
user: Option<String>,
user_agent: Option<String>,
discard_body: bool,
truncate_body: Option<u64>,
}

impl Default for RunnerOptionsBuilder {
Expand Down Expand Up @@ -136,6 +137,8 @@ impl Default for RunnerOptionsBuilder {
use_jsonpath_coercion: true,
user: None,
user_agent: None,
discard_body: false,
truncate_body: None,
}
}
}
Expand Down Expand Up @@ -337,6 +340,11 @@ impl RunnerOptionsBuilder {
self
}

pub fn truncate_body(&mut self, truncate_body: Option<u64>) -> &mut Self {
self.truncate_body = truncate_body;
self
}

/// Enables HTTP Digest authentication.
pub fn digest(&mut self, digest: bool) -> &mut Self {
self.digest = digest;
Expand Down Expand Up @@ -373,6 +381,11 @@ impl RunnerOptionsBuilder {
self
}

pub fn discard_body(&mut self, discard_body: bool) -> &mut Self {
self.discard_body = discard_body;
self
}

/// Sets list of hosts which do not use a proxy.
pub fn no_proxy(&mut self, no_proxy: Option<String>) -> &mut Self {
self.no_proxy = no_proxy;
Expand Down Expand Up @@ -553,6 +566,8 @@ impl RunnerOptionsBuilder {
use_jsonpath_coercion: self.use_jsonpath_coercion,
user: self.user.clone(),
user_agent: self.user_agent.clone(),
discard_body: self.discard_body,
truncate_body: self.truncate_body,
}
}
}
Expand Down Expand Up @@ -668,6 +683,10 @@ pub struct RunnerOptions {
pub(crate) user: Option<String>,
/// Specifies the User-Agent string to send to the HTTP server.
pub(crate) user_agent: Option<String>,
/// Specifies whether to discard the response body.
pub(crate) discard_body: bool,
/// Specifies the maximum number of bytes to truncate the response body.
pub(crate) truncate_body: Option<u64>,
}

impl Default for RunnerOptions {
Expand Down
Loading
Loading