Skip to content

Commit

Permalink
🐛 Newlines in request body (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
bayne authored Jan 17, 2020
1 parent 76db557 commit a26100f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,3 @@ serde = { version = "1.0", features = ["derive"] }
[dev-dependencies]
http-test-server = "1.0.0"
tempfile = "3.1.0"

#[dependencies.futures-preview]
#version = "0.3.0-alpha.18"
#features = ["compat", "io-compat"]
6 changes: 3 additions & 3 deletions src/parser/parser.pest
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ inline_script_string = { inline_script_string_character* }
inline_script_string_character = _{ !inline_script_end ~ !"{{" ~ ANY}
inline_script_end = _{ SP* ~ "}}" }

request_script = { request ~ (CRLF ~ request_body)? ~ (CRLF{2,} ~ request_handler)? ~ CRLF* ~ request_separator* }
request_body = ${ (!request_separator ~ !CRLF{2,} ~ (inline_script | ANY))+ }
request_script = { request ~ (CRLF ~ request_body)? ~ (CRLF+ ~ request_handler)? }
request_body = ${ (!(CRLF* ~ request_separator) ~ !(CRLF* ~ handler_script_start) ~ !(CRLF* ~ EOI) ~ (inline_script | ANY))+ }
request_handler = ${ !request_separator ~ handler_script }

request = _{ request_line ~ CRLF ~ (header_field ~ CRLF)* }
Expand All @@ -33,4 +33,4 @@ header_field = ${ field_name ~ ":" ~ SP* ~ field_value}
field_name = { token }
field_value = { (!CRLF ~ (inline_script | ANY))* }

file = { SOI ~ CRLF* ~ (request_script ~ CRLF*)* ~ EOI }
file = { SOI ~ CRLF* ~ (request_script ~ CRLF* ~ ((request_separator ~ CRLF*) | EOI))* ~ EOI }
72 changes: 59 additions & 13 deletions src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ Content-Type: {{ content_type }}
> {%
console.log('Success!');
%}
###"
%}"
);

let mut request_script_parts = request_script.into_inner();
Expand Down Expand Up @@ -88,8 +86,7 @@ Content-Type: {{ content_type }}
"\
GET http://example.com/{{url_param}}
Accept: */*
###"
"
);

let mut request_script_parts = request_script.into_inner();
Expand All @@ -112,6 +109,55 @@ fn test_min_file() {
assert!(file.is_ok());
}

#[test]
fn test_empty_body_with_handler() {
let test = "\
POST http://example.com HTTP/1.1
Accept: */*
> {%
console.log('cool');
%}
###
";

let file = ScriptParser::parse(Rule::file, test);
if let Err(e) = &file {
println!("{:?}", e);
}

assert!(file.is_ok());
}

#[test]
fn test_new_line_in_request_body_file() {
let test = "\
POST http://example.com HTTP/1.1
Accept: */*
{
\"test\": \"a\",
\"what\": [
]
}
> {%
console.log('cool');
%}
###
";

let file = ScriptParser::parse(Rule::file, test);
if let Err(e) = &file {
println!("{:?}", e);
}

assert!(file.is_ok());
}

#[test]
fn test_request_script() {
let test = "\
Expand Down Expand Up @@ -309,7 +355,7 @@ Accept: */*
selection: Selection {
filename: Path::new("").to_path_buf(),
start: Position { line: 4, col: 1 },
end: Position { line: 19, col: 4 },
end: Position { line: 17, col: 3 },
},
},
handler: Some(Handler {
Expand All @@ -323,7 +369,7 @@ Accept: */*
selection: Selection {
filename: Path::new("").to_path_buf(),
start: Position { line: 4, col: 1 },
end: Position { line: 19, col: 4 },
end: Position { line: 17, col: 3 },
},
},
RequestScript {
Expand Down Expand Up @@ -374,14 +420,14 @@ Accept: */*
selection: Selection {
filename: Path::new("").to_path_buf(),
start: Position { line: 22, col: 1 },
end: Position { line: 25, col: 1 },
end: Position { line: 24, col: 1 },
},
},
handler: None,
selection: Selection {
filename: Path::new("").to_path_buf(),
start: Position { line: 22, col: 1 },
end: Position { line: 25, col: 1 },
end: Position { line: 24, col: 1 },
},
},
],
Expand Down Expand Up @@ -435,7 +481,7 @@ Accept: */*
selection: Selection {
filename: Path::new("").to_path_buf(),
start: Position { line: 4, col: 1 },
end: Position { line: 19, col: 4 },
end: Position { line: 17, col: 3 },
},
},
handler: Some(Handler {
Expand All @@ -449,7 +495,7 @@ Accept: */*
selection: Selection {
filename: Path::new("").to_path_buf(),
start: Position { line: 4, col: 1 },
end: Position { line: 19, col: 4 },
end: Position { line: 17, col: 3 },
},
},
RequestScript {
Expand Down Expand Up @@ -481,14 +527,14 @@ Accept: */*
selection: Selection {
filename: Path::new("").to_path_buf(),
start: Position { line: 22, col: 1 },
end: Position { line: 25, col: 1 },
end: Position { line: 24, col: 1 },
},
},
handler: None,
selection: Selection {
filename: Path::new("").to_path_buf(),
start: Position { line: 22, col: 1 },
end: Position { line: 25, col: 1 },
end: Position { line: 24, col: 1 },
},
},
],
Expand Down

0 comments on commit a26100f

Please sign in to comment.