Skip to main content

cloud_sdk_testkit/
response.rs

1//! Deterministic response fixture builders.
2
3use cloud_sdk::transport::StatusCode;
4
5use crate::{ActionFixture, FixtureBody, PaginationFixture, RateLimitFixture};
6
7/// Fixture response category.
8#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
9pub enum FixtureKind {
10    /// Successful response without additional metadata.
11    Success,
12    /// Successful paginated response.
13    Pagination,
14    /// Action polling response.
15    Action,
16    /// Rate-limit response.
17    RateLimit,
18    /// Client or server error response.
19    Error,
20}
21
22/// Response fixture construction error.
23#[derive(Clone, Copy, Debug, Eq, PartialEq)]
24pub enum ResponseFixtureError {
25    /// Error fixtures require a `4xx` or `5xx` status.
26    NonErrorStatus,
27}
28
29impl_static_error!(ResponseFixtureError,
30    Self::NonErrorStatus => "error fixture requires an HTTP error status",
31);
32
33/// Provider-neutral response body plus optional interpreted metadata.
34#[derive(Clone, Copy, Debug, Eq, PartialEq)]
35pub struct ResponseFixture<'a> {
36    kind: FixtureKind,
37    status: StatusCode,
38    body: FixtureBody<'a>,
39    pagination: Option<PaginationFixture>,
40    action: Option<ActionFixture>,
41    rate_limit: Option<RateLimitFixture>,
42}
43
44impl<'a> ResponseFixture<'a> {
45    /// Creates a `200 OK` response.
46    #[must_use]
47    pub const fn success(body: FixtureBody<'a>) -> Self {
48        Self::new(FixtureKind::Success, StatusCode::OK, body)
49    }
50
51    /// Creates a `200 OK` paginated response.
52    #[must_use]
53    pub const fn paginated(body: FixtureBody<'a>, pagination: PaginationFixture) -> Self {
54        let mut fixture = Self::new(FixtureKind::Pagination, StatusCode::OK, body);
55        fixture.pagination = Some(pagination);
56        fixture
57    }
58
59    /// Creates a `200 OK` action response.
60    #[must_use]
61    pub const fn action(body: FixtureBody<'a>, action: ActionFixture) -> Self {
62        let mut fixture = Self::new(FixtureKind::Action, StatusCode::OK, body);
63        fixture.action = Some(action);
64        fixture
65    }
66
67    /// Creates a `429 Too Many Requests` response.
68    #[must_use]
69    pub const fn rate_limited(body: FixtureBody<'a>, rate_limit: RateLimitFixture) -> Self {
70        let mut fixture = Self::new(FixtureKind::RateLimit, StatusCode::TOO_MANY_REQUESTS, body);
71        fixture.rate_limit = Some(rate_limit);
72        fixture
73    }
74
75    /// Adds rate-limit metadata to any response fixture.
76    #[must_use]
77    pub const fn with_rate_limit(mut self, rate_limit: RateLimitFixture) -> Self {
78        self.rate_limit = Some(rate_limit);
79        self
80    }
81
82    /// Creates a client or server error response.
83    pub const fn error(
84        status: StatusCode,
85        body: FixtureBody<'a>,
86    ) -> Result<Self, ResponseFixtureError> {
87        if !status.is_error() {
88            return Err(ResponseFixtureError::NonErrorStatus);
89        }
90        Ok(Self::new(FixtureKind::Error, status, body))
91    }
92
93    const fn new(kind: FixtureKind, status: StatusCode, body: FixtureBody<'a>) -> Self {
94        Self {
95            kind,
96            status,
97            body,
98            pagination: None,
99            action: None,
100            rate_limit: None,
101        }
102    }
103
104    /// Returns the fixture category.
105    #[must_use]
106    pub const fn kind(self) -> FixtureKind {
107        self.kind
108    }
109
110    /// Returns the response status.
111    #[must_use]
112    pub const fn status(self) -> StatusCode {
113        self.status
114    }
115
116    /// Returns the response body source.
117    #[must_use]
118    pub const fn body(self) -> FixtureBody<'a> {
119        self.body
120    }
121
122    /// Returns pagination metadata when present.
123    #[must_use]
124    pub const fn pagination(self) -> Option<PaginationFixture> {
125        self.pagination
126    }
127
128    /// Returns action metadata when present.
129    #[must_use]
130    pub const fn action_metadata(self) -> Option<ActionFixture> {
131        self.action
132    }
133
134    /// Returns rate-limit metadata when present.
135    #[must_use]
136    pub const fn rate_limit(self) -> Option<RateLimitFixture> {
137        self.rate_limit
138    }
139}