use crate::{
    request::DeboaRequest,
    tests::{helpers::client_with_cert, TestResult},
    Client,
};

use deboa_tests::{mock_response, utils::start_mock_server};
use http::{header::HOST, StatusCode};

#[cfg(feature = "smol-rt")]
use macro_rules_attribute::apply;
#[cfg(feature = "smol-rt")]
use smol_macros::test;

//
// PATCH
//

async fn do_patch() -> TestResult<()> {
    let mut server = start_mock_server(|req| async move {
        if req.method() == "PATCH" && req.uri().path() == "/posts/1" {
            assert!(req
                .headers()
                .contains_key(HOST));
            Ok(mock_response(StatusCode::OK, "done"))
        } else {
            Ok(mock_response(StatusCode::NOT_FOUND, "Not found"))
        }
    })
    .await;

    let client: Client = client_with_cert();

    let request = DeboaRequest::patch(server.url("/posts/1"))?
        .text("text")
        .build()?;

    let response = client
        .execute(request)
        .await?;

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(
        response
            .text()
            .await?,
        "done"
    );

    server
        .stop()
        .await?;

    Ok(())
}

#[cfg(feature = "tokio-rt")]
#[tokio::test]
async fn test_patch() -> TestResult<()> {
    do_patch().await
}

#[cfg(feature = "smol-rt")]
#[apply(test!)]
async fn test_patch() -> TestResult<()> {
    do_patch().await
}
