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
/**
assert that the status code of a conn is as specified.

```
use trillium_testing::prelude::*;
async fn handler(conn: trillium::Conn) -> trillium::Conn {
    conn.with_status(418)
}


assert_status!(get("/").on(&handler), 418);
assert_status!(get("/").on(&handler), Status::ImATeapot);

let conn = get("/").on(&handler);
assert_status!(&conn, 418);
assert_status!(conn, 418);
```


```rust,should_panic
use trillium_testing::prelude::*;
async fn handler(conn: trillium::Conn) -> trillium::Conn {
    conn.ok("handled")
}

assert_status!(get("/").on(&handler), 418);
```
*/

#[macro_export]
macro_rules! assert_status {
    ($conn:expr, $status:expr) => {{
        use std::convert::TryInto;
        let expected_status: $crate::prelude::Status =
            $status.try_into().expect("expected a status code");

        match $conn.status() {
            Some(status) => assert_eq!(status, expected_status),
            None => panic!("expected status code, but none was set"),
        }
    }};
}

/**
assert that all of the following are true:
* the status was not set
* the body was not set
* the conn was not halted

```
use trillium_testing::prelude::*;
async fn handler(conn: trillium::Conn) -> trillium::Conn {
    conn
}


assert_not_handled!(get("/").on(&handler));

let conn = get("/").on(&handler);
assert_not_handled!(&conn);
assert_not_handled!(conn);
```


```rust,should_panic
use trillium_testing::prelude::*;
async fn handler(conn: trillium::Conn) -> trillium::Conn {
    conn.ok("handled")
}

assert_not_handled!(get("/").on(&handler));
```
*/

#[macro_export]
macro_rules! assert_not_handled {
    ($conn:expr) => {{
        let conn = $conn;
        assert_eq!(conn.status(), None);
        assert!(conn.inner().response_body().is_none());
        assert!(!conn.is_halted());
    }};
}

/**
assert that the response body is as specified. this assertion requires mutation of the conn.

```
use trillium_testing::prelude::*;
async fn handler(conn: trillium::Conn) -> trillium::Conn {
    conn.ok("it's-a-me, trillium")
}


assert_body!(get("/").on(&handler), "it's-a-me, trillium");

let mut conn = get("/").on(&handler);
assert_body!(&mut conn, "it's-a-me, trillium");

let mut conn = get("/").on(&handler);
assert_body!(conn, "it's-a-me, trillium");
```


```rust,should_panic
use trillium_testing::prelude::*;
assert_body!(get("/").on(&()), "what body?");
```

```rust,should_panic
use trillium_testing::prelude::*;
assert_body!(get("/").on(&"beach body"), "winter body");
```
*/

#[macro_export]
macro_rules! assert_body {
    ($conn:expr, $body:expr) => {
        match $conn.take_response_body_string().unwrap_or_default() {
            body => {
                assert_eq!(body.trim_end(), $body.trim_end());
            }
        }
    };
}

/**

asserts that the response body matches the specified pattern, using [`str::contains`]
```
use trillium_testing::prelude::*;
let handler = "there's a needle in this haystack";
assert_body_contains!(get("/").on(&handler), "needle");

let mut conn = get("/").on(&handler);
let body = assert_body_contains!(&mut conn, "needle");
assert!(body.contains("haystack"));

```


```rust,should_panic
use trillium_testing::prelude::*;
assert_body_contains!(get("/").on(&()), "what body?");
```

```rust,should_panic
use trillium_testing::prelude::*;
assert_body_contains!(get("/").on(&"just a haystack"), "needle");
```
*/

#[macro_export]
macro_rules! assert_body_contains {
    ($conn:expr, $pattern:expr) => {
        match $conn.take_response_body_string().unwrap_or_default() {
            body => {
                assert!(
                    body.contains($pattern),
                    "\nexpected \n`{}`\n to contain `{}`\n but it did not",
                    &body,
                    $pattern
                );

                body
            }
        }
    };
}

/**
combines several other assertions. this assertion can be used to assert:
* just a status code,
* a status code and a response body, or
* a status code, a response body, and any number of headers

```
use trillium_testing::prelude::*;
async fn handler(conn: Conn) -> Conn {
    conn.with_body("just tea stuff here")
        .with_status(418)
        .with_header("server", "zojirushi")
}

assert_response!(get("/").on(&handler), 418);
assert_response!(get("/").on(&handler), Status::ImATeapot);
assert_response!(get("/").on(&handler), 418, "just tea stuff here");
assert_response!(get("/").on(&handler), Status::ImATeapot, "just tea stuff here");

assert_response!(
    get("/").on(&handler),
    Status::ImATeapot,
    "just tea stuff here",
    "server" => "zojirushi",
    "content-length" => "19"
);

```


*/

#[macro_export]
macro_rules! assert_response {
    ($conn:expr, $status:expr, $body:expr) => {{
        let mut conn = $conn;
        $crate::assert_status!(conn, $status);
        $crate::assert_body!(conn, $body);
    }};

    ($conn:expr, $status:expr) => {
        $crate::assert_status!($conn, $status);
    };

    ($conn:expr, $status:expr, $body:expr, $($header_name:literal => $header_value:expr,)+) => {
        assert_response!($conn, $status, $body, $($header_name => $header_value),+);
    };

    ($conn:expr, $status:expr, $body:expr, $($header_name:literal => $header_value:expr),*) => {
        let mut conn = $conn;
        $crate::assert_response!(&mut conn, $status, $body);
        $crate::assert_headers!(&conn, $($header_name => $header_value),*);
    };

}

/**
asserts any number of response headers

```
use trillium_testing::prelude::*;
async fn handler(conn: Conn) -> Conn {
    conn.ok("headers")
        .with_header("server", "special-custom-server")
        .with_header("request-id", "10")
}

assert_headers!(get("/").on(&handler), "server" => "special-custom-server");
assert_headers!(
    get("/").on(&handler),
    "server" => "special-custom-server",
    "request-id" => "10",
    "content-length" => "7"
);

```
*/
#[macro_export]
macro_rules! assert_headers {
    (@pair, $conn:expr, $header_name:tt, None) => {
        match $conn.inner().response_headers().get_str($header_name) {
            actual => {
                assert_eq!(actual, None, concat!("for header ", stringify!($header_name)));
            }
        };
    };

    (@pair, $conn:expr, $header_name:tt, $header_value:expr) => {
        match ($conn.inner().response_headers().get_str($header_name), $header_value) {
            (actual, expected) => {
                assert_eq!(actual, Some(expected), concat!("for header ", stringify!($header_name)));
            }
        };
    };

    ($conn:expr, $($header_name:tt => $header_value:tt,)+) => {
        assert_headers!($conn, $($header_name => $header_value),+);
    };

    ($conn:expr, $($header_name:tt => $header_value:tt),*) => {
        match $conn {
            conn => {
                $(assert_headers!(@pair, conn, $header_name, $header_value);)*
            }
        };
    }
}

/**
assert_ok is like [`assert_response!`] except it always asserts a status of 200 Ok.

it can be used to assert:
* just that the response was successful,
* that the response was successful and a response body, or
* that the response was successful, a response body, and any number of headers

```
use trillium_testing::prelude::*;
async fn handler(conn: Conn) -> Conn {
    conn.ok("body")
        .with_header("server", "special-custom-server")
        .with_header("request-id", "10")
}

assert_ok!(get("/").on(&handler));
assert_ok!(get("/").on(&handler), "body");
assert_ok!(get("/").on(&handler), "body");
assert_ok!(get("/").on(&handler), "body", "server" => "special-custom-server");

assert_ok!(
    get("/").on(&handler),
    "body",
    "server" => "special-custom-server",
    "request-id" => "10",
    "content-length" => "4"
);

```


*/

#[macro_export]
macro_rules! assert_ok {
    ($conn:expr) => {
        $crate::assert_response!($conn, 200);
    };

    ($conn:expr, $body:expr) => {
        $crate::assert_response!($conn, 200, $body);
    };


    ($conn:expr, $body:expr, $($header_name:literal => $header_value:expr,)+) => {
        assert_ok!($conn, $body, $($header_name => $header_value),+);
    };

    ($conn:expr, $body:expr, $($header_name:literal => $header_value:expr),*) => {
        $crate::assert_response!($conn, 200, $body, $($header_name => $header_value),*);
    };
}