oparlint-suites 0.0.2

Lint suites that can be run by oparlint
Documentation
// SPDX-FileCopyrightText: Politik im Blick developers
// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
//
// SPDX-License-Identifier: AGPL-3.0-or-later OR EUPL-1.2

use oparlint_linting::{LintSuite, LintingResult};
use oparlint_lints::http::{CorsMethodsLint, CorsOriginLint, RequestSucceededLint, StatusCodeLint};
use reqwest::{Response, header::ACCEPT};
use url::Url;

#[derive(Debug)]
pub struct HttpRequestSuite<'a> {
    url: &'a Url,
}

impl<'a> HttpRequestSuite<'a> {
    /// Create a instance of the lint suite.
    pub fn new(url: &'a Url) -> Self {
        Self { url }
    }
}

#[async_trait::async_trait]
impl<'a> LintSuite for HttpRequestSuite<'a> {
    type Output = Response;

    fn identifier(&self) -> String {
        "http-request".into()
    }

    fn title(&self) -> String {
        format!("HTTP request to {} succeeds", self.url)
    }

    async fn execute<L: oparlint_linting::Linter + Send>(
        self,
        linter: &mut L,
    ) -> LintingResult<Self::Output> {
        let client = reqwest::Client::new();
        let response = client
            .get(self.url.clone())
            .header(ACCEPT, "application/json")
            .send()
            .await;

        let response = linter.execute_lint(RequestSucceededLint::new(response))?;
        _ = linter.execute_lint(StatusCodeLint::new(&response.status()));
        _ = linter.execute_lint(CorsOriginLint::new(response.headers()));
        _ = linter.execute_lint(CorsMethodsLint::new(response.headers()));
        Ok(response)
    }
}