Test: GitHubClient-Token-Handling verbessert
This commit is contained in:
+9
-4
@@ -1,4 +1,4 @@
|
|||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{Context, Result, bail};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// Unterstützte Linux-Paketverteilungstypen.
|
/// Unterstützte Linux-Paketverteilungstypen.
|
||||||
@@ -81,7 +81,7 @@ struct GhApiAsset {
|
|||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct GitHubClient {
|
pub struct GitHubClient {
|
||||||
client: reqwest::Client,
|
client: reqwest::Client,
|
||||||
token: Option<String>,
|
pub token: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GitHubClient {
|
impl GitHubClient {
|
||||||
@@ -105,6 +105,7 @@ impl GitHubClient {
|
|||||||
Ok(Self { client, token })
|
Ok(Self { client, token })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Ruft Releases für ein angegebenes Repository ab (Format "owner/repo").
|
/// Ruft Releases für ein angegebenes Repository ab (Format "owner/repo").
|
||||||
///
|
///
|
||||||
/// Wenn `history` false ist, werden nur die neuesten Releases abgerufen.
|
/// Wenn `history` false ist, werden nur die neuesten Releases abgerufen.
|
||||||
@@ -129,7 +130,9 @@ impl GitHubClient {
|
|||||||
|
|
||||||
let mut req = self.client.get(&url);
|
let mut req = self.client.get(&url);
|
||||||
if let Some(token) = &self.token {
|
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))?;
|
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<reqwest::Response> {
|
pub async fn download_asset_stream(&self, download_url: &str) -> Result<reqwest::Response> {
|
||||||
let mut req = self.client.get(download_url);
|
let mut req = self.client.get(download_url);
|
||||||
if let Some(token) = &self.token {
|
if let Some(token) = &self.token {
|
||||||
req = req.bearer_auth(token);
|
if !token.is_empty() {
|
||||||
|
req = req.bearer_auth(token);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let resp = req
|
let resp = req
|
||||||
|
|||||||
+20
-1
@@ -1,4 +1,4 @@
|
|||||||
use mirror_package::github::{parse_repo_owner_name, PackageType};
|
use mirror_package::github::{PackageType, parse_repo_owner_name};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_package_classification() {
|
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").is_err());
|
||||||
assert!(parse_repo_owner_name("invalid/repo/extra").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());
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user