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

Add square brackets in key-string #458

Merged
merged 1 commit into from
Jan 25, 2022
Merged
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
4 changes: 2 additions & 2 deletions integration/tests/form_params.curl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
curl 'http://localhost:8000/form-params' --data 'param1=value1' --data 'param2=' --data 'param3=a%3Db' --data 'param4=a%253db'
curl 'http://localhost:8000/form-params' -H 'Content-Type: application/x-www-form-urlencoded' --data 'param1=value1&param2=&param3=a%3db&param4=a%253db'
curl 'http://localhost:8000/form-params' --data 'param1=value1' --data 'param2=' --data 'param3=a%3Db' --data 'param4=a%253db' --data 'values[0]=0' --data 'values[1]=1'
curl 'http://localhost:8000/form-params' -H 'Content-Type: application/x-www-form-urlencoded' --data 'param1=value1&param2=&param3=a%3db&param4=a%253db&values[0]=0&values[1]=1'
4 changes: 3 additions & 1 deletion integration/tests/form_params.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
<span class="line"><span class="string">param2</span><span>:</span><span class="string"></span></span>
<span class="line"><span class="string">param3</span><span>:</span> <span class="string">a=b</span></span>
<span class="line"><span class="string">param4</span><span>:</span> <span class="string">a%3db</span></span>
<span class="line"><span class="string">values\u{5b}0\u{5d}</span><span>:</span> <span class="string">0</span></span>
<span class="line"><span class="string">values[1]</span><span>:</span> <span class="string">1</span></span>
</span><span class="response"><span class="line"></span>
<span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
</span></span><span class="hurl-entry"><span class="request"><span class="line"></span>
<span class="line"></span><span class="comment"># same version as raw</span>
<span class="line"><span class="method">POST</span> <span class="url">http://localhost:8000/form-params</span></span>
<span class="line"><span class="string">Content-Type</span><span>:</span> <span class="string">application/x-www-form-urlencoded</span></span>
<span class="raw"><span class="line">```param1=value1&amp;param2=&amp;param3=a%3db&amp;param4=a%253db```</span></span>
<span class="raw"><span class="line">```param1=value1&amp;param2=&amp;param3=a%3db&amp;param4=a%253db&amp;values[0]=0&amp;values[1]=1```</span></span>
</span><span class="response"><span class="line"></span>
<span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
</span></span><span class="line"></span>
Expand Down
4 changes: 3 additions & 1 deletion integration/tests/form_params.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ param1: value1
param2:
param3: a=b
param4: a%3db
values\u{5b}0\u{5d}: 0
values[1]: 1

HTTP/1.0 200

# same version as raw
POST http://localhost:8000/form-params
Content-Type: application/x-www-form-urlencoded
```param1=value1&param2=&param3=a%3db&param4=a%253db```
```param1=value1&param2=&param3=a%3db&param4=a%253db&values[0]=0&values[1]=1```

HTTP/1.0 200

Expand Down
2 changes: 1 addition & 1 deletion integration/tests/form_params.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"entries":[{"request":{"method":"POST","url":"http://localhost:8000/form-params","form_params":[{"name":"param1","value":"value1"},{"name":"param2","value":""},{"name":"param3","value":"a=b"},{"name":"param4","value":"a%3db"}]},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/form-params","headers":[{"name":"Content-Type","value":"application/x-www-form-urlencoded"}],"body":{"type":"raw-string","value":"param1=value1&param2=&param3=a%3db&param4=a%253db"}},"response":{"version":"HTTP/1.0","status":200}}]}
{"entries":[{"request":{"method":"POST","url":"http://localhost:8000/form-params","form_params":[{"name":"param1","value":"value1"},{"name":"param2","value":""},{"name":"param3","value":"a=b"},{"name":"param4","value":"a%3db"},{"name":"values[0]","value":"0"},{"name":"values[1]","value":"1"}]},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/form-params","headers":[{"name":"Content-Type","value":"application/x-www-form-urlencoded"}],"body":{"type":"raw-string","value":"param1=value1&param2=&param3=a%3db&param4=a%253db&values[0]=0&values[1]=1"}},"response":{"version":"HTTP/1.0","status":200}}]}
2 changes: 2 additions & 0 deletions integration/tests/form_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def form_params():
assert request.form['param2'] == ''
assert request.form['param3'] == 'a=b'
assert request.form['param4'] == 'a%3db'
assert request.form['values[0]'] == '0'
assert request.form['values[1]'] == '1'
return ''


10 changes: 9 additions & 1 deletion packages/hurl/tests/libcurl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ fn test_form_params() {
name: "param4".to_string(),
value: "a%3db".to_string(),
},
Param {
name: "values[0]".to_string(),
value: "0".to_string(),
},
Param {
name: "values[1]".to_string(),
value: "1".to_string(),
},
],
multipart: vec![],
cookies: vec![],
Expand All @@ -288,7 +296,7 @@ fn test_form_params() {
};
assert_eq!(
client.curl_command_line(&request_spec),
"curl 'http://localhost:8000/form-params' --data 'param1=value1' --data 'param2=' --data 'param3=a%3Db' --data 'param4=a%253db'".to_string()
"curl 'http://localhost:8000/form-params' --data 'param1=value1' --data 'param2=' --data 'param3=a%3Db' --data 'param4=a%253db' --data 'values[0]=0' --data 'values[1]=1'".to_string()
);

let (request, response) = client.execute(&request_spec).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion packages/hurl_core/src/format/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl Htmlable for KeyValue {
add_line_terminators(&mut buffer, self.line_terminators.clone());
buffer.push_str("<span class=\"line\">");
buffer.push_str(self.space0.to_html().as_str());
buffer.push_str(format!("<span class=\"string\">{}</span>", self.key.value).as_str());
buffer.push_str(format!("<span class=\"string\">{}</span>", self.key.encoded).as_str());
buffer.push_str(self.space1.to_html().as_str());
buffer.push_str("<span>:</span>");
buffer.push_str(self.space2.to_html().as_str());
Expand Down
62 changes: 59 additions & 3 deletions packages/hurl_core/src/parser/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ pub fn unquoted_string_key(reader: &mut Reader) -> ParseResult<'static, EncodedS
match reader.read() {
None => break,
Some(c) => {
if c.is_alphanumeric() || c == '_' || c == '-' || c == '.' {
if c.is_alphanumeric()
|| c == '_'
|| c == '-'
|| c == '.'
|| c == '['
|| c == ']'
{
value.push(c);
encoded.push_str(reader.from(save.cursor).as_str())
} else {
Expand All @@ -113,8 +119,8 @@ pub fn unquoted_string_key(reader: &mut Reader) -> ParseResult<'static, EncodedS
}
}

// check nonempty
if value.is_empty() {
// check nonempty/ starts with [
if value.is_empty() || encoded.starts_with('[') {
return Err(Error {
pos: start,
recoverable: true,
Expand Down Expand Up @@ -446,6 +452,56 @@ mod tests {
assert_eq!(reader.state.cursor, 15);
}

#[test]
fn test_unquoted_key_with_square_bracket() {
let mut reader = Reader::init("values\\u{5b}0\\u{5d} :");
assert_eq!(
unquoted_string_key(&mut reader).unwrap(),
EncodedString {
value: "values[0]".to_string(),
encoded: "values\\u{5b}0\\u{5d}".to_string(),
quotes: false,
source_info: SourceInfo::init(1, 1, 1, 20),
}
);
assert_eq!(reader.state.cursor, 19);

let mut reader = Reader::init("values[0] :");
assert_eq!(
unquoted_string_key(&mut reader).unwrap(),
EncodedString {
value: "values[0]".to_string(),
encoded: "values[0]".to_string(),
quotes: false,
source_info: SourceInfo::init(1, 1, 1, 10),
}
);
assert_eq!(reader.state.cursor, 9);
}

#[test]
fn test_unquoted_keys_ignore_start_square_bracket() {
let mut reader = Reader::init("[0]:");
let error = unquoted_string_key(&mut reader).err().unwrap();
assert!(error.recoverable);
assert_eq!(reader.state.cursor, 3);
}

#[test]
fn test_unquoted_keys_accept_start_escape_square_bracket() {
let mut reader = Reader::init("\\u{5b}0\\u{5d}");
assert_eq!(
unquoted_string_key(&mut reader).unwrap(),
EncodedString {
value: "[0]".to_string(),
encoded: "\\u{5b}0\\u{5d}".to_string(),
quotes: false,
source_info: SourceInfo::init(1, 1, 1, 14),
}
);
assert_eq!(reader.state.cursor, 13);
}

#[test]
fn test_unquoted_key_error() {
let mut reader = Reader::init("");
Expand Down