//! Helpers for reading the `X-Company-Id` header.
use actix_web::HttpRequest;

/// Returns the company identifier carried in the request headers.
pub fn get_x_company_id(req: &HttpRequest) -> Option<String> {
    req.headers()
        .get("X-Company-Id")
        .and_then(|h| h.to_str().ok())
        .map(|s| s.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use actix_web::test::TestRequest;

    #[actix_web::test]
    async fn returns_none_when_missing() {
        let req = TestRequest::default().to_http_request();
        assert!(get_x_company_id(&req).is_none());
    }

    #[actix_web::test]
    async fn returns_value_when_present() {
        let req = TestRequest::default()
            .insert_header(("X-Company-Id", "company-1"))
            .to_http_request();
        assert_eq!(get_x_company_id(&req).unwrap(), "company-1");
    }
}
