Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 58 additions & 19 deletions crates/taurus-core/src/runtime/functions/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,20 @@ impl HttpAuthType {
fn from_value(input: &Value) -> Result<HttpAuthType, String> {
match input.kind.as_ref() {
Some(Kind::NullValue(_)) | None => Ok(HttpAuthType::None),
Some(Kind::StringValue(value)) => match value.as_str() {
"Bearer" => Ok(HttpAuthType::Bearer),
"Basic" => Ok(HttpAuthType::Basic),
"X-API-Key" => Ok(HttpAuthType::XApiKey),
"undefined" | "" => Ok(HttpAuthType::None),
custom => Ok(HttpAuthType::Custom(custom.to_string())),
},
Some(Kind::StringValue(value)) => {
if value.eq_ignore_ascii_case("bearer") {
Ok(HttpAuthType::Bearer)
} else if value.eq_ignore_ascii_case("basic") {
Ok(HttpAuthType::Basic)
} else if value.eq_ignore_ascii_case("x-api-key") {
Ok(HttpAuthType::XApiKey)
} else {
match value.as_str() {
"undefined" | "" => Ok(HttpAuthType::None),
custom => Ok(HttpAuthType::Custom(custom.to_string())),
}
}
}
_ => Err("Auth Type must be a string or undefined".to_string()),
}
}
Expand All @@ -93,15 +100,21 @@ impl HttpAuthPlace {
fn from_value(input: &Value) -> Result<Option<HttpAuthPlace>, String> {
match input.kind.as_ref() {
Some(Kind::NullValue(_)) | None => Ok(None),
Some(Kind::StringValue(value)) => match value.as_str() {
"Header" => Ok(Some(HttpAuthPlace::Header)),
"Url" => Ok(Some(HttpAuthPlace::Url)),
"undefined" | "" => Ok(None),
other => Err(format!(
"Auth Placement must be 'Header', 'Url', or undefined, got '{}'",
other
)),
},
Some(Kind::StringValue(value)) => {
if value.eq_ignore_ascii_case("header") {
Ok(Some(HttpAuthPlace::Header))
} else if value.eq_ignore_ascii_case("url") {
Ok(Some(HttpAuthPlace::Url))
} else {
match value.as_str() {
"undefined" | "" => Ok(None),
other => Err(format!(
"Auth Placement must be 'Header', 'Url', or undefined, got '{}'",
other
)),
}
}
}
_ => Err("Auth Placement must be a string or undefined".to_string()),
}
}
Expand Down Expand Up @@ -335,9 +348,7 @@ fn apply_auth(
HttpAuthType::Custom(scheme) => {
let value = auth_string_value(auth_value, "Custom auth value")?;
match place {
HttpAuthPlace::Header => {
insert_header(headers, "authorization", format!("{} {}", scheme, value))
}
HttpAuthPlace::Header => insert_header(headers, "authorization", value),
HttpAuthPlace::Url => append_query_param(url, scheme, &value),
}
Ok(())
Expand Down Expand Up @@ -677,6 +688,34 @@ mod tests {
assert_eq!(url, "https://example.test/resource?X-API-Key=a%20b");
}

#[test]
fn auth_type_parses_builtin_values_case_insensitively() {
assert_eq!(
HttpAuthType::from_value(&string_value("bearer")),
Ok(HttpAuthType::Bearer)
);
assert_eq!(
HttpAuthType::from_value(&string_value("basic")),
Ok(HttpAuthType::Basic)
);
assert_eq!(
HttpAuthType::from_value(&string_value("x-api-key")),
Ok(HttpAuthType::XApiKey)
);
}

#[test]
fn auth_place_parses_values_case_insensitively() {
assert_eq!(
HttpAuthPlace::from_value(&string_value("header")),
Ok(Some(HttpAuthPlace::Header))
);
assert_eq!(
HttpAuthPlace::from_value(&string_value("url")),
Ok(Some(HttpAuthPlace::Url))
);
}

#[test]
fn encode_headers_rejects_null_values() {
let headers = Struct {
Expand Down