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> {
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)
}
}