armature-testing 0.2.1

Testing utilities for Armature applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//! Contract Testing (Pact)
//!
//! Provides consumer-driven contract testing capabilities.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use thiserror::Error;

/// Contract testing errors
#[derive(Debug, Error)]
pub enum ContractError {
    #[error("Verification failed: {0}")]
    VerificationFailed(String),

    #[error("Serialization error: {0}")]
    SerializationError(String),

    #[error("IO error: {0}")]
    IoError(String),

    #[error("Invalid contract: {0}")]
    InvalidContract(String),
}

/// HTTP method for contract
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "UPPERCASE")]
pub enum ContractMethod {
    Get,
    Post,
    Put,
    Delete,
    Patch,
    Head,
    Options,
}

/// Contract request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractRequest {
    /// HTTP method
    pub method: ContractMethod,

    /// Request path
    pub path: String,

    /// Query parameters
    #[serde(skip_serializing_if = "Option::is_none")]
    pub query: Option<HashMap<String, String>>,

    /// Request headers
    #[serde(skip_serializing_if = "Option::is_none")]
    pub headers: Option<HashMap<String, String>>,

    /// Request body
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<serde_json::Value>,
}

impl ContractRequest {
    /// Create new contract request
    pub fn new(method: ContractMethod, path: impl Into<String>) -> Self {
        Self {
            method,
            path: path.into(),
            query: None,
            headers: None,
            body: None,
        }
    }

    /// Add query parameter
    pub fn with_query(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.query
            .get_or_insert_with(HashMap::new)
            .insert(key.into(), value.into());
        self
    }

    /// Add header
    pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers
            .get_or_insert_with(HashMap::new)
            .insert(key.into(), value.into());
        self
    }

    /// Set body
    pub fn with_body(mut self, body: serde_json::Value) -> Self {
        self.body = Some(body);
        self
    }
}

/// Contract response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractResponse {
    /// HTTP status code
    pub status: u16,

    /// Response headers
    #[serde(skip_serializing_if = "Option::is_none")]
    pub headers: Option<HashMap<String, String>>,

    /// Response body
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<serde_json::Value>,
}

impl ContractResponse {
    /// Create new contract response
    pub fn new(status: u16) -> Self {
        Self {
            status,
            headers: None,
            body: None,
        }
    }

    /// Add header
    pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers
            .get_or_insert_with(HashMap::new)
            .insert(key.into(), value.into());
        self
    }

    /// Set body
    pub fn with_body(mut self, body: serde_json::Value) -> Self {
        self.body = Some(body);
        self
    }
}

/// Contract interaction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractInteraction {
    /// Interaction description
    pub description: String,

    /// Provider state (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provider_state: Option<String>,

    /// Request
    pub request: ContractRequest,

    /// Response
    pub response: ContractResponse,
}

impl ContractInteraction {
    /// Create new interaction
    pub fn new(
        description: impl Into<String>,
        request: ContractRequest,
        response: ContractResponse,
    ) -> Self {
        Self {
            description: description.into(),
            provider_state: None,
            request,
            response,
        }
    }

    /// Set provider state
    pub fn with_provider_state(mut self, state: impl Into<String>) -> Self {
        self.provider_state = Some(state.into());
        self
    }
}

/// Consumer contract (Pact)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Contract {
    /// Consumer name
    pub consumer: Participant,

    /// Provider name
    pub provider: Participant,

    /// Interactions
    pub interactions: Vec<ContractInteraction>,

    /// Metadata
    pub metadata: ContractMetadata,
}

/// Participant (consumer or provider)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Participant {
    pub name: String,
}

impl Participant {
    pub fn new(name: impl Into<String>) -> Self {
        Self { name: name.into() }
    }
}

/// Contract metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(Default)]
pub struct ContractMetadata {
    pub pact_specification: PactSpecification,
}

/// Pact specification version
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PactSpecification {
    pub version: String,
}

impl Default for PactSpecification {
    fn default() -> Self {
        Self {
            version: "3.0.0".to_string(),
        }
    }
}

/// Contract builder
pub struct ContractBuilder {
    consumer: String,
    provider: String,
    interactions: Vec<ContractInteraction>,
}

impl ContractBuilder {
    /// Create new contract builder
    ///
    /// # Examples
    ///
    /// ```
    /// use armature_testing::contract::*;
    ///
    /// let builder = ContractBuilder::new("MyConsumer", "MyProvider");
    /// ```
    pub fn new(consumer: impl Into<String>, provider: impl Into<String>) -> Self {
        Self {
            consumer: consumer.into(),
            provider: provider.into(),
            interactions: Vec::new(),
        }
    }

    /// Add interaction
    ///
    /// # Examples
    ///
    /// ```
    /// use armature_testing::contract::*;
    ///
    /// let request = ContractRequest::new(ContractMethod::Get, "/api/users/1");
    /// let response = ContractResponse::new(200)
    ///     .with_body(serde_json::json!({"id": 1, "name": "Alice"}));
    ///
    /// let mut builder = ContractBuilder::new("Frontend", "Backend");
    /// builder.add_interaction(ContractInteraction::new(
    ///     "get user by ID",
    ///     request,
    ///     response,
    /// ));
    /// ```
    pub fn add_interaction(&mut self, interaction: ContractInteraction) -> &mut Self {
        self.interactions.push(interaction);
        self
    }

    /// Build contract
    pub fn build(self) -> Contract {
        Contract {
            consumer: Participant::new(self.consumer),
            provider: Participant::new(self.provider),
            interactions: self.interactions,
            metadata: ContractMetadata::default(),
        }
    }
}

/// Contract manager
pub struct ContractManager {
    contracts_dir: PathBuf,
}

impl ContractManager {
    /// Create new contract manager
    ///
    /// # Examples
    ///
    /// ```
    /// use armature_testing::contract::ContractManager;
    /// use std::path::PathBuf;
    ///
    /// let manager = ContractManager::new(PathBuf::from("./pacts"));
    /// ```
    pub fn new(contracts_dir: PathBuf) -> Self {
        Self { contracts_dir }
    }

    /// Save contract to file
    pub fn save(&self, contract: &Contract) -> Result<(), ContractError> {
        // Create contracts directory if it doesn't exist
        std::fs::create_dir_all(&self.contracts_dir)
            .map_err(|e| ContractError::IoError(e.to_string()))?;

        // Generate filename
        let filename = format!(
            "{}-{}.json",
            contract.consumer.name.to_lowercase(),
            contract.provider.name.to_lowercase()
        );
        let path = self.contracts_dir.join(filename);

        // Serialize contract
        let json = serde_json::to_string_pretty(contract)
            .map_err(|e| ContractError::SerializationError(e.to_string()))?;

        // Write to file
        std::fs::write(path, json).map_err(|e| ContractError::IoError(e.to_string()))?;

        Ok(())
    }

    /// Load contract from file
    pub fn load(&self, consumer: &str, provider: &str) -> Result<Contract, ContractError> {
        let filename = format!(
            "{}-{}.json",
            consumer.to_lowercase(),
            provider.to_lowercase()
        );
        let path = self.contracts_dir.join(filename);

        let content =
            std::fs::read_to_string(path).map_err(|e| ContractError::IoError(e.to_string()))?;

        let contract: Contract = serde_json::from_str(&content)
            .map_err(|e| ContractError::SerializationError(e.to_string()))?;

        Ok(contract)
    }

    /// List all contracts.
    ///
    /// Reads consumer/provider names back out of each contract file's JSON
    /// body (`Contract::consumer`/`Contract::provider`), rather than
    /// splitting the filename on `-`: `save()` lowercases and joins the
    /// names with `-` to build `{consumer}-{provider}.json`, which is not
    /// reversible by splitting when either name itself contains a hyphen
    /// (e.g. consumer `"my-service"` -> filename `my-service-backend.json`
    /// would misparse as consumer `"my"`, provider `"service"`). Reading the
    /// JSON instead means the returned pairs always round-trip into
    /// `load()`, whatever characters the original names contained.
    pub fn list(&self) -> Result<Vec<(String, String)>, ContractError> {
        let entries = std::fs::read_dir(&self.contracts_dir)
            .map_err(|e| ContractError::IoError(e.to_string()))?;

        let mut contracts = Vec::new();

        for entry in entries {
            let entry = entry.map_err(|e| ContractError::IoError(e.to_string()))?;
            let path = entry.path();

            if path.extension().and_then(|ext| ext.to_str()) != Some("json") {
                continue;
            }

            let content = std::fs::read_to_string(&path)
                .map_err(|e| ContractError::IoError(e.to_string()))?;
            let contract: Contract = serde_json::from_str(&content)
                .map_err(|e| ContractError::SerializationError(e.to_string()))?;

            contracts.push((contract.consumer.name, contract.provider.name));
        }

        Ok(contracts)
    }
}

/// Contract verifier
pub struct ContractVerifier;

impl ContractVerifier {
    /// Verify a contract interaction's expected response against an actual
    /// response.
    ///
    /// This checks only the **response** side (status, headers, body) of
    /// `interaction` against `actual_response`; it never inspects, sends, or
    /// otherwise uses `interaction.request` — issuing the real HTTP call
    /// implied by `interaction.request` and capturing its response is the
    /// caller's responsibility, not this method's. Pact-style provider
    /// verification (replaying the request and asserting the response
    /// contract) is only partially covered as a result.
    pub fn verify_interaction(
        interaction: &ContractInteraction,
        actual_response: &ContractResponse,
    ) -> Result<(), ContractError> {
        // Verify status
        if interaction.response.status != actual_response.status {
            return Err(ContractError::VerificationFailed(format!(
                "Status mismatch: expected {}, got {}",
                interaction.response.status, actual_response.status
            )));
        }

        // Verify headers (if specified)
        if let Some(expected_headers) = &interaction.response.headers {
            let actual_headers = actual_response.headers.as_ref().ok_or_else(|| {
                ContractError::VerificationFailed("Missing response headers".to_string())
            })?;

            for (key, expected_value) in expected_headers {
                let actual_value = actual_headers.get(key).ok_or_else(|| {
                    ContractError::VerificationFailed(format!("Missing header: {}", key))
                })?;

                if actual_value != expected_value {
                    return Err(ContractError::VerificationFailed(format!(
                        "Header mismatch for '{}': expected '{}', got '{}'",
                        key, expected_value, actual_value
                    )));
                }
            }
        }

        // Verify body (if specified)
        if let Some(expected_body) = &interaction.response.body {
            let actual_body = actual_response.body.as_ref().ok_or_else(|| {
                ContractError::VerificationFailed("Missing response body".to_string())
            })?;

            if expected_body != actual_body {
                return Err(ContractError::VerificationFailed(format!(
                    "Body mismatch: expected {:?}, got {:?}",
                    expected_body, actual_body
                )));
            }
        }

        Ok(())
    }

    /// Verify entire contract
    pub fn verify_contract(
        _contract: &Contract,
        interactions_results: &[(ContractInteraction, ContractResponse)],
    ) -> Result<(), ContractError> {
        for (interaction, actual_response) in interactions_results {
            Self::verify_interaction(interaction, actual_response)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_contract_builder() {
        let request = ContractRequest::new(ContractMethod::Get, "/api/users/1");
        let response =
            ContractResponse::new(200).with_body(serde_json::json!({"id": 1, "name": "Alice"}));

        let mut builder = ContractBuilder::new("Frontend", "Backend");
        builder.add_interaction(ContractInteraction::new(
            "get user by ID",
            request,
            response,
        ));

        let contract = builder.build();
        assert_eq!(contract.consumer.name, "Frontend");
        assert_eq!(contract.provider.name, "Backend");
        assert_eq!(contract.interactions.len(), 1);
    }

    #[test]
    fn test_contract_verification_success() {
        let interaction = ContractInteraction::new(
            "get user",
            ContractRequest::new(ContractMethod::Get, "/api/users/1"),
            ContractResponse::new(200).with_body(serde_json::json!({"id": 1})),
        );

        let actual = ContractResponse::new(200).with_body(serde_json::json!({"id": 1}));

        assert!(ContractVerifier::verify_interaction(&interaction, &actual).is_ok());
    }

    #[test]
    fn test_contract_verification_status_mismatch() {
        let interaction = ContractInteraction::new(
            "get user",
            ContractRequest::new(ContractMethod::Get, "/api/users/1"),
            ContractResponse::new(200),
        );

        let actual = ContractResponse::new(404);

        assert!(ContractVerifier::verify_interaction(&interaction, &actual).is_err());
    }

    #[test]
    fn test_contract_request_builder() {
        let request = ContractRequest::new(ContractMethod::Post, "/api/users")
            .with_query("page", "1")
            .with_header("Content-Type", "application/json")
            .with_body(serde_json::json!({"name": "Bob"}));

        assert_eq!(request.method, ContractMethod::Post);
        assert_eq!(request.path, "/api/users");
        assert!(request.query.is_some());
        assert!(request.headers.is_some());
        assert!(request.body.is_some());
    }

    /// Isolated scratch directory under the OS temp dir, cleaned up on drop.
    struct TempDir(PathBuf);

    impl TempDir {
        fn new() -> Self {
            let dir = std::env::temp_dir()
                .join(format!("armature-contract-test-{}", uuid::Uuid::new_v4()));
            std::fs::create_dir_all(&dir).unwrap();
            Self(dir)
        }
    }

    impl Drop for TempDir {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }

    #[test]
    fn list_round_trips_hyphenated_consumer_and_provider_names_into_load() {
        let dir = TempDir::new();
        let manager = ContractManager::new(dir.0.clone());

        // Both names contain hyphens, so the filename
        // "my-service-backend-api.json" cannot be unambiguously split back
        // into (consumer, provider) by position; list() must read the
        // names from the JSON body instead.
        let contract = ContractBuilder::new("my-service", "backend-api").build();
        manager.save(&contract).unwrap();

        let listed = manager.list().unwrap();
        assert_eq!(
            listed,
            vec![("my-service".to_string(), "backend-api".to_string())]
        );

        let (consumer, provider) = &listed[0];
        let loaded = manager
            .load(consumer, provider)
            .expect("list()'s output must round-trip into load()");
        assert_eq!(loaded.consumer.name, "my-service");
        assert_eq!(loaded.provider.name, "backend-api");
    }

    #[test]
    fn list_returns_multiple_contracts_with_non_hyphenated_names_too() {
        let dir = TempDir::new();
        let manager = ContractManager::new(dir.0.clone());

        manager
            .save(&ContractBuilder::new("Frontend", "Backend").build())
            .unwrap();
        manager
            .save(&ContractBuilder::new("Mobile", "Backend").build())
            .unwrap();

        let mut listed = manager.list().unwrap();
        listed.sort();
        assert_eq!(
            listed,
            vec![
                ("Frontend".to_string(), "Backend".to_string()),
                ("Mobile".to_string(), "Backend".to_string()),
            ]
        );
    }
}