Test: GitHubClient-Token-Handling verbessert

This commit is contained in:
2026-09-03 18:56:27 +02:00
parent 2f61e15646
commit 08cca0ca51
2 changed files with 29 additions and 5 deletions
+7 -2
View File
@@ -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<String>,
pub token: Option<String>,
}
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,8 +130,10 @@ impl GitHubClient {
let mut req = self.client.get(&url);
if let Some(token) = &self.token {
if !token.is_empty() {
req = req.bearer_auth(token);
}
}
let resp = req.send().await.with_context(|| format!("Failed to send request to {}", url))?;
let status = resp.status();
@@ -217,8 +220,10 @@ impl GitHubClient {
pub async fn download_asset_stream(&self, download_url: &str) -> Result<reqwest::Response> {
let mut req = self.client.get(download_url);
if let Some(token) = &self.token {
if !token.is_empty() {
req = req.bearer_auth(token);
}
}
let resp = req
.send()
+20 -1
View File
@@ -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());
}