Repository: Orange-OpenSource/hurl
File: packages/hurl/src/runner/query.rs (Rust)

Code before:
            CertificateAttributeName::StartDate => Value::Date(certificate.start_date),
            CertificateAttributeName::ExpireDate => Value::Date(certificate.expire_date),
            CertificateAttributeName::SerialNumber => {
                Value::String(certificate.serial_number.clone())
            }
        };
        Ok(Some(value))
    } else {
        Ok(None)
    }
}

/// Evaluates the ip address of the HTTP `response`.
fn eval_ip(response: &http::Response) -> QueryResult {
    Ok(Some(Value::String(response.ip_addr.to_string())))
}

/// Evaluates the redirects within a list of HTTP `responses`
fn eval_redirects(responses: &[&http::Response]) -> QueryResult {
    let mut values: Vec<Value> = responses
        .iter()
        .map(|r| Value::HttpResponse(HttpResponse::new(r.url.clone(), r.status)))
        .collect();
    values.pop();
    Ok(Some(Value::List(values)))
}

fn eval_cookie_attribute_name(
    cookie_attribute_name: CookieAttributeName,
    cookie: http::ResponseCookie,
) -> Option<Value> {
    match cookie_attribute_name {
        CookieAttributeName::Value(_) => Some(Value::String(cookie.value)),
        CookieAttributeName::Expires(_) => {
            if let Some(s) = cookie.expires() {
                match chrono::DateTime::parse_from_rfc2822(s.as_str()) {
                    Ok(v) => Some(Value::Date(v.with_timezone(&chrono::Utc))),
                    Err(_) => todo!(),
                }
            } else {
                None
            }
        }
        CookieAttributeName::MaxAge(_) => {
            cookie.max_age().map(|v| Value::Number(Number::Integer(v)))
        }
        CookieAttributeName::Domain(_) => cookie.domain().map(Value::String),
        CookieAttributeName::Path(_) => cookie.path().map(Value::String),
        CookieAttributeName::Secure(_) => {
            if cookie.has_secure() {
                Some(Value::Unit)

Review comment (suggestion):
Maybe we should add a comment here about why we pop the last values.
```
// `responses` is the chain list of response triggers by following redirection. We want to only keep 
// "redirected" HTTP response so we drop the last, not-redirected, response.


Code after:
            CertificateAttributeName::StartDate => Value::Date(certificate.start_date),
            CertificateAttributeName::ExpireDate => Value::Date(certificate.expire_date),
            CertificateAttributeName::SerialNumber => {
                Value::String(certificate.serial_number.clone())
            }
        };
        Ok(Some(value))
    } else {
        Ok(None)
    }
}

/// Evaluates the ip address of the HTTP `response`.
fn eval_ip(response: &http::Response) -> QueryResult {
    Ok(Some(Value::String(response.ip_addr.to_string())))
}

/// Evaluates the redirects within a list of HTTP `responses`
fn eval_redirects(responses: &[&http::Response]) -> QueryResult {
    let mut values: Vec<Value> = responses
        .iter()
        .map(|r| Value::HttpResponse(HttpResponse::new(r.url.clone(), r.status)))
        .collect();
    // `responses` is the chain list of response triggers by following redirection. We want to only keep
    // "redirected" HTTP response so we drop the last, not-redirected, response.
    values.pop();
    Ok(Some(Value::List(values)))
}

fn eval_cookie_attribute_name(
    cookie_attribute_name: CookieAttributeName,
    cookie: http::ResponseCookie,
) -> Option<Value> {
    match cookie_attribute_name {
        CookieAttributeName::Value(_) => Some(Value::String(cookie.value)),
        CookieAttributeName::Expires(_) => {
            if let Some(s) = cookie.expires() {
                match chrono::DateTime::parse_from_rfc2822(s.as_str()) {
                    Ok(v) => Some(Value::Date(v.with_timezone(&chrono::Utc))),
                    Err(_) => todo!(),
                }
            } else {
                None
            }
        }
        CookieAttributeName::MaxAge(_) => {
            cookie.max_age().map(|v| Value::Number(Number::Integer(v)))
        }
        CookieAttributeName::Domain(_) => cookie.domain().map(Value::String),
        CookieAttributeName::Path(_) => cookie.path().map(Value::String),
        CookieAttributeName::Secure(_) => {