@@ -13,149 +13,19 @@ mocktail is a minimal framework for mocking HTTP and gRPC services in Rust with
13
13
14
14
# Features
15
15
- Mocks HTTP and gRPC servers
16
- - Mocks defined in Rust using a simple API
16
+ - Mocks defined in Rust using a simple, ergonomic API
17
17
- Supports HTTP streaming
18
18
- Supports gRPC unary, client-streaming, server-streaming, and bidirectional-streaming methods
19
- - Performs basic "full body" (equals) matching
20
-
21
- # Concepts
22
- ## Mock Server
23
- A server that handles mock requests. This crate contains 2 servers:
24
- - HttpMockServer
25
- - GrpcMockServer
26
-
27
- ## Mock Body
28
- An enum containing the bytes of a mock request or response body.
29
-
30
- ``` rust
31
- pub enum MockBody {
32
- Empty ,
33
- Full (Bytes ),
34
- Stream (Vec <Bytes >),
35
- }
36
- ```
37
-
38
- ## Mock Request
39
- A mock request containing an optional body and optional headers.
40
-
41
- ``` rust
42
- // An empty body
43
- MockRequest :: empty ()
44
- // With a body that implements `Into<Bytes>`
45
- MockRequest :: new (body )
46
- MockRequest :: stream (messages )
47
- // Convenience constructors:
48
- // JSON: with a body that implements `serde::Serialize`
49
- MockRequest :: json (body )
50
- MockRequest :: json_stream (messages )
51
- // Protobuf (gRPC): with a body that implements `prost::Message`
52
- MockRequest :: pb (body )
53
- MockRequest :: pb_stream (messages )
54
- ```
55
-
56
- ## Mock Response
57
-
58
- A mock response containing a response code, optional body, optional headers, and optional error message. The response code defaults to ` 200 ` .
59
-
60
- ``` rust
61
- // An empty body
62
- MockResponse :: empty ()
63
- // With a body that implements `Into<Bytes>`
64
- MockResponse :: new (body )
65
- MockResponse :: stream (messages )
66
- // Convenience constructors:
67
- // JSON: with a body that implements `serde::Serialize`
68
- MockResponse :: json (body )
69
- MockResponse :: json_stream (messages )
70
- // Protobuf (gRPC): with a body that implements `prost::Message`
71
- MockResponse :: pb (body )
72
- MockResponse :: pb_stream (messages )
73
- ```
74
-
75
- ## Mock
76
- A mock request and response pair.
77
-
78
- ## Mock Path
79
- A mock path for request matching.
80
-
81
- ## Mock Set
82
- A set of mocks for a service.
19
+ - Performs matching using built-in matchers or custom matchers
83
20
84
21
# Usage
85
22
1 . Add ` mocktail ` to ` Cargo.toml ` as a development dependency:
86
23
``` toml
87
24
[dev-dependencies ]
88
- mocktail = { git = " https://github.com/IBM/mocktail.git" , version = " 0.1.1 -alpha" }
25
+ mocktail = { git = " https://github.com/IBM/mocktail.git" , version = " 0.2.0 -alpha" }
89
26
```
90
27
91
- 2. In a test context, use as follows. See [mocktail-test](/mocktail-test/) crate for more usage examples.
92
-
93
- ```rust
94
- use serde::{Deserialize, Serialize};
95
-
96
- # [derive(Debug, Clone, Serialize, Deserialize)]
97
- pub struct HelloRequest {
98
- pub name: String,
99
- }
100
-
101
- # [derive(Debug, Clone, Serialize, Deserialize)]
102
- pub struct HelloResponse {
103
- pub message: String,
104
- }
105
-
106
- # [cfg(test)]
107
- mod tests {
108
- use super::*;
109
-
110
- use mocktail::prelude::*;
111
-
112
- # [tokio::test]
113
- async fn test_hello_simple() -> Result<(), Box<dyn std::error::Error>> {
114
- // Create a mock set.
115
- let mut mocks = MockSet::new();
116
- // Insert mocks.
117
- // `MockRequest::json()` and `MockResponse::json()` are convenience methods
118
- // that handle JSON serialization to avoid `serde_json::to_vec(&value)` boilerplate.
119
- mocks.insert(
120
- MockPath::new(Method::POST, "/hello"),
121
- Mock::new(
122
- MockRequest::json(HelloRequest { name: "World".into() }),
123
- MockResponse::json(HelloResponse {
124
- message: "Hello World!".into(),
125
- }),
126
- ),
127
- );
128
- // Create and start a mock server.
129
- let server = HttpMockServer::new("hello", mocks)?;
130
- server.start().await?;
131
-
132
- // Send request to mock server.
133
- let client = reqwest::Client::new();
134
- let response = client
135
- .post(server.url("/hello"))
136
- .json(&HelloRequest { name: "World".into() })
137
- .send()
138
- .await?;
139
- assert!(response.status() == StatusCode::OK);
140
- let body = response.json::<HelloResponse>().await?;
141
- dbg!(&body);
142
-
143
- // Send request to mock server.
144
- // Doesn't match a mock so should return an error.
145
- let client = reqwest::Client::new();
146
- let response = client
147
- .post(server.url("/hello"))
148
- .json(&HelloRequest {
149
- name: "Missing".into(),
150
- })
151
- .send()
152
- .await?;
153
- assert!(response.status() == StatusCode::NOT_FOUND);
154
-
155
- Ok(())
156
- }
157
- }
158
- ```
28
+ 2. See [mocktail-tests](/mocktail-tests/) crate for usage examples.
159
29
160
30
# Examples
161
- See [mocktail-test ](/mocktail-test /) crate.
31
+ See [mocktail-tests ](/mocktail-tests /) crate.
0 commit comments