Skip to content

Commit

Permalink
test: add test for basic auth plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Nov 9, 2024
1 parent 9c13cbb commit 04d1ec0
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 5 deletions.
84 changes: 79 additions & 5 deletions src/http_extra/http_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,13 @@ pub static HTTP_HEADER_NAME_X_REQUEST_ID: Lazy<HeaderName> =

#[cfg(test)]
mod tests {
use crate::state::State;

use super::{
convert_header_value, convert_headers, HTTP_HEADER_CONTENT_HTML,
HTTP_HEADER_CONTENT_JSON, HTTP_HEADER_NAME_X_REQUEST_ID,
HTTP_HEADER_NO_CACHE, HTTP_HEADER_NO_STORE,
HTTP_HEADER_TRANSFER_CHUNKED, HTTP_HEADER_WWW_AUTHENTICATE,
};
use crate::state::State;
use http::HeaderValue;
use pingora::proxy::Session;
use pretty_assertions::assert_eq;
Expand All @@ -255,23 +254,98 @@ mod tests {

#[tokio::test]
async fn test_convert_header_value() {
let headers = [""].join("\r\n");
let headers = ["Host: pingap.io"].join("\r\n");
let input_header =
format!("GET /vicanso/pingap?size=1 HTTP/1.1\r\n{headers}\r\n\r\n");
let mock_io = Builder::new().read(input_header.as_bytes()).build();
let mut session = Session::new_h1(Box::new(mock_io));
session.read_request().await.unwrap();
let default_state = &State {
tls_version: Some("tls1.3".to_string()),
remote_addr: Some("10.1.1.1".to_string()),
remote_port: Some(6000),
server_addr: Some("10.1.1.2".to_string()),
server_port: Some(6001),
upstream_address: "10.1.1.3:4123".to_string(),
connection_id: 102,
..Default::default()
};

let value = convert_header_value(
&HeaderValue::from_str("$remote_addr").unwrap(),
&HeaderValue::from_str("$host").unwrap(),
&session,
&State {
..Default::default()
},
);
assert_eq!(true, value.is_some());
assert_eq!("pingap.io", value.unwrap().to_str().unwrap());

let value = convert_header_value(
&HeaderValue::from_str("$scheme").unwrap(),
&session,
&State {
remote_addr: Some("10.1.1.1".to_string()),
..Default::default()
},
);
assert_eq!(true, value.is_some());
assert_eq!("http", value.unwrap().to_str().unwrap());
let value = convert_header_value(
&HeaderValue::from_str("$scheme").unwrap(),
&session,
&default_state,
);
assert_eq!(true, value.is_some());
assert_eq!("https", value.unwrap().to_str().unwrap());

let value = convert_header_value(
&HeaderValue::from_str("$remote_addr").unwrap(),
&session,
&default_state,
);
assert_eq!(true, value.is_some());
assert_eq!("10.1.1.1", value.unwrap().to_str().unwrap());

let value = convert_header_value(
&HeaderValue::from_str("$remote_port").unwrap(),
&session,
&default_state,
);
assert_eq!(true, value.is_some());
assert_eq!("6000", value.unwrap().to_str().unwrap());

let value = convert_header_value(
&HeaderValue::from_str("$server_addr").unwrap(),
&session,
&default_state,
);
assert_eq!(true, value.is_some());
assert_eq!("10.1.1.2", value.unwrap().to_str().unwrap());

let value = convert_header_value(
&HeaderValue::from_str("$server_port").unwrap(),
&session,
&default_state,
);
assert_eq!(true, value.is_some());
assert_eq!("6001", value.unwrap().to_str().unwrap());

let value = convert_header_value(
&HeaderValue::from_str("$upstream_addr").unwrap(),
&session,
&default_state,
);
assert_eq!(true, value.is_some());
assert_eq!("10.1.1.3:4123", value.unwrap().to_str().unwrap());

let value = convert_header_value(
&HeaderValue::from_str(":connection_id").unwrap(),
&session,
&default_state,
);
assert_eq!(true, value.is_some());
assert_eq!("102", value.unwrap().to_str().unwrap());

let headers = ["X-Forwarded-For: 1.1.1.1, 2.2.2.2"].join("\r\n");
let input_header =
format!("GET /vicanso/pingap?size=1 HTTP/1.1\r\n{headers}\r\n\r\n");
Expand Down
9 changes: 9 additions & 0 deletions src/plugin/basic_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ mod tests {
use http::StatusCode;
use pingora::proxy::Session;
use pretty_assertions::assert_eq;
use std::time::Duration;
use tokio_test::io::Builder;

#[test]
Expand All @@ -173,6 +174,7 @@ authorizations = [
"MTIz",
"NDU2",
]
delay = "10s"
"###,
)
.unwrap(),
Expand All @@ -188,6 +190,8 @@ authorizations = [
.collect::<Vec<_>>()
.join(","),
);
assert_eq!(Duration::from_secs(10), params.delay.unwrap());
assert_eq!("AC7E9E03", params.hash_key());

let result = BasicAuth::try_from(
&toml::from_str::<PluginConf>(
Expand Down Expand Up @@ -229,6 +233,7 @@ authorizations = [
authorizations = [
"YWRtaW46MTIzMTIz"
]
hide_credentials = true
"###,
)
.unwrap(),
Expand All @@ -251,6 +256,10 @@ authorizations = [
.await
.unwrap();
assert_eq!(true, result.is_none());
assert_eq!(
false,
session.req_header().headers.contains_key("Authorization")
);

// auth fail
let headers = ["Authorization: Basic YWRtaW46MTIzMTIa"].join("\r\n");
Expand Down

0 comments on commit 04d1ec0

Please sign in to comment.