Use Template type for cookie value

This commit is contained in:
Fabrice Reix 2022-06-12 18:51:26 +00:00 committed by jcamiel
parent 1e3c16192f
commit e239891ffc
14 changed files with 45 additions and 77 deletions

View File

@ -1,6 +1,6 @@
curl 'http://localhost:8000/cookies/set-request-cookie1-valueA' --cookie 'cookie1=valueA'
curl 'http://localhost:8000/cookies/assert-that-cookie1-is-not-in-session'
curl 'http://localhost:8000/cookies/set-multiple-request-cookies' --cookie 'user1=Bob; user2=Bill'
curl 'http://localhost:8000/cookies/set-multiple-request-cookies' --cookie 'user1=Bob; user2=Bill; user3=Bruce'
curl 'http://localhost:8000/cookies/set-session-cookie2-valueA'
curl 'http://localhost:8000/cookies/assert-that-cookie2-is-valueA' --cookie 'cookie2=valueA'
curl 'http://localhost:8000/cookies/assert-that-cookie2-is-valueA-and-valueB' --cookie 'cookie2=valueB; cookie2=valueA'

View File

@ -2,7 +2,7 @@
<span class="line"></span>
<span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/cookies/set-request-cookie1-valueA</span></span>
<span class="line section-header">[Cookies]</span>
<span class="line"><span class="name">cookie1</span><span>:</span> <span class="cookie-value">valueA</span></span>
<span class="line"><span class="name">cookie1</span><span>:</span> valueA</span>
</span><span class="response"><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"># The cookie is not added in the cookie storage</span>
@ -11,8 +11,9 @@
</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/cookies/set-multiple-request-cookies</span></span>
<span class="line section-header">[Cookies]</span>
<span class="line"><span class="name">user1</span><span>:</span> <span class="cookie-value">Bob</span></span>
<span class="line"><span class="name">user2</span><span>:</span> <span class="cookie-value">Bill</span></span>
<span class="line"><span class="name">user1</span><span>:</span> Bob</span>
<span class="line"><span class="name">user2</span><span>:</span> Bill</span>
<span class="line"><span class="name">user3</span><span>:</span> {{name}}</span>
</span><span class="response"><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>
@ -28,7 +29,7 @@
</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/cookies/assert-that-cookie2-is-valueA-and-valueB</span></span>
<span class="line section-header">[Cookies]</span>
<span class="line"><span class="name">cookie2</span><span>:</span> <span class="cookie-value">valueB</span></span>
<span class="line"><span class="name">cookie2</span><span>:</span> valueB</span>
</span><span class="response"><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>

View File

@ -13,6 +13,7 @@ GET http://localhost:8000/cookies/set-multiple-request-cookies
[Cookies]
user1: Bob
user2: Bill
user3: {{name}}
HTTP/1.0 200

View File

@ -1 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/cookies/set-request-cookie1-valueA","cookies":[{"name":"cookie1","value":"valueA"}]},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/assert-that-cookie1-is-not-in-session"},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/set-multiple-request-cookies","cookies":[{"name":"user1","value":"Bob"},{"name":"user2","value":"Bill"}]},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/set-session-cookie2-valueA"},"response":{"version":"HTTP/1.0","status":200,"asserts":[{"query":{"type":"cookie","expr":"cookie2"},"predicate":{"type":"equal","value":"valueA"}}]}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/assert-that-cookie2-is-valueA"},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/assert-that-cookie2-is-valueA-and-valueB","cookies":[{"name":"cookie2","value":"valueB"}]},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/delete-cookie2"},"response":{"version":"HTTP/1.0","status":200,"asserts":[{"query":{"type":"cookie","expr":"cookie2"},"predicate":{"type":"equal","value":""}},{"query":{"type":"cookie","expr":"cookie2[Max-Age]"},"predicate":{"type":"equal","value":0}}]}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/assert-that-cookie2-is-not-in-session"},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/set"},"response":{"version":"HTTP/1.0","status":200,"headers":[{"name":"Set-Cookie","value":"LSID=DQAAAKEaem_vYg; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Secure; HttpOnly; Path=/accounts"},{"name":"Set-Cookie","value":"HSID=AYQEVnDKrdst; Domain=.localhost; Expires=Wed, 13 Jan 2021 22:23:01 GMT; HttpOnly; Path=/"},{"name":"Set-Cookie","value":"SSID=Ap4PGTEq; Domain=.localhost; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Secure; HttpOnly; Path=/"}],"asserts":[{"query":{"type":"header","name":"Set-Cookie","subquery":{"type":"count"}},"predicate":{"type":"equal","value":3}},{"query":{"type":"cookie","expr":"LSID"},"predicate":{"type":"equal","value":"DQAAAKEaem_vYg"}},{"query":{"type":"cookie","expr":"LSID[Value]"},"predicate":{"type":"equal","value":"DQAAAKEaem_vYg"}},{"query":{"type":"cookie","expr":"LSID[Expires]"},"predicate":{"type":"exist"}},{"query":{"type":"cookie","expr":"LSID[Expires]"},"predicate":{"type":"equal","value":"Wed, 13 Jan 2021 22:23:01 GMT"}},{"query":{"type":"cookie","expr":"LSID[Max-Age]"},"predicate":{"not":true,"type":"exist"}},{"query":{"type":"cookie","expr":"LSID[Domain]"},"predicate":{"not":true,"type":"exist"}},{"query":{"type":"cookie","expr":"LSID[Path]"},"predicate":{"type":"equal","value":"/accounts"}},{"query":{"type":"cookie","expr":"LSID[Secure]"},"predicate":{"type":"exist"}},{"query":{"type":"cookie","expr":"LSID[HttpOnly]"},"predicate":{"type":"exist"}},{"query":{"type":"cookie","expr":"LSID[SameSite]"},"predicate":{"not":true,"type":"exist"}}]}}]}
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/cookies/set-request-cookie1-valueA","cookies":[{"name":"cookie1","value":"valueA"}]},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/assert-that-cookie1-is-not-in-session"},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/set-multiple-request-cookies","cookies":[{"name":"user1","value":"Bob"},{"name":"user2","value":"Bill"},{"name":"user3","value":"{{name}}"}]},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/set-session-cookie2-valueA"},"response":{"version":"HTTP/1.0","status":200,"asserts":[{"query":{"type":"cookie","expr":"cookie2"},"predicate":{"type":"equal","value":"valueA"}}]}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/assert-that-cookie2-is-valueA"},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/assert-that-cookie2-is-valueA-and-valueB","cookies":[{"name":"cookie2","value":"valueB"}]},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/delete-cookie2"},"response":{"version":"HTTP/1.0","status":200,"asserts":[{"query":{"type":"cookie","expr":"cookie2"},"predicate":{"type":"equal","value":""}},{"query":{"type":"cookie","expr":"cookie2[Max-Age]"},"predicate":{"type":"equal","value":0}}]}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/assert-that-cookie2-is-not-in-session"},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/cookies/set"},"response":{"version":"HTTP/1.0","status":200,"headers":[{"name":"Set-Cookie","value":"LSID=DQAAAKEaem_vYg; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Secure; HttpOnly; Path=/accounts"},{"name":"Set-Cookie","value":"HSID=AYQEVnDKrdst; Domain=.localhost; Expires=Wed, 13 Jan 2021 22:23:01 GMT; HttpOnly; Path=/"},{"name":"Set-Cookie","value":"SSID=Ap4PGTEq; Domain=.localhost; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Secure; HttpOnly; Path=/"}],"asserts":[{"query":{"type":"header","name":"Set-Cookie","subquery":{"type":"count"}},"predicate":{"type":"equal","value":3}},{"query":{"type":"cookie","expr":"LSID"},"predicate":{"type":"equal","value":"DQAAAKEaem_vYg"}},{"query":{"type":"cookie","expr":"LSID[Value]"},"predicate":{"type":"equal","value":"DQAAAKEaem_vYg"}},{"query":{"type":"cookie","expr":"LSID[Expires]"},"predicate":{"type":"exist"}},{"query":{"type":"cookie","expr":"LSID[Expires]"},"predicate":{"type":"equal","value":"Wed, 13 Jan 2021 22:23:01 GMT"}},{"query":{"type":"cookie","expr":"LSID[Max-Age]"},"predicate":{"not":true,"type":"exist"}},{"query":{"type":"cookie","expr":"LSID[Domain]"},"predicate":{"not":true,"type":"exist"}},{"query":{"type":"cookie","expr":"LSID[Path]"},"predicate":{"type":"equal","value":"/accounts"}},{"query":{"type":"cookie","expr":"LSID[Secure]"},"predicate":{"type":"exist"}},{"query":{"type":"cookie","expr":"LSID[HttpOnly]"},"predicate":{"type":"exist"}},{"query":{"type":"cookie","expr":"LSID[SameSite]"},"predicate":{"not":true,"type":"exist"}}]}}]}

View File

@ -0,0 +1,3 @@
--variable
name=Bruce

View File

@ -13,7 +13,8 @@ def set_request_cookie1_value1():
def set_multiple_request_cookies():
assert request.cookies["user1"] == "Bob"
assert request.cookies["user2"] == "Bill"
assert request.headers["Cookie"] == "user1=Bob; user2=Bill"
assert request.cookies["user3"] == "Bruce"
assert request.headers["Cookie"] == "user1=Bob; user2=Bill; user3=Bruce"
return ""

View File

@ -109,7 +109,7 @@ fn log_request(request: Request) {
}
SectionValue::Cookies(cookies) => {
for cookie in cookies {
eprintln!("\r{}: {}", cookie.name.value, cookie.value.value);
eprintln!("\r{}: {}", cookie.name.value, cookie.value);
}
}
_ => {}

View File

@ -79,9 +79,10 @@ pub fn eval_request(
let mut cookies = vec![];
for cookie in request.clone().cookies() {
let value = eval_template(&cookie.value, variables)?;
let cookie = http::RequestCookie {
name: cookie.clone().name.value,
value: cookie.clone().value.value,
value,
};
cookies.push(cookie);
}

View File

@ -884,19 +884,23 @@ fn test_multiple_request_cookies() {
name: "user2".to_string(),
value: "Bill".to_string(),
},
RequestCookie {
name: "user3".to_string(),
value: "Bruce".to_string(),
},
],
body: Body::Binary(vec![]),
content_type: None,
};
assert_eq!(
client.curl_command_line(&request_spec),
"curl 'http://localhost:8000/cookies/set-multiple-request-cookies' --cookie 'user1=Bob; user2=Bill'".to_string()
"curl 'http://localhost:8000/cookies/set-multiple-request-cookies' --cookie 'user1=Bob; user2=Bill; user3=Bruce'".to_string()
);
let (request, response) = client.execute(&request_spec).unwrap();
assert!(request.headers.contains(&Header {
name: "Cookie".to_string(),
value: "user1=Bob; user2=Bill".to_string(),
value: "user1=Bob; user2=Bill; user3=Bruce".to_string(),
}));
assert_eq!(response.status, 200);
assert!(response.body.is_empty());

View File

@ -230,15 +230,10 @@ pub struct Cookie {
pub name: EncodedString,
pub space1: Whitespace,
pub space2: Whitespace,
pub value: CookieValue,
pub value: Template,
pub line_terminator0: LineTerminator,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CookieValue {
pub value: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct KeyValue {
pub line_terminators: Vec<LineTerminator>,

View File

@ -298,15 +298,6 @@ impl Htmlable for Cookie {
}
}
impl Htmlable for CookieValue {
fn to_html(&self) -> String {
format!(
"<span class=\"cookie-value\">{}</span>",
self.value.as_str()
)
}
}
impl Htmlable for Capture {
fn to_html(&self) -> String {
let mut buffer = String::from("");

View File

@ -158,7 +158,7 @@ fn cookie(reader: &mut Reader) -> ParseResult<'static, Cookie> {
let space1 = zero_or_more_spaces(reader)?;
recover(|p1| literal(":", p1), reader)?;
let space2 = zero_or_more_spaces(reader)?;
let value = cookie_value(reader);
let value = unquoted_template(reader)?;
let line_terminator0 = line_terminator(reader)?;
Ok(Cookie {
line_terminators,
@ -171,23 +171,6 @@ fn cookie(reader: &mut Reader) -> ParseResult<'static, Cookie> {
})
}
///
/// The cookie-value must not be wrapped within double-quotes
/// This is optional in the spec, if you really want to include these optional quotes, you must use directly the header Cookie
///
fn cookie_value(reader: &mut Reader) -> CookieValue {
//let start = reader.state.clone();
let value = reader.read_while(|c| {
c.is_ascii_alphanumeric()
|| vec![
'!', '#', '$', '%', '&', '\'', '(', ')', '*', '+', '-', '.', '/', ':', '<', '=',
'>', '?', '@', '[', ']', '^', '_', '`', '~', '|', '"', ';', ',',
]
.contains(c)
});
CookieValue { value }
}
fn multipart_param(reader: &mut Reader) -> ParseResult<'static, MultipartParam> {
let save = reader.state.clone();
match file_param(reader) {
@ -488,38 +471,35 @@ mod tests {
let mut reader = Reader::init("Foo: Bar");
let c = cookie(&mut reader).unwrap();
assert_eq!(c.name.value, String::from("Foo"));
assert_eq!(c.value.value, String::from("Bar"));
assert_eq!(
c.value,
Template {
quotes: false,
elements: vec![TemplateElement::String {
value: "Bar".to_string(),
encoded: "Bar".to_string()
}],
source_info: SourceInfo::init(1, 6, 1, 9)
}
);
}
#[test]
fn test_cookie_error() {
let mut reader = Reader::init("Foo: {Bar}");
let mut reader = Reader::init("Foo: {{Bar");
let error = cookie(&mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 6 });
assert_eq!(
error.pos,
Pos {
line: 1,
column: 11
}
);
assert!(!error.recoverable);
assert_eq!(
error.inner,
ParseError::Expecting {
value: "line_terminator".to_string()
}
);
}
#[test]
fn test_cookie_value() {
let mut reader = Reader::init("Bar");
assert_eq!(
cookie_value(&mut reader),
CookieValue {
value: String::from("Bar")
}
);
let mut reader = Reader::init("\"Bar\"");
assert_eq!(
cookie_value(&mut reader),
CookieValue {
value: String::from("\"Bar\"")
value: "}}".to_string()
}
);
}

View File

@ -243,10 +243,7 @@ impl ToJson for Cookie {
fn to_json(&self) -> JValue {
let attributes = vec![
("name".to_string(), JValue::String(self.name.value.clone())),
(
"value".to_string(),
JValue::String(self.value.value.clone()),
),
("value".to_string(), JValue::String(self.value.to_string())),
];
JValue::Object(attributes)
}

View File

@ -390,12 +390,6 @@ impl Tokenizable for Cookie {
}
}
impl Tokenizable for CookieValue {
fn tokenize(&self) -> Vec<Token> {
vec![Token::Value(self.clone().value)]
}
}
impl Tokenizable for Capture {
fn tokenize(&self) -> Vec<Token> {
let mut tokens: Vec<Token> = vec![];