garrison 0.9.0

Garrison - 面向 Rust 生态的一站式身份认证鉴权框架
Documentation
// Copyright (c) 2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

//! 上下文层测试 mock 实现。
//!
//! 仅 `cfg(test)` 下编译,提供模拟 `GarrisonResponse` trait 的 `MockResponse` 供测试复用。

use crate::context::GarrisonResponse;
use crate::error::GarrisonResult;
use std::collections::HashMap;

/// Mock 响应实现,用于测试 GarrisonResponse trait 的默认方法。
pub struct MockResponse {
    /// cookie 存储(供测试断言直接访问)。
    pub cookies: HashMap<String, String>,
    /// header 存储(供测试断言直接访问)。
    pub headers: HashMap<String, String>,
    /// status code(供测试断言直接访问)。
    pub status: Option<u16>,
}

impl MockResponse {
    /// 创建空的 mock 响应(无 cookie/header/status)。
    pub fn new() -> Self {
        Self {
            cookies: HashMap::new(),
            headers: HashMap::new(),
            status: None,
        }
    }
}

impl GarrisonResponse for MockResponse {
    fn set_status(&mut self, code: u16) -> GarrisonResult<()> {
        self.status = Some(code);
        Ok(())
    }

    fn set_header(&mut self, name: &str, value: &str) -> GarrisonResult<()> {
        self.headers.insert(name.to_string(), value.to_string());
        Ok(())
    }

    fn set_cookie_with_config(
        &mut self,
        name: &str,
        value: &str,
        _config: &crate::config::GarrisonConfig,
    ) -> GarrisonResult<()> {
        self.cookies.insert(name.to_string(), value.to_string());
        Ok(())
    }
}