Skip to content

Commit c9c75ff

Browse files
committed
add tests
1 parent 9d6ebbc commit c9c75ff

File tree

2 files changed

+80
-8
lines changed

2 files changed

+80
-8
lines changed

crates/weaver_common/src/test.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use paris::error;
66
use std::path::PathBuf;
77

8-
use rouille::{match_assets, Server};
8+
use rouille::{match_assets, Response, Server};
99
use std::sync::mpsc::Sender;
1010

1111
/// An error that can occur while starting the HTTP server.
@@ -33,13 +33,41 @@ impl ServeStaticFiles {
3333
/// Creates a new HTTP server that serves static files from a directory.
3434
/// Note: This server is only available for testing purposes.
3535
pub fn from(static_path: impl Into<PathBuf>) -> Result<Self, HttpServerError> {
36+
Self::from_impl(static_path, None)
37+
}
38+
39+
/// Same as [`Self::from`], but requires `Authorization: Bearer <token>` on every request.
40+
/// Note: This server is only available for testing purposes.
41+
pub fn from_with_bearer(
42+
static_path: impl Into<PathBuf>,
43+
token: impl Into<String>,
44+
) -> Result<Self, HttpServerError> {
45+
Self::from_impl(static_path, Some(token.into()))
46+
}
47+
48+
/// Internal helper to keep the original implementation effectively intact.
49+
fn from_impl(
50+
static_path: impl Into<PathBuf>,
51+
token: Option<String>,
52+
) -> Result<Self, HttpServerError> {
3653
let static_path = static_path.into();
54+
3755
let server = Server::new("127.0.0.1:0", move |request| {
56+
if let Some(token) = token.as_ref() {
57+
if !request
58+
.header("Authorization")
59+
.map(|h| h == format!("Bearer {}", token))
60+
.unwrap_or(false)
61+
{
62+
return Response::text("Unauthorized").with_status_code(401);
63+
}
64+
}
3865
match_assets(request, &static_path)
3966
})
4067
.map_err(|e| HttpServerError {
4168
error: e.to_string(),
4269
})?;
70+
4371
let port = server.server_addr().port();
4472
let (_, kill_switch) = server.stoppable();
4573
Ok(Self { kill_switch, port })
@@ -93,4 +121,27 @@ mod tests {
93121
assert!(result.is_err());
94122
assert!(matches!(result.unwrap_err(), ureq::Error::Status(404, _)));
95123
}
124+
125+
#[test]
126+
fn test_http_server_with_bearer_auth() {
127+
let token = "token";
128+
let server = ServeStaticFiles::from_with_bearer("tests/test_data", token).unwrap();
129+
130+
let resp = ureq::get(&server.relative_path_to_url("file_a.yaml")).call();
131+
assert!(resp.is_err());
132+
assert!(matches!(resp.unwrap_err(), ureq::Error::Status(401, _)));
133+
134+
let resp = ureq::get(&server.relative_path_to_url("file_a.yaml"))
135+
.set("Authorization", "wrong_token")
136+
.call();
137+
assert!(resp.is_err());
138+
assert!(matches!(resp.unwrap_err(), ureq::Error::Status(401, _)));
139+
140+
let content = ureq::get(&server.relative_path_to_url("file_a.yaml"))
141+
.set("Authorization", &format!("Bearer {}", token))
142+
.call()
143+
.unwrap();
144+
assert_eq!(content.status(), 200);
145+
assert_eq!(content.into_string().unwrap(), "file: A");
146+
}
96147
}

crates/weaver_common/src/vdir.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,12 @@ mod tests {
875875
assert!(repo_path.exists());
876876
}
877877

878-
fn check_archive(vdir_path: VirtualDirectoryPath, file_to_check: Option<&str>) {
879-
let repo = VirtualDirectory::try_new(&vdir_path, None).unwrap();
878+
fn check_archive(
879+
vdir_path: VirtualDirectoryPath,
880+
file_to_check: Option<&str>,
881+
auth_token: Option<&String>,
882+
) {
883+
let repo = VirtualDirectory::try_new(&vdir_path, auth_token).unwrap();
880884
let repo_path = repo.path().to_path_buf();
881885
// At this point, the repo should be cloned into a temporary directory.
882886
assert!(repo_path.exists());
@@ -902,23 +906,23 @@ mod tests {
902906
sub_folder: Some("model".to_owned()),
903907
refspec: None,
904908
};
905-
check_archive(registry_path, None);
909+
check_archive(registry_path, None, None);
906910
}
907911

908912
#[test]
909913
fn test_semconv_registry_local_tar_gz_archive() {
910914
let registry_path = "../../test_data/semantic-conventions-1.26.0.tar.gz[model]"
911915
.parse::<VirtualDirectoryPath>()
912916
.unwrap();
913-
check_archive(registry_path, Some("general.yaml"));
917+
check_archive(registry_path, Some("general.yaml"), None);
914918
}
915919

916920
#[test]
917921
fn test_semconv_registry_local_zip_archive() {
918922
let registry_path = "../../test_data/semantic-conventions-1.26.0.zip[model]"
919923
.parse::<VirtualDirectoryPath>()
920924
.unwrap();
921-
check_archive(registry_path, Some("general.yaml"));
925+
check_archive(registry_path, Some("general.yaml"), None);
922926
}
923927

924928
#[test]
@@ -930,7 +934,7 @@ mod tests {
930934
)
931935
.parse::<VirtualDirectoryPath>()
932936
.unwrap();
933-
check_archive(registry_path, Some("general.yaml"));
937+
check_archive(registry_path, Some("general.yaml"), None);
934938
}
935939

936940
#[test]
@@ -942,6 +946,23 @@ mod tests {
942946
)
943947
.parse::<VirtualDirectoryPath>()
944948
.unwrap();
945-
check_archive(registry_path, Some("general.yaml"));
949+
check_archive(registry_path, Some("general.yaml"), None);
950+
}
951+
952+
#[test]
953+
fn test_semconv_registry_authentication() {
954+
let token = "token";
955+
let server = ServeStaticFiles::from_with_bearer("tests/test_data", token).unwrap();
956+
let registry_path = format!(
957+
"{}[model]",
958+
server.relative_path_to_url("semconv_registry_v1.26.0.zip")
959+
)
960+
.parse::<VirtualDirectoryPath>()
961+
.unwrap();
962+
check_archive(
963+
registry_path,
964+
Some("general.yaml"),
965+
Some(&token.to_string()),
966+
);
946967
}
947968
}

0 commit comments

Comments
 (0)