feat: add ping endpoint with test infrastructure
Implement ping() method on OnspringClient. Set up wiremock-based test infrastructure with shared setup helper. Add tests for success and unauthorized scenarios verifying correct headers are sent.
This commit is contained in:
@@ -1 +1 @@
|
||||
|
||||
mod ping;
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
use reqwest::Method;
|
||||
|
||||
use crate::client::OnspringClient;
|
||||
use crate::error::Result;
|
||||
|
||||
impl OnspringClient {
|
||||
/// Checks if the Onspring API is reachable.
|
||||
pub async fn ping(&self) -> Result<()> {
|
||||
self.request_no_content(Method::GET, "/Ping", &[], Option::<&()>::None)
|
||||
.await
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
use onspring::OnspringClient;
|
||||
use wiremock::MockServer;
|
||||
|
||||
pub async fn setup() -> (MockServer, OnspringClient) {
|
||||
let mock_server = MockServer::start().await;
|
||||
let client = OnspringClient::builder("test-api-key")
|
||||
.base_url(mock_server.uri())
|
||||
.build();
|
||||
(mock_server, client)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
mod common;
|
||||
|
||||
use wiremock::matchers::{header, method, path};
|
||||
use wiremock::{Mock, ResponseTemplate};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_ping_success() {
|
||||
let (mock_server, client) = common::setup().await;
|
||||
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/Ping"))
|
||||
.and(header("x-apikey", "test-api-key"))
|
||||
.and(header("x-api-version", "2"))
|
||||
.respond_with(ResponseTemplate::new(200))
|
||||
.mount(&mock_server)
|
||||
.await;
|
||||
|
||||
let result = client.ping().await;
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_ping_unauthorized() {
|
||||
let (mock_server, client) = common::setup().await;
|
||||
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/Ping"))
|
||||
.respond_with(ResponseTemplate::new(401))
|
||||
.mount(&mock_server)
|
||||
.await;
|
||||
|
||||
let result = client.ping().await;
|
||||
assert!(result.is_err());
|
||||
|
||||
if let Err(onspring::OnspringError::Api { status_code, .. }) = result {
|
||||
assert_eq!(status_code, 401);
|
||||
} else {
|
||||
panic!("Expected Api error");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user