Skip to content

Commit

Permalink
chore: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
junkurihara committed Feb 4, 2024
1 parent 89d6768 commit b77fd4e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
7 changes: 5 additions & 2 deletions ext-hyper/src/hyper_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,12 @@ mod tests {
);

let headers = req.headers_mut();
headers.insert("signature-input", http::HeaderValue::from_static("sample input"));
headers.insert(
"signature-input",
http::HeaderValue::from_static(r##"sig1=("@method" "@authority")"##),
);
let component_id = HttpMessageComponentId::try_from("@signature-params").unwrap();
let component = extract_http_message_component_from_request(&req, &component_id).unwrap();
assert_eq!(component.to_string(), "\"@signature-params\": sample input");
assert_eq!(component.to_string(), "\"@signature-params\": (\"@method\" \"@input\")");
}
}
54 changes: 49 additions & 5 deletions lib/src/message_component/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,22 @@ impl TryFrom<(&str, &str)> for HttpMessageComponentId {
/// Http message component value
pub struct HttpMessageComponentValue {
/// inner value originally from http message header or derived from http message
inner: String,
inner: HttpMessageComponentValueInner,
}

impl From<&str> for HttpMessageComponentValue {
fn from(val: &str) -> Self {
Self { inner: val.to_string() }
Self {
inner: HttpMessageComponentValueInner::String(val.to_string()),
}
}
}

impl From<(&str, &str)> for HttpMessageComponentValue {
fn from((key, val): (&str, &str)) -> Self {
Self {
inner: HttpMessageComponentValueInner::KeyValue((key.to_string(), val.to_string())),
}
}
}

Expand All @@ -165,6 +175,39 @@ impl std::fmt::Display for HttpMessageComponentValue {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Http message component value inner, simple string or key-value pair
enum HttpMessageComponentValueInner {
String(String),
KeyValue((String, String)),
}

impl std::fmt::Display for HttpMessageComponentValueInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::String(val) => write!(f, "{}", val),
Self::KeyValue((key, val)) => write!(f, "{}={}", key, val),
}
}
}

impl HttpMessageComponentValue {
/// Get key if pair, otherwise None
pub fn key(&self) -> Option<&str> {
match &self.inner {
HttpMessageComponentValueInner::String(_) => None,
HttpMessageComponentValueInner::KeyValue((key, _)) => Some(key.as_ref()),
}
}
/// Get value
pub fn value(&self) -> &str {
match &self.inner {
HttpMessageComponentValueInner::String(val) => val.as_ref(),
HttpMessageComponentValueInner::KeyValue((_, val)) => val.as_ref(),
}
}
}

/* ---------------------------------------------------------------- */
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
/// Http message component identifier
Expand Down Expand Up @@ -368,7 +411,7 @@ mod tests {
} else {
assert!(!comp.id.params.0.is_empty());
}
assert_eq!(comp.value.inner, value);
assert_eq!(comp.value.inner.to_string(), value);
assert_eq!(comp.to_string(), format!("{}: {}", id, value));
}
}
Expand All @@ -382,7 +425,8 @@ mod tests {
comp.id.params.0.get(&HttpMessageComponentParam::Name("key".to_string())),
Some(&HttpMessageComponentParam::Name("key".to_string()))
);
assert_eq!(comp.value.inner, value);
assert_eq!(comp.value.inner.to_string(), value);
assert_eq!(comp.value.value(), value);
assert_eq!(comp.to_string(), format!("{}: {}", id, value));
}

Expand All @@ -402,7 +446,7 @@ mod tests {
} else {
assert!(!comp.id.params.0.is_empty());
}
assert_eq!(comp.value.inner, value);
assert_eq!(comp.value.inner.to_string(), value);
assert_eq!(comp.to_string(), format!("{}: {}", id, value));
}
}
Expand Down
6 changes: 5 additions & 1 deletion lib/src/message_component/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ fn build_derived_component(id: &HttpMessageComponentId, field_values: &[String])
.collect::<Vec<_>>();
kvs.join(", ")
}
super::DerivedComponentName::SignatureParams => field_values.join(", "),
super::DerivedComponentName::SignatureParams => {
let value = field_values[0].to_string();
// let s = value.trim().split_once('=');
value
}
};
let component = HttpMessageComponent {
id: id.clone(),
Expand Down
8 changes: 6 additions & 2 deletions lib/src/signature_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ impl SignatureBase {
/// This should not be exposed to user and not used directly.
/// TODO: Use wrapper functions generating SignatureBase from base HTTP request and Signer itself instead when newly generating signature
/// TODO: When verifying signature, use wrapper functions generating SignatureBase from HTTP request containing signature params itself instead.
pub fn try_new(component_lines: &Vec<HttpMessageComponent>, signature_params: &HttpSignatureParams) -> anyhow::Result<Self> {
pub fn try_new(
component_lines: &Vec<HttpMessageComponent>,
signature_params: &HttpSignatureParams,
signature_key: Option<&str>,
) -> anyhow::Result<Self> {
// check if the order of component lines is the same as the order of covered message component ids
if component_lines.len() != signature_params.covered_components.len() {
anyhow::bail!("The number of component lines is not the same as the number of covered message component ids");
Expand Down Expand Up @@ -73,7 +77,7 @@ mod test {
HttpMessageComponent::from_serialized_str("\"content-digest\": sha-256=:X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=:")
.unwrap(),
];
let signature_base = SignatureBase::try_new(&component_lines, &signature_params).unwrap();
let signature_base = SignatureBase::try_new(&component_lines, &signature_params, None).unwrap();
let test_string = r##""@method": GET
"@path": /
"date": Tue, 07 Jun 2014 20:51:35 GMT
Expand Down

0 comments on commit b77fd4e

Please sign in to comment.