oas3 0.2.1

Structures and tools to parse, navigate and validate OpenAPI v3 specifications.
Documentation
use std::{fmt, ops::Deref};

use http::{header, HeaderMap, HeaderValue, Method, StatusCode};
use reqwest::Request;

use crate::{
    conformance::TestRequest, spec::Operation, validation::Error as ValidationError, Spec,
};

#[derive(Clone)]
pub enum TestAuthentication {
    Bearer(String),
    Headers(HeaderMap),
    Custom(fn(TestRequest) -> TestRequest),
}

impl TestAuthentication {
    /// Use the `Authorization: Bearer ...` method to provide authentication key/token.
    pub fn bearer(token: impl Into<String>) -> Self {
        Self::Bearer(token.into())
    }

    /// Shorthand for setting cookie header.
    pub fn cookie(cookies: Vec<impl AsRef<str>>) -> Self {
        let headers: HeaderMap = cookies
            .into_iter()
            .map(|cookie| {
                (
                    header::COOKIE,
                    HeaderValue::from_str(cookie.as_ref()).unwrap(),
                )
            })
            .collect();

        Self::Headers(headers)
    }

    /// Provide a closure that transforms a `TestRequest` into an authenticated `TestRequest`.
    pub fn custom(closure: fn(TestRequest) -> TestRequest) -> Self {
        Self::Custom(closure)
    }
}

impl fmt::Debug for TestAuthentication {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Custom(_) => write!(f, "[custom auth transformer]"),
            other => write!(f, "{:?}", other),
        }
    }
}