mirror of
https://github.com/Orange-OpenSource/hurl.git
synced 2024-12-26 20:42:01 +03:00
Merge pull request #48 from Orange-OpenSource/feature/add-option-user
Add option user
This commit is contained in:
commit
d348679d9b
@ -133,6 +133,11 @@ Read cookies from file (using the Netscape cookie file format).
|
||||
Combined with \fI-c, --cookie-jar\fP, you can simulate a cookie storage between successive Hurl runs.
|
||||
|
||||
|
||||
.IP "--compressed"
|
||||
|
||||
Request a compressed response using one of the algorithms br, gzip, deflate and automatically decompress the content.
|
||||
|
||||
|
||||
.IP "--connect-timeout <seconds> "
|
||||
|
||||
Maximum time in seconds that you allow hurl's connection to take.
|
||||
@ -251,6 +256,9 @@ Write output to <file> instead of stdout.
|
||||
|
||||
Use the specified proxy.
|
||||
|
||||
.IP "-u, --user <user:password> "
|
||||
|
||||
Add basic Authentication header to each request.
|
||||
|
||||
|
||||
.IP "--variable <name=value> "
|
||||
|
@ -253,6 +253,9 @@ Write output to <file> instead of stdout.
|
||||
|
||||
Use the specified proxy.
|
||||
|
||||
### -u, --user <user:password> {#user}
|
||||
|
||||
Add basic Authentication header to each request.
|
||||
|
||||
|
||||
### --variable <name=value> {#variable}
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
1
integration/tests/basic_authentication.exit
Normal file
1
integration/tests/basic_authentication.exit
Normal file
@ -0,0 +1 @@
|
||||
0
|
5
integration/tests/basic_authentication.hurl
Normal file
5
integration/tests/basic_authentication.hurl
Normal file
@ -0,0 +1,5 @@
|
||||
GET http://localhost:8000/basic-authentication
|
||||
HTTP/1.0 200
|
||||
```You are authenticated```
|
||||
|
||||
|
1
integration/tests/basic_authentication.options
Normal file
1
integration/tests/basic_authentication.options
Normal file
@ -0,0 +1 @@
|
||||
--user bob:secret
|
1
integration/tests/basic_authentication.out
Normal file
1
integration/tests/basic_authentication.out
Normal file
@ -0,0 +1 @@
|
||||
You are authenticated
|
10
integration/tests/basic_authentication.py
Normal file
10
integration/tests/basic_authentication.py
Normal file
@ -0,0 +1,10 @@
|
||||
from tests import app
|
||||
from flask import request
|
||||
|
||||
@app.route('/basic-authentication')
|
||||
def basic_authentication():
|
||||
assert request.headers['Authorization'] == 'Basic Ym9iOnNlY3JldA=='
|
||||
return 'You are authenticated'
|
||||
|
||||
|
||||
|
@ -53,6 +53,7 @@ pub struct CLIOptions {
|
||||
pub timeout: Duration,
|
||||
pub connect_timeout: Duration,
|
||||
pub compressed: bool,
|
||||
pub user: Option<String>,
|
||||
}
|
||||
|
||||
fn execute(
|
||||
@ -126,6 +127,7 @@ fn execute(
|
||||
|
||||
let timeout = cli_options.timeout;
|
||||
let connect_timeout = cli_options.connect_timeout;
|
||||
let user = cli_options.user;
|
||||
let options = http::ClientOptions {
|
||||
follow_location,
|
||||
max_redirect,
|
||||
@ -136,6 +138,7 @@ fn execute(
|
||||
insecure,
|
||||
timeout,
|
||||
connect_timeout,
|
||||
user,
|
||||
};
|
||||
let mut client = http::Client::init(options);
|
||||
|
||||
@ -429,6 +432,14 @@ fn app() -> clap::App<'static, 'static> {
|
||||
.help("Execute hurl file to ENTRY_NUMBER (starting at 1)")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
clap::Arg::with_name("user")
|
||||
.short("u")
|
||||
.long("user")
|
||||
.value_name("user:password")
|
||||
.help("Add basic Authentication header to each request.")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
clap::Arg::with_name("variable")
|
||||
.long("variable")
|
||||
@ -507,6 +518,7 @@ fn parse_options(matches: ArgMatches) -> Result<CLIOptions, cli::CLIError> {
|
||||
},
|
||||
};
|
||||
let compressed = matches.is_present("compressed");
|
||||
let user = matches.value_of("user").map(|x| x.to_string());
|
||||
|
||||
Ok(CLIOptions {
|
||||
verbose,
|
||||
@ -523,6 +535,7 @@ fn parse_options(matches: ArgMatches) -> Result<CLIOptions, cli::CLIError> {
|
||||
timeout,
|
||||
connect_timeout,
|
||||
compressed,
|
||||
user,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -53,6 +53,7 @@ pub struct Client {
|
||||
pub redirect_count: usize,
|
||||
pub max_redirect: Option<usize>,
|
||||
pub verbose: bool,
|
||||
pub authorization: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@ -66,6 +67,7 @@ pub struct ClientOptions {
|
||||
pub insecure: bool,
|
||||
pub timeout: Duration,
|
||||
pub connect_timeout: Duration,
|
||||
pub user: Option<String>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
@ -98,12 +100,14 @@ impl Client {
|
||||
h.timeout(options.timeout).unwrap();
|
||||
h.connect_timeout(options.connect_timeout).unwrap();
|
||||
|
||||
let authorization = options.user.map(|user| base64::encode(user.as_bytes()));
|
||||
Client {
|
||||
handle: Box::new(h),
|
||||
follow_location: options.follow_location,
|
||||
max_redirect: options.max_redirect,
|
||||
redirect_count: 0,
|
||||
verbose: options.verbose,
|
||||
authorization,
|
||||
}
|
||||
}
|
||||
|
||||
@ -301,15 +305,18 @@ impl Client {
|
||||
list.append("Expect:").unwrap(); // remove header Expect
|
||||
}
|
||||
|
||||
// if request.form.is_empty() && request.multipart.is_empty() && request.body.is_empty() {
|
||||
// list.append("Content-Length:").unwrap();
|
||||
// }
|
||||
|
||||
if get_header_values(request.headers.clone(), "User-Agent".to_string()).is_empty() {
|
||||
list.append(format!("User-Agent: hurl/{}", clap::crate_version!()).as_str())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if let Some(authorization) = self.authorization.clone() {
|
||||
if get_header_values(request.headers.clone(), "Authorization".to_string()).is_empty() {
|
||||
list.append(format!("Authorization: Basic {}", authorization).as_str())
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
self.handle.http_headers(list).unwrap();
|
||||
}
|
||||
|
||||
|
@ -61,6 +61,7 @@ use super::value::Value;
|
||||
/// insecure: false,
|
||||
/// timeout: Default::default(),
|
||||
/// connect_timeout: Default::default(),
|
||||
/// user: None,
|
||||
/// };
|
||||
/// let mut client = http::Client::init(options);
|
||||
///
|
||||
|
@ -65,6 +65,7 @@ fn default_client() -> Client {
|
||||
insecure: false,
|
||||
timeout: Default::default(),
|
||||
connect_timeout: Duration::from_secs(300),
|
||||
user: None,
|
||||
};
|
||||
Client::init(options)
|
||||
}
|
||||
@ -306,6 +307,7 @@ fn test_follow_location() {
|
||||
insecure: false,
|
||||
timeout: Default::default(),
|
||||
connect_timeout: Default::default(),
|
||||
user: None,
|
||||
};
|
||||
let mut client = Client::init(options);
|
||||
let response = client.execute(&request, 0).unwrap();
|
||||
@ -339,6 +341,7 @@ fn test_max_redirect() {
|
||||
insecure: false,
|
||||
timeout: Default::default(),
|
||||
connect_timeout: Default::default(),
|
||||
user: None,
|
||||
};
|
||||
let mut client = Client::init(options);
|
||||
let request = default_get_request("http://localhost:8000/redirect".to_string());
|
||||
@ -450,6 +453,38 @@ fn test_expect() {
|
||||
assert!(response.body.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_basic_authentication() {
|
||||
let options = ClientOptions {
|
||||
follow_location: false,
|
||||
max_redirect: None,
|
||||
cookie_input_file: None,
|
||||
proxy: None,
|
||||
no_proxy: None,
|
||||
verbose: true,
|
||||
insecure: false,
|
||||
timeout: Default::default(),
|
||||
connect_timeout: Duration::from_secs(300),
|
||||
user: Some("bob:secret".to_string()),
|
||||
};
|
||||
let mut client = Client::init(options);
|
||||
let request = Request {
|
||||
method: Method::Get,
|
||||
url: "http://localhost:8000/basic-authentication".to_string(),
|
||||
headers: vec![],
|
||||
querystring: vec![],
|
||||
form: vec![],
|
||||
multipart: vec![],
|
||||
cookies: vec![],
|
||||
body: vec![],
|
||||
content_type: None,
|
||||
};
|
||||
let response = client.execute(&request, 0).unwrap();
|
||||
assert_eq!(response.status, 200);
|
||||
assert_eq!(response.version, Version::Http10);
|
||||
assert_eq!(response.body, b"You are authenticated".to_vec());
|
||||
}
|
||||
|
||||
// region error
|
||||
|
||||
#[test]
|
||||
@ -478,6 +513,7 @@ fn test_error_fail_to_connect() {
|
||||
insecure: false,
|
||||
timeout: Default::default(),
|
||||
connect_timeout: Default::default(),
|
||||
user: None,
|
||||
};
|
||||
let mut client = Client::init(options);
|
||||
let request = default_get_request("http://localhost:8000/hello".to_string());
|
||||
@ -497,6 +533,7 @@ fn test_error_could_not_resolve_proxy_name() {
|
||||
insecure: false,
|
||||
timeout: Default::default(),
|
||||
connect_timeout: Default::default(),
|
||||
user: None,
|
||||
};
|
||||
let mut client = Client::init(options);
|
||||
let request = default_get_request("http://localhost:8000/hello".to_string());
|
||||
@ -516,6 +553,7 @@ fn test_timeout() {
|
||||
insecure: false,
|
||||
timeout: Duration::from_millis(100),
|
||||
connect_timeout: Default::default(),
|
||||
user: None,
|
||||
};
|
||||
let mut client = Client::init(options);
|
||||
let request = default_get_request("http://localhost:8000/timeout".to_string());
|
||||
@ -537,6 +575,7 @@ fn test_connect_timeout() {
|
||||
insecure: false,
|
||||
timeout: Default::default(),
|
||||
connect_timeout: Duration::from_secs(1),
|
||||
user: None,
|
||||
};
|
||||
let mut client = Client::init(options);
|
||||
let request = default_get_request("http://example.com:81".to_string());
|
||||
@ -639,6 +678,7 @@ fn test_cookie_file() {
|
||||
insecure: false,
|
||||
timeout: Default::default(),
|
||||
connect_timeout: Default::default(),
|
||||
user: None,
|
||||
};
|
||||
let mut client = Client::init(options);
|
||||
let request = default_get_request(
|
||||
@ -666,6 +706,7 @@ fn test_proxy() {
|
||||
insecure: false,
|
||||
timeout: Default::default(),
|
||||
connect_timeout: Default::default(),
|
||||
user: None,
|
||||
};
|
||||
let mut client = Client::init(options);
|
||||
let request = default_get_request("http://localhost:8000/hello".to_string());
|
||||
|
@ -53,6 +53,7 @@ fn test_hurl_file() {
|
||||
insecure: false,
|
||||
timeout: Default::default(),
|
||||
connect_timeout: Default::default(),
|
||||
user: None,
|
||||
};
|
||||
let mut client = http::Client::init(options);
|
||||
let mut lines: Vec<&str> = regex::Regex::new(r"\n|\r\n")
|
||||
@ -159,6 +160,7 @@ fn test_hello() {
|
||||
insecure: false,
|
||||
timeout: Default::default(),
|
||||
connect_timeout: Default::default(),
|
||||
user: None,
|
||||
};
|
||||
let mut client = http::Client::init(options);
|
||||
let source_info = SourceInfo {
|
||||
|
Loading…
Reference in New Issue
Block a user