Skip to content

Commit 437c0c1

Browse files
authored
Update book (#28)
Signed-off-by: declark1 <[email protected]>
1 parent 87d8be8 commit 437c0c1

18 files changed

+242
-40
lines changed

book/src/SUMMARY.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@
2020
- [Priority](./concepts/priority.md)
2121
- [Mock Server](./concepts/mock-server.md)
2222
- [Defining Mocks](./defining-mocks.md)
23-
- [Miscellaneous](./misc.md)
24-
- [FAQ](./faq.md)
25-
- [CHANGELOG](./CHANGELOG.md)
26-
- [Contributing](./contributing.md)
23+
- [Example: HTTP Simple](./defining-mocks/example-http-simple.md)
24+
- [Example: HTTP Streaming (ndjson)](./defining-mocks/example-http-streaming-ndjson.md)
25+
- [Example: HTTP Streaming (SSE)](./defining-mocks/example-http-streaming-sse.md)
26+
- [Example: gRPC Unary](./defining-mocks/example-grpc-unary.md)
27+
- [Example: gRPC Client Streaming](./defining-mocks/example-grpc-client-streaming.md)
28+
- [Example: gRPC Server Streaming](./defining-mocks/example-grpc-server-streaming.md)
29+
- [Example: gRPC Bidi Streaming](./defining-mocks/example-grpc-bidi-streaming.md)
30+
- [Example: Custom Matcher](./defining-mocks/example-custom-matcher.md)
31+
---
32+
[FAQ](./FAQ.md)
33+
[CHANGELOG](./CHANGELOG.md)
34+
[CONTRIBUTING](./CONTRIBUTING.md)

book/src/concepts/matchers/any.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,13 @@
55
Matches any request. Should not be combined with other matchers.
66

77
### `When` method
8-
- `any()`
8+
#### `any()`
9+
Matches any request.
10+
11+
Example:
12+
```rust
13+
let mock = Mock::new(|when, then| {
14+
when.any();
15+
then.ok();
16+
})
17+
```

book/src/concepts/matchers/body.md

+95-10
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,98 @@
55
Matches a request by body.
66

77
### `When` methods:
8-
- `body()` *(primary)*
9-
- `empty()`
10-
- `bytes()`
11-
- `bytes_stream()`
12-
- `text()`
13-
- `text_stream()`
14-
- `json()`
15-
- `json_lines_stream()`
16-
- `pb()`
17-
- `pb_stream()`
8+
9+
#### `body(body)` *(primary)*
10+
11+
#### `empty()`
12+
An empty body.
13+
14+
Example:
15+
```rust
16+
let mock = Mock::new(|when, then| {
17+
when.empty();
18+
then.ok();
19+
})
20+
```
21+
#### `bytes(body)`
22+
A raw bytes body. `body` is a type implementing `Into<Bytes>`.
23+
```rust
24+
let mock = Mock::new(|when, then| {
25+
when.bytes("hello".as_bytes());
26+
then.ok();
27+
})
28+
```
29+
#### `bytes_stream(messages)`
30+
A raw bytes streaming body. `messages` is an iterator of messages implementing `Into<Bytes>`.
31+
```rust
32+
let mock = Mock::new(|when, then| {
33+
when.bytes_stream([
34+
"msg1".as_bytes(),
35+
"msg2".as_bytes(),
36+
"msg3".as_bytes(),
37+
]);
38+
then.ok();
39+
})
40+
```
41+
#### `text(body)`
42+
A text body. `body` is a type implementing `Into<String>`.
43+
```rust
44+
let mock = Mock::new(|when, then| {
45+
when.text("hello");
46+
then.ok();
47+
})
48+
```
49+
#### `text_stream(messages)`
50+
A text streaming body. `messages` is an iterator of `String` messages.
51+
```rust
52+
let mock = Mock::new(|when, then| {
53+
when.text_stream([
54+
"msg1",
55+
"msg2",
56+
"msg3"
57+
]);
58+
then.ok();
59+
})
60+
```
61+
#### `json(body)`
62+
A json body. `body` is a type implementing `serde::Serialize`.
63+
```rust
64+
use serde_json::json;
65+
let mock = Mock::new(|when, then| {
66+
when.json(json!({"message": "hello"}));
67+
then.ok();
68+
})
69+
```
70+
#### `json_lines_stream(messages)`
71+
A newline delimited json streaming body. `messages` is an iterator of messages implementing `serde::Serialize`.
72+
```rust
73+
use serde_json::json;
74+
let mock = Mock::new(|when, then| {
75+
when.json_lines_stream([
76+
json!({"message": "msg1"}),
77+
json!({"message": "msg2"}),
78+
json!({"message": "msg3"}),
79+
]);
80+
then.ok();
81+
})
82+
```
83+
#### `pb(body)`
84+
A protobuf body. `body` is a prost-generated type implementing `prost::Message`.
85+
```rust
86+
let mock = Mock::new(|when, then| {
87+
when.pb(ExampleMessage { message: "msg" });
88+
then.ok();
89+
})
90+
```
91+
#### `pb_stream(messages)`
92+
A protobuf streaming body. `messages` is an iterator of messages implementing `prost::Message`.
93+
```rust
94+
let mock = Mock::new(|when, then| {
95+
when.pb_stream([
96+
ExampleMessage { message: "msg1" },
97+
ExampleMessage { message: "msg2" },
98+
ExampleMessage { message: "msg3" },
99+
]);
100+
then.ok();
101+
})
102+
```

book/src/concepts/matchers/custom.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
Matches a request by a custom `Matcher` implementation.
66

77
### `When` method:
8-
- `matcher()`
8+
#### `matcher(matcher)`
9+
Custom matcher. `matcher` is type implementing `Matcher`.

book/src/concepts/matchers/headers.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@
55
Matches a request by headers. Returns `true` if the request headers are a *superset* of the headers, i.e. contains *at least* all of the headers.
66

77
### `When` method:
8-
- `headers()`
8+
#### `headers(headers)`
9+
Headers. `headers` is an iterator of name-value pairs.
910

1011
## Headers Exact
1112

1213
Matches a request by exact headers. Returns `true` if the request headers are *equal to* the headers.
1314

1415
### `When` method:
15-
- `headers_exact()`
16+
#### `headers_exact(headers)`
17+
Exact headers. `headers` is an iterator of name-value pairs.
1618

1719
## Header
1820

1921
Matches a request by header. Returns `true` if the request contains a header *equal to* the header.
2022

2123
### `When` method:
22-
- `header()`
24+
#### `header(name, value)`
25+
Header. `name` and `value` are types implementing `Into<String>`.
2326

2427
## Header Exists
2528

2629
Matches a request by header exists. Returns `true` if the request contains a header with the header name.
2730

2831
### `When` method:
29-
- `header_exists()`
32+
#### `header_exists(name)`
33+
Header exists. `name` is a type implementing `Into<String>`.

book/src/concepts/matchers/method.md

+57-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,60 @@
55
Matches a request by HTTP method.
66

77
### `When` methods:
8-
- `method()` *(primary)*
9-
- `get()`
10-
- `post()`
11-
- `put()`
12-
- `head()`
13-
- `delete()`
8+
#### `method(method)` *(primary)*
9+
HTTP method.
10+
11+
#### `get()`
12+
HTTP GET method.
13+
14+
Example:
15+
```rust
16+
let mock = Mock::new(|when, then| {
17+
when.get();
18+
then.ok();
19+
})
20+
```
21+
22+
#### `post()`
23+
HTTP POST method.
24+
25+
Example:
26+
```rust
27+
let mock = Mock::new(|when, then| {
28+
when.post();
29+
then.ok();
30+
})
31+
```
32+
33+
#### `put()`
34+
HTTP PUT method.
35+
36+
Example:
37+
```rust
38+
let mock = Mock::new(|when, then| {
39+
when.put();
40+
then.ok();
41+
})
42+
```
43+
44+
#### `head()`
45+
HTTP HEAD method.
46+
47+
Example:
48+
```rust
49+
let mock = Mock::new(|when, then| {
50+
when.head();
51+
then.ok();
52+
})
53+
```
54+
55+
#### `delete()`
56+
HTTP DELETE method.
57+
58+
Example:
59+
```rust
60+
let mock = Mock::new(|when, then| {
61+
when.delete();
62+
then.ok();
63+
})
64+
```

book/src/concepts/matchers/path.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,30 @@
55
Matches a request by path.
66

77
### `When` method:
8-
- `path()`
8+
9+
#### `path(path)`
10+
Path.
11+
12+
Example:
13+
```rust
14+
let mock = Mock::new(|when, then| {
15+
when.path("/path");
16+
then.ok();
17+
})
18+
```
919

1020
## Path Prefix
1121

1222
Matches a request by path prefix. Returns `true` if the request path starts with prefix.
1323

1424
### `When` method:
15-
- `path_prefix()`
25+
#### `path_prefix(prefix)`
26+
Path prefix.
27+
28+
Example:
29+
```rust
30+
let mock = Mock::new(|when, then| {
31+
when.path("/p");
32+
then.ok();
33+
})
34+
```

book/src/concepts/matchers/query-params.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
Matches a request by query params. Returns `true` if the request query params are *equal to* the query params.
66

77
### `When` method:
8-
- `query_params()`
8+
#### `query_params(params)`
9+
Query params. `params` is an iterator of key-value pairs.
910

1011
## Query Param
1112

1213
Matches a request by query param. Returns `true` if the request contains a query param *equal to* the query param.
1314

1415
### `When` method:
15-
- `query_param()`
16+
#### `query_param(key, value)`
17+
Query param. `key` and `value` are types implementing `Into<String>`.
1618

1719
## Query Param Exists
1820

1921
Matches a request by query param exists. Returns `true` if the request contains a query param with the query key.
2022

2123
### `When` method:
22-
- `query_param_exists()`
24+
#### `query_param_exists(key)`
25+
Query param exists. `key` is a type implementing `Into<String>`.

book/src/defining-mocks.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
# Defining Mocks
22

3-
Mocks are defined in Rust using a simple, ergonomic builder-like API. See [Mock Builder](./concepts/mock-builder.md) for additional details.
3+
Mocks are defined in Rust using a simple, ergonomic [builder-like API](./concepts/mock-builder.md).
44

5-
You can define your mocks first, then create a mock server with your mock set.
5+
You can define your mocks first, then create a mock server with your mock set:
66
```rust
7-
// Build and insert mocks using MockSet::mock()
7+
// Create a mock set
88
let mut mocks = MockSet::new();
9+
// Build and insert a mock
910
mocks.mock(|when, then| {
1011
when.get().path("/health");
1112
then.text("healthy!");
1213
});
13-
// Alternatively, use Mock::new() and MockSet::insert()
14+
// Alternatively, Mock::new() and mocks.insert(mock)
1415

1516
// Create mock server with the mock set
1617
let mut server = MockServer::new("example").with_mocks(mocks);
1718
server.run().await?;
1819
```
1920

20-
Alternatively, you can create a mock server with a default (empty) mock set and register mocks directly to the server.
21+
Or, you can create a mock server with a default empty mock set and register mocks directly to the server:
2122
```rust
22-
// Create mock server (with a default empty mock set)
23+
// Create mock server
2324
let mut server = MockServer::new("example");
2425
server.run().await?;
2526

26-
// Register mocks to the server directly using MockServer::mock()
27+
// Build and insert a mock to the server's mock set
2728
server.mock(|when, then| {
2829
when.get().path("/health");
2930
then.text("healthy!");
3031
});
31-
// Alternatively, use Mock::new() and MockServer::mocks().insert()
32+
// Alternatively, use Mock::new() and server.mocks.insert(mock)
3233
```
3334

3435

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Example: Custom Matcher
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Example: gRPC Bidi Streaming
2+
3+
TODO
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Example: gRPC Client Streaming
2+
3+
TODO
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Example: gRPC Server Streaming
2+
3+
TODO
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Example: gRPC Unary
2+
3+
TODO
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Example: HTTP Simple
2+
3+
TODO

0 commit comments

Comments
 (0)