Fix crash with --json option when capturing 'HttpOnly' and 'Secure' cookie attribute

This commit is contained in:
Jean-Christophe Amiel 2024-06-04 15:51:39 +02:00
parent 11dd314b1d
commit 76ef2c98bc
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
5 changed files with 69 additions and 3 deletions

View File

@ -1,4 +1,5 @@
curl 'http://localhost:8000/captures' curl 'http://localhost:8000/captures'
curl 'http://localhost:8000/captures-check?param1=value1&param2=Bob' curl 'http://localhost:8000/captures-check?param1=value1&param2=Bob'
curl 'http://localhost:8000/captures-xml' curl 'http://localhost:8000/captures-xml'
curl 'http://localhost:8000/captures-cookie'
curl 'http://localhost:8000/captures-json' curl 'http://localhost:8000/captures-json'

View File

@ -31,6 +31,31 @@ a_node_set: xpath "//p"
variable "a_node_set" count == 2 variable "a_node_set" count == 2
GET http://localhost:8000/captures-cookie
HTTP 200
[Captures]
a_cookie_value_attr: cookie "foo"
a_cookie_path_attr: cookie "foo[Path]"
a_cookie_expires_attr: cookie "foo[Expires]"
a_cookie_domain_attr: cookie "foo[Domain]"
a_cookie_max_age_attr: cookie "foo[Max-Age]"
a_cookie_http_only_attr: cookie "foo[HttpOnly]"
[Asserts]
variable "a_cookie_value_attr" == "value1"
variable "a_cookie_value_attr" == {{a_cookie_value_attr}}
variable "a_cookie_path_attr" == "/bar"
variable "a_cookie_path_attr" == {{a_cookie_path_attr}}
variable "a_cookie_expires_attr" format "%Y-%m-%dT%H:%M:%S%.fZ" == "2078-01-13T22:23:01Z"
# FIXME: the next assert should pass
# variable "a_cookie_expires_attr" == {{a_cookie_expires_attr}}
variable "a_cookie_domain_attr" == "localhost"
variable "a_cookie_domain_attr" == {{a_cookie_domain_attr}}
variable "a_cookie_max_age_attr" == 2592000
variable "a_cookie_max_age_attr" == {{a_cookie_max_age_attr}}
variable "a_cookie_http_only_attr" exists
GET http://localhost:8000/captures-json GET http://localhost:8000/captures-json
HTTP 200 HTTP 200
[Captures] [Captures]

View File

@ -18,6 +18,25 @@ def captures_check():
return "" return ""
@app.route("/captures-cookie")
def captures_cookie():
resp = make_response()
resp.set_cookie(
"foo",
"value1",
domain="localhost",
path="/bar",
httponly=True,
# Can't use secure attributes here because secure cookie are not
# stored in cookie storage by curl for version < 7.79
# See <https://github.com/curl/curl/issues/6733>
# secure=True,
expires="Thu, 13 Jan 2078 22:23:01 GMT",
max_age=2592000,
)
return resp
@app.route("/captures-xml") @app.route("/captures-xml")
def captures_xml(): def captures_xml():
return ( return (

File diff suppressed because one or more lines are too long

View File

@ -44,7 +44,15 @@ impl Value {
serde_json::Value::Object(map) serde_json::Value::Object(map)
} }
Value::Nodeset(size) => { Value::Nodeset(size) => {
// TODO: explain why we do this for nodeset. // For nodeset, we don't have a "native" JSON representation to use as a serialized
// format. As a fallback, we serialize with a `type` field:
//
// ```json
// {
// "type": "nodeset",
// "size": 4,
// }
// ```
let mut map = serde_json::Map::new(); let mut map = serde_json::Map::new();
let size = *size as i64; let size = *size as i64;
map.insert( map.insert(
@ -60,12 +68,25 @@ impl Value {
} }
Value::Null => serde_json::Value::Null, Value::Null => serde_json::Value::Null,
Value::Regex(value) => serde_json::Value::String(value.to_string()), Value::Regex(value) => serde_json::Value::String(value.to_string()),
Value::Unit => todo!("how to serialize that in json?"), Value::Unit => {
// Like nodeset, we don't have a "native" JSON representation for the unit type,
// we use a general fallback with `type` field
let mut map = serde_json::Map::new();
map.insert(
"type".to_string(),
serde_json::Value::String("unit".to_string()),
);
serde_json::Value::Object(map)
}
} }
} }
} }
impl Number { impl Number {
/// Serializes a number to JSON.
///
/// Numbers that are representable in JSON use the number JSON type, while big number
/// will be serialized as string.
pub fn to_json(&self) -> serde_json::Value { pub fn to_json(&self) -> serde_json::Value {
match self { match self {
Number::Integer(v) => serde_json::Value::Number(serde_json::Number::from(*v)), Number::Integer(v) => serde_json::Value::Number(serde_json::Number::from(*v)),