|
5 | 5 | Matches a request by body.
|
6 | 6 |
|
7 | 7 | ### `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 | +``` |
0 commit comments