Skip to content

Commit

Permalink
Add BasicAuth section
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricereix committed Dec 17, 2021
1 parent ce79951 commit 82894a8
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 17 deletions.
3 changes: 3 additions & 0 deletions integration/tests/basic_authentication_per_request.curl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
curl 'http://bob:secret@localhost:8000/basic-authentication-per-request'
curl 'http://localhost:8000/basic-authentication-per-request' -H 'Authorization: Basic Ym9iOnNlY3JldA=='
curl 'http://localhost:8000/basic-authentication-per-request' -H 'Authorization: Basic Ym9iOnNlY3JldA=='
File renamed without changes.
15 changes: 15 additions & 0 deletions integration/tests/basic_authentication_per_request.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<pre><code class="language-hurl"><span class="hurl-entry"><span class="request"><span class="line"><span class="method">GET</span> <span class="url">http://bob:secret@localhost:8000/basic-authentication-per-request</span></span>
</span><span class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
<span class="raw"><span class="line">```You are authenticated```</span></span>
</span></span><span class="hurl-entry"><span class="request"><span class="line"></span>
<span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/basic-authentication-per-request</span></span>
<span class="line"><span class="string">Authorization</span><span>:</span> <span class="string">Basic Ym9iOnNlY3JldA==</span></span>
</span><span class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
<span class="raw"><span class="line">```You are authenticated```</span></span>
</span></span><span class="hurl-entry"><span class="request"><span class="line"></span>
<span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/basic-authentication-per-request</span></span>
<span class="line section-header">[BasicAuth]</span>
<span class="line"><span class="string">bob</span><span>:</span><span class="string">secret</span></span>
</span><span class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
<span class="raw"><span class="line">```You are authenticated```</span></span>
</span></span></code></pre>
14 changes: 14 additions & 0 deletions integration/tests/basic_authentication_per_request.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
GET http://bob:secret@localhost:8000/basic-authentication-per-request
HTTP/1.0 200
```You are authenticated```

GET http://localhost:8000/basic-authentication-per-request
Authorization: Basic Ym9iOnNlY3JldA==
HTTP/1.0 200
```You are authenticated```

GET http://localhost:8000/basic-authentication-per-request
[BasicAuth]
bob:secret
HTTP/1.0 200
```You are authenticated```
1 change: 1 addition & 0 deletions integration/tests/basic_authentication_per_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://bob:secret@localhost:8000/basic-authentication-per-request"},"response":{"version":"HTTP/1.0","status":200,"body":{"type":"raw-string","value":"You are authenticated"}}},{"request":{"method":"GET","url":"http://localhost:8000/basic-authentication-per-request","headers":[{"name":"Authorization","value":"Basic Ym9iOnNlY3JldA=="}]},"response":{"version":"HTTP/1.0","status":200,"body":{"type":"raw-string","value":"You are authenticated"}}},{"request":{"method":"GET","url":"http://localhost:8000/basic-authentication-per-request"},"response":{"version":"HTTP/1.0","status":200,"body":{"type":"raw-string","value":"You are authenticated"}}}]}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from tests import app
from flask import request

@app.route('/user-in-url')
def user_in_url():
@app.route('/basic-authentication-per-request')
def basic_authentication_per_request():
assert request.headers['Authorization'] == 'Basic Ym9iOnNlY3JldA=='
return 'You are authenticated'

Expand Down
1 change: 0 additions & 1 deletion integration/tests/user_in_url.curl

This file was deleted.

6 changes: 0 additions & 6 deletions integration/tests/user_in_url.html

This file was deleted.

5 changes: 0 additions & 5 deletions integration/tests/user_in_url.hurl

This file was deleted.

1 change: 0 additions & 1 deletion integration/tests/user_in_url.json

This file was deleted.

10 changes: 10 additions & 0 deletions packages/hurl/src/runner/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ pub fn eval_request(
headers.push(http::Header { name, value });
}

if let Some(kv) = request.clone().basic_auth() {
let value = eval_template(kv.value, variables)?;
let user_password = format!("{}:{}", kv.key.value, value);
let authorization = base64::encode(user_password.as_bytes());

let name = "Authorization".to_string();
let value = format!("Basic {}", authorization);
headers.push(http::Header { name, value });
}

let mut querystring: Vec<http::Param> = vec![];
for param in request.clone().querystring_params() {
let name = param.key.value;
Expand Down
12 changes: 12 additions & 0 deletions packages/hurl_core/src/ast/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ impl Request {
}
return vec![];
}

pub fn basic_auth(self) -> Option<KeyValue> {
for section in self.sections {
if let SectionValue::BasicAuth(kv) = section.value {
return Some(kv);
}
}
None
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -193,6 +202,7 @@ impl Section {
match self.value {
SectionValue::Asserts(_) => "Asserts",
SectionValue::QueryParams(_) => "QueryStringParams",
SectionValue::BasicAuth(_) => "BasicAuth",
SectionValue::FormParams(_) => "FormParams",
SectionValue::Cookies(_) => "Cookies",
SectionValue::Captures(_) => "Captures",
Expand All @@ -202,8 +212,10 @@ impl Section {
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[allow(clippy::large_enum_variant)]
pub enum SectionValue {
QueryParams(Vec<KeyValue>),
BasicAuth(KeyValue),
FormParams(Vec<KeyValue>),
MultipartFormData(Vec<MultipartParam>),
Cookies(Vec<Cookie>),
Expand Down
1 change: 1 addition & 0 deletions packages/hurl_core/src/format/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl Htmlable for SectionValue {
buffer.push_str(item.to_html().as_str())
}
}
SectionValue::BasicAuth(item) => buffer.push_str(item.to_html().as_str()),
SectionValue::FormParams(items) => {
for item in items {
buffer.push_str(item.to_html().as_str())
Expand Down
6 changes: 6 additions & 0 deletions packages/hurl_core/src/parser/sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn request_section(reader: &mut Reader) -> ParseResult<'static, Section> {
let line_terminator0 = line_terminator(reader)?;
let value = match name.as_str() {
"QueryStringParams" => section_value_query_params(reader)?,
"BasicAuth" => section_value_basic_auth(reader)?,
"FormParams" => section_value_form_params(reader)?,
"MultipartFormData" => section_value_multipart_form_data(reader)?,
"Cookies" => section_value_cookies(reader)?,
Expand Down Expand Up @@ -119,6 +120,11 @@ fn section_value_query_params(reader: &mut Reader) -> ParseResult<'static, Secti
Ok(SectionValue::QueryParams(items))
}

fn section_value_basic_auth(reader: &mut Reader) -> ParseResult<'static, SectionValue> {
let kv = key_value(reader)?;
Ok(SectionValue::BasicAuth(kv))
}

fn section_value_form_params(reader: &mut Reader) -> ParseResult<'static, SectionValue> {
let items = zero_or_more(key_value, reader)?;
Ok(SectionValue::FormParams(items))
Expand Down
3 changes: 3 additions & 0 deletions packages/hurlfmt/src/format/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ impl Tokenizable for SectionValue {
items.iter().flat_map(|e| e.tokenize()).collect(),
);
}
SectionValue::BasicAuth(item) => {
tokens.append(&mut item.tokenize());
}
SectionValue::FormParams(items) => {
add_tokens(
&mut tokens,
Expand Down
6 changes: 4 additions & 2 deletions packages/hurlfmt/src/linter/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl Lintable<SectionValue> for SectionValue {
SectionValue::QueryParams(params) => {
SectionValue::QueryParams(params.iter().map(|e| e.lint()).collect())
}
SectionValue::BasicAuth(param) => SectionValue::BasicAuth(param.lint()),
SectionValue::Captures(captures) => {
SectionValue::Captures(captures.iter().map(|e| e.lint()).collect())
}
Expand All @@ -191,8 +192,9 @@ impl Lintable<SectionValue> for SectionValue {
fn section_value_index(section_value: SectionValue) -> u32 {
match section_value {
SectionValue::QueryParams(_) => 0,
SectionValue::FormParams(_) => 1,
SectionValue::MultipartFormData(_) => 2,
SectionValue::BasicAuth(_) => 1,
SectionValue::FormParams(_) => 2,
SectionValue::MultipartFormData(_) => 3,
SectionValue::Cookies(_) => 3,
SectionValue::Captures(_) => 0,
SectionValue::Asserts(_) => 1,
Expand Down

0 comments on commit 82894a8

Please sign in to comment.