From 14a1c4944a77fcea87655ccc8f94d74873603bf2 Mon Sep 17 00:00:00 2001 From: DragonSlayer_14 Date: Wed, 2 Sep 2026 23:40:23 +0200 Subject: [PATCH] Test: GitHubClient-Token-Handling verbessert --- src/github.rs | 13 +++++++++---- tests/github_tests.rs | 21 ++++++++++++++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/github.rs b/src/github.rs index 03253d4..a034f37 100644 --- a/src/github.rs +++ b/src/github.rs @@ -1,4 +1,4 @@ -use anyhow::{bail, Context, Result}; +use anyhow::{Context, Result, bail}; use serde::{Deserialize, Serialize}; /// Unterstützte Linux-Paketverteilungstypen. @@ -81,7 +81,7 @@ struct GhApiAsset { #[derive(Clone, Debug)] pub struct GitHubClient { client: reqwest::Client, - token: Option, + pub token: Option, } impl GitHubClient { @@ -105,6 +105,7 @@ impl GitHubClient { Ok(Self { client, token }) } + /// Ruft Releases für ein angegebenes Repository ab (Format "owner/repo"). /// /// Wenn `history` false ist, werden nur die neuesten Releases abgerufen. @@ -129,7 +130,9 @@ impl GitHubClient { let mut req = self.client.get(&url); if let Some(token) = &self.token { - req = req.bearer_auth(token); + if !token.is_empty() { + req = req.bearer_auth(token); + } } let resp = req.send().await.with_context(|| format!("Failed to send request to {}", url))?; @@ -217,7 +220,9 @@ impl GitHubClient { pub async fn download_asset_stream(&self, download_url: &str) -> Result { let mut req = self.client.get(download_url); if let Some(token) = &self.token { - req = req.bearer_auth(token); + if !token.is_empty() { + req = req.bearer_auth(token); + } } let resp = req diff --git a/tests/github_tests.rs b/tests/github_tests.rs index 1a3b4f4..a64af71 100644 --- a/tests/github_tests.rs +++ b/tests/github_tests.rs @@ -1,4 +1,4 @@ -use mirror_package::github::{parse_repo_owner_name, PackageType}; +use mirror_package::github::{PackageType, parse_repo_owner_name}; #[test] fn test_package_classification() { @@ -68,3 +68,22 @@ fn test_parse_repo_owner_name() { assert!(parse_repo_owner_name("invalid_repo").is_err()); assert!(parse_repo_owner_name("invalid/repo/extra").is_err()); } + +#[tokio::test] +async fn test_github_client_empty_token() { + use mirror_package::github::GitHubClient; + + // Test that a client with an empty token can be created + let client = GitHubClient::new(Some("".to_string())).expect("Failed to create client with empty token"); + + // The client should be created successfully but has_auth() should return false + assert!(client.token.is_none() || client.token.as_ref().map_or(true, |t| t.is_empty())); + + // Test with a valid token + let client_with_token = GitHubClient::new(Some("valid_token".to_string())).expect("Failed to create client with valid token"); + assert!(client_with_token.token.as_ref().map_or(false, |t| !t.is_empty())); + + // Test with no token + let client_no_token = GitHubClient::new(None).expect("Failed to create client with no token"); + assert!(client_no_token.token.is_none()); +}