Lines
97.79 %
Functions
100 %
Branches
/*
* Hurl (https://hurl.dev)
* Copyright (C) 2024 Orange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::ast::*;
use crate::combinator::{optional, recover, zero_or_more};
use crate::parser::error::*;
use crate::parser::filter::filters;
use crate::parser::predicate::predicate;
use crate::parser::primitives::*;
use crate::parser::query::query;
use crate::parser::string::*;
use crate::parser::{filename, key_string, option, ParseResult};
use crate::reader::{Pos, Reader};
pub fn request_sections(reader: &mut Reader) -> ParseResult<Vec<Section>> {
let sections = zero_or_more(request_section, reader)?;
Ok(sections)
}
pub fn response_sections(reader: &mut Reader) -> ParseResult<Vec<Section>> {
let sections = zero_or_more(response_section, reader)?;
fn request_section(reader: &mut Reader) -> ParseResult<Section> {
let line_terminators = optional_line_terminators(reader)?;
let space0 = zero_or_more_spaces(reader)?;
let start = reader.cursor();
let name = section_name(reader)?;
let source_info = SourceInfo::new(start.pos, reader.cursor().pos);
let line_terminator0 = line_terminator(reader)?;
let value = match name.as_str() {
"Query" => section_value_query_params(reader, true)?,
"QueryStringParams" => section_value_query_params(reader, false)?,
"BasicAuth" => section_value_basic_auth(reader)?,
"Form" => section_value_form_params(reader, true)?,
"FormParams" => section_value_form_params(reader, false)?,
"Multipart" => section_value_multipart_form_data(reader, true)?,
"MultipartFormData" => section_value_multipart_form_data(reader, false)?,
"Cookies" => section_value_cookies(reader)?,
"Options" => section_value_options(reader)?,
_ => {
let kind = ParseErrorKind::RequestSectionName { name: name.clone() };
let pos = Pos::new(start.pos.line, start.pos.column + 1);
return Err(ParseError::new(pos, false, kind));
};
Ok(Section {
line_terminators,
space0,
line_terminator0,
value,
source_info,
})
fn response_section(reader: &mut Reader) -> ParseResult<Section> {
let end = reader.cursor();
let source_info = SourceInfo::new(start.pos, end.pos);
"Captures" => section_value_captures(reader)?,
"Asserts" => section_value_asserts(reader)?,
let kind = ParseErrorKind::ResponseSectionName { name: name.clone() };
fn section_name(reader: &mut Reader) -> ParseResult<String> {
let pos = reader.cursor().pos;
try_literal("[", reader)?;
let name = reader.read_while(|c| c.is_alphanumeric());
if name.is_empty() {
// Could be the empty json array for the body
let kind = ParseErrorKind::Expecting {
value: "a valid section name".to_string(),
return Err(ParseError::new(pos, true, kind));
try_literal("]", reader)?;
Ok(name)
fn section_value_query_params(reader: &mut Reader, short: bool) -> ParseResult<SectionValue> {
let items = zero_or_more(key_value, reader)?;
Ok(SectionValue::QueryParams(items, short))
fn section_value_basic_auth(reader: &mut Reader) -> ParseResult<SectionValue> {
let v = optional(key_value, reader)?;
Ok(SectionValue::BasicAuth(v))
fn section_value_form_params(reader: &mut Reader, short: bool) -> ParseResult<SectionValue> {
Ok(SectionValue::FormParams(items, short))
fn section_value_multipart_form_data(
reader: &mut Reader,
short: bool,
) -> ParseResult<SectionValue> {
let items = zero_or_more(multipart_param, reader)?;
Ok(SectionValue::MultipartFormData(items, short))
fn section_value_cookies(reader: &mut Reader) -> ParseResult<SectionValue> {
let items = zero_or_more(cookie, reader)?;
Ok(SectionValue::Cookies(items))
fn section_value_captures(reader: &mut Reader) -> ParseResult<SectionValue> {
let items = zero_or_more(capture, reader)?;
Ok(SectionValue::Captures(items))
fn section_value_asserts(reader: &mut Reader) -> ParseResult<SectionValue> {
let asserts = zero_or_more(assert, reader)?;
Ok(SectionValue::Asserts(asserts))
fn section_value_options(reader: &mut Reader) -> ParseResult<SectionValue> {
let options = zero_or_more(option::parse, reader)?;
Ok(SectionValue::Options(options))
fn cookie(reader: &mut Reader) -> ParseResult<Cookie> {
// let start = reader.state.clone();
let name = recover(key_string::parse, reader)?;
let space1 = zero_or_more_spaces(reader)?;
recover(|p1| literal(":", p1), reader)?;
let space2 = zero_or_more_spaces(reader)?;
let value = unquoted_template(reader)?;
Ok(Cookie {
name,
space1,
space2,
fn multipart_param(reader: &mut Reader) -> ParseResult<MultipartParam> {
let save = reader.cursor();
match file_param(reader) {
Ok(f) => Ok(MultipartParam::FileParam(f)),
Err(e) => {
if e.recoverable {
reader.seek(save);
let param = key_value(reader)?;
Ok(MultipartParam::Param(param))
} else {
Err(e)
fn file_param(reader: &mut Reader) -> ParseResult<FileParam> {
let key = recover(key_string::parse, reader)?;
recover(|reader1| literal(":", reader1), reader)?;
let value = file_value(reader)?;
Ok(FileParam {
key,
fn file_value(reader: &mut Reader) -> ParseResult<FileValue> {
try_literal("file,", reader)?;
let f = filename::parse(reader)?;
literal(";", reader)?;
let (space2, content_type) = match line_terminator(reader) {
Ok(_) => {
let space2 = Whitespace {
value: String::new(),
source_info: SourceInfo {
start: save.pos,
end: save.pos,
},
(space2, None)
Err(_) => {
let content_type = file_content_type(reader)?;
(space2, Some(content_type))
Ok(FileValue {
filename: f,
content_type,
fn file_content_type(reader: &mut Reader) -> ParseResult<String> {
let mut buf = String::new();
let mut spaces = String::new();
let mut save = reader.cursor();
while let Some(c) = reader.read() {
if c.is_alphanumeric() || c == '/' || c == ';' || c == '=' || c == '-' {
buf.push_str(spaces.as_str());
spaces = String::new();
buf.push(c);
save = reader.cursor();
} else if c == ' ' {
spaces.push(' ');
break;
if buf.is_empty() {
return Err(ParseError::new(
start.pos,
false,
ParseErrorKind::FileContentType,
));
Ok(buf)
fn capture(reader: &mut Reader) -> ParseResult<Capture> {
let q = query(reader)?;
let filters = filters(reader)?;
Ok(Capture {
query: q,
filters,
fn assert(reader: &mut Reader) -> ParseResult<Assert> {
let query0 = query(reader)?;
let space1 = one_or_more_spaces(reader)?;
let predicate0 = predicate(reader)?;
Ok(Assert {
query: query0,
predicate: predicate0,
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_section_name() {
let mut reader = Reader::new("[SectionA]");
assert_eq!(section_name(&mut reader).unwrap(), String::from("SectionA"));
let mut reader = Reader::new("[]");
assert!(section_name(&mut reader).err().unwrap().recoverable);
fn test_asserts_section() {
let mut reader = Reader::new("[Asserts]\nheader \"Location\" == \"https://google.fr\"\n");
assert_eq!(
response_section(&mut reader).unwrap(),
Section {
line_terminators: vec![],
space0: Whitespace {
source_info: SourceInfo::new(Pos::new(1, 1), Pos::new(1, 1)),
line_terminator0: LineTerminator {
source_info: SourceInfo::new(Pos::new(1, 10), Pos::new(1, 10)),
comment: None,
newline: Whitespace {
value: String::from("\n"),
source_info: SourceInfo::new(Pos::new(1, 10), Pos::new(2, 1)),
value: SectionValue::Asserts(vec![Assert {
source_info: SourceInfo::new(Pos::new(2, 1), Pos::new(2, 1)),
query: Query {
source_info: SourceInfo::new(Pos::new(2, 1), Pos::new(2, 18)),
value: QueryValue::Header {
value: String::from(" "),
source_info: SourceInfo::new(Pos::new(2, 7), Pos::new(2, 8)),
name: Template {
delimiter: Some('"'),
elements: vec![TemplateElement::String {
value: "Location".to_string(),
encoded: "Location".to_string(),
}],
source_info: SourceInfo::new(Pos::new(2, 8), Pos::new(2, 18)),
filters: vec![],
space1: Whitespace {
source_info: SourceInfo::new(Pos::new(2, 18), Pos::new(2, 19)),
predicate: Predicate {
not: false,
source_info: SourceInfo::new(Pos::new(2, 19), Pos::new(2, 19)),
predicate_func: PredicateFunc {
source_info: SourceInfo::new(Pos::new(2, 19), Pos::new(2, 41)),
value: PredicateFuncValue::Equal {
source_info: SourceInfo::new(Pos::new(2, 21), Pos::new(2, 22)),
value: PredicateValue::String(Template {
value: "https://google.fr".to_string(),
encoded: "https://google.fr".to_string(),
source_info: SourceInfo::new(Pos::new(2, 22), Pos::new(2, 41)),
}),
operator: true,
source_info: SourceInfo::new(Pos::new(2, 41), Pos::new(2, 41)),
source_info: SourceInfo::new(Pos::new(2, 41), Pos::new(3, 1)),
}]),
source_info: SourceInfo::new(Pos::new(1, 1), Pos::new(1, 10)),
);
fn test_asserts_section_error() {
let mut reader = Reader::new("x[Assertsx]\nheader Location == \"https://google.fr\"\n");
let error = response_section(&mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 1 });
error.kind,
ParseErrorKind::Expecting {
value: String::from("[")
assert!(error.recoverable);
let mut reader = Reader::new("[Assertsx]\nheader Location == \"https://google.fr\"\n");
assert_eq!(error.pos, Pos { line: 1, column: 2 });
ParseErrorKind::ResponseSectionName {
name: String::from("Assertsx")
assert!(!error.recoverable);
fn test_cookie() {
let mut reader = Reader::new("Foo: Bar");
let c = cookie(&mut reader).unwrap();
assert_eq!(c.name.to_string(), String::from("Foo"));
c.value,
Template {
delimiter: None,
value: "Bar".to_string(),
encoded: "Bar".to_string(),
source_info: SourceInfo::new(Pos::new(1, 6), Pos::new(1, 9)),
fn test_cookie_error() {
let mut reader = Reader::new("Foo: {{Bar");
let error = cookie(&mut reader).err().unwrap();
error.pos,
Pos {
line: 1,
column: 11,
value: "}}".to_string()
fn test_file_value() {
let mut reader = Reader::new("file,hello.txt;");
file_value(&mut reader).unwrap(),
FileValue {
source_info: SourceInfo::new(Pos::new(1, 6), Pos::new(1, 6)),
filename: Template {
value: "hello.txt".to_string(),
encoded: "hello.txt".to_string(),
source_info: SourceInfo::new(Pos::new(1, 6), Pos::new(1, 15)),
source_info: SourceInfo::new(Pos::new(1, 15), Pos::new(1, 15)),
space2: Whitespace {
source_info: SourceInfo::new(Pos::new(1, 16), Pos::new(1, 16)),
content_type: None,
let mut reader = Reader::new("file,hello.txt; text/html");
value: " ".to_string(),
source_info: SourceInfo::new(Pos::new(1, 16), Pos::new(1, 17)),
content_type: Some("text/html".to_string()),
fn test_file_content_type() {
let mut reader = Reader::new("text/html");
file_content_type(&mut reader).unwrap(),
"text/html".to_string()
assert_eq!(reader.cursor().index, 9);
let mut reader = Reader::new("text/plain; charset=us-ascii");
"text/plain; charset=us-ascii".to_string()
assert_eq!(reader.cursor().index, 28);
let mut reader = Reader::new("text/html # comment");
fn test_capture() {
let mut reader = Reader::new("url: header \"Location\"");
let capture0 = capture(&mut reader).unwrap();
capture0.name,
value: "url".to_string(),
encoded: "url".to_string(),
source_info: SourceInfo::new(Pos::new(1, 1), Pos::new(1, 4)),
capture0.query,
Query {
source_info: SourceInfo::new(Pos::new(1, 6), Pos::new(1, 23)),
source_info: SourceInfo::new(Pos::new(1, 12), Pos::new(1, 13)),
source_info: SourceInfo::new(Pos::new(1, 13), Pos::new(1, 23)),
fn test_capture_with_filter() {
let mut reader = Reader::new("token: header \"Location\" regex \"token=(.*)\"");
source_info: SourceInfo::new(Pos::new(1, 8), Pos::new(1, 25)),
source_info: SourceInfo::new(Pos::new(1, 14), Pos::new(1, 15)),
source_info: SourceInfo::new(Pos::new(1, 15), Pos::new(1, 25)),
assert_eq!(reader.cursor().index, 43);
fn test_capture_with_filter_error() {
let mut reader = Reader::new("token: header \"Location\" regex ");
let error = capture(&mut reader).err().unwrap();
column: 32,
value: "\" or /".to_string()
let mut reader = Reader::new("token: header \"Location\" xxx");
column: 26,
value: "line_terminator".to_string()
fn test_assert() {
let mut reader = Reader::new("header \"Location\" == \"https://google.fr\"");
let assert0 = assert(&mut reader).unwrap();
assert0.query,
source_info: SourceInfo::new(Pos::new(1, 1), Pos::new(1, 18)),
source_info: SourceInfo::new(Pos::new(1, 7), Pos::new(1, 8)),
source_info: SourceInfo::new(Pos::new(1, 8), Pos::new(1, 18)),
fn test_assert_jsonpath() {
let mut reader = Reader::new("jsonpath \"$.errors\" == 5");
assert(&mut reader).unwrap().predicate,
Predicate {
source_info: SourceInfo::new(Pos::new(1, 21), Pos::new(1, 21)),
source_info: SourceInfo::new(Pos::new(1, 21), Pos::new(1, 25)),
source_info: SourceInfo::new(Pos::new(1, 23), Pos::new(1, 24)),
value: PredicateValue::Number(Number::Integer(5)),
fn test_basicauth_section() {
let mut reader = Reader::new("[BasicAuth]\nuser:password\n\nHTTP 200\n");
request_section(&mut reader).unwrap(),
source_info: SourceInfo::new(Pos::new(1, 12), Pos::new(1, 12)),
source_info: SourceInfo::new(Pos::new(1, 12), Pos::new(2, 1)),
value: SectionValue::BasicAuth(Some(KeyValue {
source_info: SourceInfo::new(Pos::new(2, 1), Pos::new(2, 1))
key: Template {
value: "user".to_string(),
encoded: "user".to_string()
source_info: SourceInfo::new(Pos::new(2, 1), Pos::new(2, 5)),
source_info: SourceInfo::new(Pos::new(2, 5), Pos::new(2, 5))
source_info: SourceInfo::new(Pos::new(2, 6), Pos::new(2, 6))
value: Template {
value: "password".to_string(),
encoded: "password".to_string()
source_info: SourceInfo::new(Pos::new(2, 6), Pos::new(2, 14)),
source_info: SourceInfo::new(Pos::new(2, 14), Pos::new(2, 14))
value: "\n".to_string(),
source_info: SourceInfo::new(Pos::new(2, 14), Pos::new(3, 1))
})),
source_info: SourceInfo::new(Pos::new(1, 1), Pos::new(1, 12)),
assert_eq!(reader.cursor().pos, Pos { line: 3, column: 1 });
let mut reader = Reader::new("[BasicAuth]\nHTTP 200\n");
value: SectionValue::BasicAuth(None),
assert_eq!(reader.cursor().pos, Pos { line: 2, column: 1 });