whyhttp 1.0.0

HTTP mock server for use in tests. Starts a real server, expectations configured via fluent builder API, verified automatically on drop.
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#![doc = include_str!("../README.md")]

use std::{
    net::SocketAddr,
    ops::Deref,
    sync::{Arc, mpsc::Sender},
};

use crate::{
    matchers::Matcher,
    print::print_report,
    server::run_server,
    worker::{ExpectationId, SharedWorker},
};

mod expectation;
mod io;
mod matchers;
mod print;
mod reports;
pub mod server;
mod worker;

struct Inner {
    worker: SharedWorker,
    addr: SocketAddr,
    server_shutdown_tx: Sender<()>,
}

impl Deref for Inner {
    type Target = SharedWorker;

    fn deref(&self) -> &Self::Target {
        &self.worker
    }
}

impl Drop for Inner {
    fn drop(&mut self) {
        self.server_shutdown_tx.send(()).ok();
        let Some(reports) = self.worker.lock().unwrap().reports() else {
            return;
        };

        for report in reports {
            print_report(report);
        }

        panic!("assertion http request")
    }
}

impl Inner {
    fn addr(&self) -> SocketAddr {
        self.addr
    }
}

#[derive(Clone)]
struct InnerGuard {
    inner: Arc<Inner>,
    id: ExpectationId,
}

impl Deref for InnerGuard {
    type Target = SharedWorker;

    fn deref(&self) -> &Self::Target {
        self.inner.deref()
    }
}

impl InnerGuard {
    fn new(inner: Arc<Inner>) -> Self {
        let id = inner.worker.lock().unwrap().create_next();
        Self { inner, id }
    }

    fn addr(&self) -> SocketAddr {
        self.inner.addr()
    }
}

/// HTTP mock server for use in tests.
///
/// Start with [`Whyhttp::run`], configure expectations via the builder chain,
/// then point your HTTP client at [`url`](Whyhttp::url).
///
/// On drop, any unfulfilled or violated expectations are printed and the
/// the test panics on drop - so a failing mock automatically fails the test.
///
/// See the [crate-level documentation](crate) for a full example.
pub struct Whyhttp {
    inner: Arc<Inner>,
}

/// Builder for routing matchers.
///
/// Obtained from [`Whyhttp::when`]. Chain matcher methods to narrow which
/// requests this expectation handles, then call [`response`](WhenWhyhttpRequest::response)
/// to configure the reply.
///
/// Routing matchers are evaluated on every incoming request; the first
/// expectation whose matchers all pass handles the request.
pub struct WhenWhyhttpRequest {
    inner: InnerGuard,
}

/// Builder for validating matchers.
///
/// Obtained from [`WhenWhyhttpRequest::should`]. Validating matchers run
/// after an expectation is selected by routing. They never affect which
/// response is returned - failures are collected and reported on drop.
///
/// Use this to assert *how* a matched request looks (e.g. confirm a specific
/// header is present) without affecting routing.
pub struct ShouldWhyhttpRequest {
    inner: InnerGuard,
}

/// Builder for configuring the mock response.
///
/// Obtained from [`WhenWhyhttpRequest::response`] or
/// [`ShouldWhyhttpRequest::response`]. Chain [`status`](WhyhttpResponse::status),
/// [`header`](WhyhttpResponse::header), and [`body`](WhyhttpResponse::body) to
/// build the response returned when this expectation is matched.
///
/// Calling [`when`](WhyhttpResponse::when) starts a **new** expectation,
/// which is useful for registering multiple mocks in one chain.
pub struct WhyhttpResponse {
    inner: InnerGuard,
}

impl Whyhttp {
    /// Starts the mock server on an ephemeral port.
    ///
    /// The server runs in a background thread and shuts down automatically
    /// when this instance is dropped. Unfulfilled expectations cause a panic
    /// on drop.
    ///
    /// ```
    /// let server = whyhttp::Whyhttp::run();
    /// assert!(server.url().starts_with("http://"));
    /// ```
    pub fn run() -> Self {
        let worker = SharedWorker::default();
        let (server_shutdown_tx, addr) = run_server(worker.clone());
        Self {
            inner: Arc::new(Inner {
                worker,
                addr,
                server_shutdown_tx,
            }),
        }
    }

    /// Creates a new expectation and enters the routing phase.
    ///
    /// The returned [`WhenWhyhttpRequest`] lets you add routing matchers.
    /// Call [`response`](WhenWhyhttpRequest::response) to configure the reply,
    /// or [`should`](WhenWhyhttpRequest::should) to add validating matchers first.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().path("/api").method("POST").response().status(201);
    /// ```
    pub fn when(&self) -> WhenWhyhttpRequest {
        WhenWhyhttpRequest {
            inner: InnerGuard::new(self.inner.clone()),
        }
    }

    /// Creates a new expectation and enters the validation phase.
    ///
    /// Shorthand for `self.when().should()`. The expectation has no routing
    /// matchers, so it matches every request.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// // Assert every request carries this header, regardless of path.
    /// server.should().header("x-request-id", "abc").response().status(200);
    /// ```
    pub fn should(&self) -> ShouldWhyhttpRequest {
        self.when().should()
    }

    /// Creates a new expectation and enters the response phase.
    ///
    /// Shorthand for `self.when().response()`. No routing or validating
    /// matchers: the expectation matches every request.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.response().status(503).body("unavailable");
    /// ```
    pub fn response(&self) -> WhyhttpResponse {
        self.when().response()
    }
}

impl WhenWhyhttpRequest {
    /// Transitions to the validation phase for this expectation.
    ///
    /// Routing matchers added so far are preserved. Subsequent matcher calls
    /// add validating matchers that are checked after routing.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().path("/api")
    ///     .should().method("POST").header("content-type", "application/json")
    ///     .response().status(200);
    /// ```
    pub fn should(&self) -> ShouldWhyhttpRequest {
        ShouldWhyhttpRequest {
            inner: self.inner.clone(),
        }
    }

    /// Transitions to the response phase for this expectation.
    ///
    /// Shorthand for `self.should().response()`.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().path("/ping").response().status(200);
    /// ```
    pub fn response(&self) -> WhyhttpResponse {
        self.should().response()
    }

    /// Routes requests where the path equals `path`.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().path("/api/users").response().status(200);
    /// ```
    pub fn path<S: Into<String>>(self, path: S) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_routing(&self.inner.id, Matcher::Path(path.into()));

        self
    }

    /// Routes requests where the HTTP method equals `method` (case-insensitive).
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().method("DELETE").response().status(204);
    /// ```
    pub fn method<S: Into<String>>(self, method: S) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_routing(&self.inner.id, Matcher::Method(method.into()));

        self
    }

    /// Routes requests where query parameter `key` equals `value`.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().query("page", "2").response().status(200);
    /// ```
    pub fn query<K: Into<String>, V: Into<String>>(self, key: K, value: V) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_routing(&self.inner.id, Matcher::QueryEq(key.into(), value.into()));

        self
    }

    /// Routes requests where query parameter `key` is present (any value).
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().query_exists("token").response().status(200);
    /// ```
    pub fn query_exists<K: Into<String>>(self, key: K) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_routing(&self.inner.id, Matcher::QueryExists(key.into()));

        self
    }

    /// Routes requests where query parameter `key` is absent.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().without_query("debug").response().status(200);
    /// ```
    pub fn without_query<K: Into<String>>(self, key: K) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_routing(&self.inner.id, Matcher::QueryMiss(key.into()));

        self
    }

    /// Routes requests where header `key` equals `value`.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().header("accept", "application/json").response().status(200);
    /// ```
    pub fn header<K: Into<String>, V: Into<String>>(self, key: K, value: V) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_routing(&self.inner.id, Matcher::HeaderEq(key.into(), value.into()));

        self
    }

    /// Routes requests where header `key` is present (any value).
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().header_exists("authorization").response().status(200);
    /// ```
    pub fn header_exists<K: Into<String>>(self, key: K) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_routing(&self.inner.id, Matcher::HeaderExists(key.into()));

        self
    }

    /// Routes requests where header `key` is absent.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().without_header("x-internal").response().status(200);
    /// ```
    pub fn without_header<K: Into<String>>(self, key: K) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_routing(&self.inner.id, Matcher::HeaderMiss(key.into()));

        self
    }

    /// Routes requests where the body equals `body`.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().body(r#"{"ok":true}"#).response().status(200);
    /// ```
    pub fn body<S: Into<String>>(self, body: S) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_routing(&self.inner.id, Matcher::BodyEq(body.into()));

        self
    }

    /// Routes requests with an empty body.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().without_body().response().status(400);
    /// ```
    pub fn without_body(self) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_routing(&self.inner.id, Matcher::BodyMiss);

        self
    }
}

impl ShouldWhyhttpRequest {
    /// Transitions to the response phase for this expectation.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().path("/api").should().method("GET").response().status(200);
    /// ```
    pub fn response(&self) -> WhyhttpResponse {
        WhyhttpResponse {
            inner: self.inner.clone(),
        }
    }

    /// Creates a **new** expectation and enters the routing phase.
    ///
    /// Shorthand for `self.response().when()`. Use this to chain multiple
    /// expectation setups without storing intermediate values.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server
    ///     .when().path("/a").should().method("GET").response().status(200)
    ///     .when().path("/b").should().method("POST").response().status(201);
    /// ```
    pub fn when(&self) -> WhenWhyhttpRequest {
        self.response().when()
    }

    /// Validates that the path equals `path`.
    pub fn path<S: Into<String>>(self, path: S) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_validating(&self.inner.id, Matcher::Path(path.into()));

        self
    }

    /// Validates that the HTTP method equals `method` (case-insensitive).
    pub fn method<S: Into<String>>(self, method: S) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_validating(&self.inner.id, Matcher::Method(method.into()));

        self
    }

    /// Validates that query parameter `key` equals `value`.
    pub fn query<K: Into<String>, V: Into<String>>(self, key: K, value: V) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_validating(&self.inner.id, Matcher::QueryEq(key.into(), value.into()));

        self
    }

    /// Validates that query parameter `key` is present.
    pub fn query_exists<K: Into<String>>(self, key: K) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_validating(&self.inner.id, Matcher::QueryExists(key.into()));

        self
    }

    /// Validates that query parameter `key` is absent.
    pub fn without_query<K: Into<String>>(self, key: K) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_validating(&self.inner.id, Matcher::QueryMiss(key.into()));

        self
    }

    /// Validates that header `key` equals `value`.
    pub fn header<K: Into<String>, V: Into<String>>(self, key: K, value: V) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_validating(&self.inner.id, Matcher::HeaderEq(key.into(), value.into()));

        self
    }

    /// Validates that header `key` is present.
    pub fn header_exists<K: Into<String>>(self, key: K) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_validating(&self.inner.id, Matcher::HeaderExists(key.into()));

        self
    }

    /// Validates that header `key` is absent.
    pub fn without_header<K: Into<String>>(self, key: K) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_validating(&self.inner.id, Matcher::HeaderMiss(key.into()));

        self
    }

    /// Validates that the body equals `body`.
    pub fn body<S: Into<String>>(self, body: S) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_validating(&self.inner.id, Matcher::BodyEq(body.into()));

        self
    }

    /// Validates that the body is empty.
    pub fn without_body(self) -> Self {
        self.inner
            .lock()
            .unwrap()
            .add_validating(&self.inner.id, Matcher::BodyMiss);

        self
    }

    /// Asserts this expectation is called exactly `times` times.
    ///
    /// A mismatch is reported on drop.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().path("/api").should().times(3).response().status(200);
    /// ```
    pub fn times(self, times: u16) -> Self {
        self.inner.lock().unwrap().set_times(&self.inner.id, times);
        self
    }
}

impl WhyhttpResponse {
    /// Creates a **new** expectation and enters the routing phase.
    ///
    /// Use this to register multiple expectations in a single chain.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server
    ///     .when().path("/a").response().status(200)
    ///     .when().path("/b").response().status(201);
    /// ```
    pub fn when(&self) -> WhenWhyhttpRequest {
        WhenWhyhttpRequest {
            inner: InnerGuard::new(self.inner.inner.clone()),
        }
    }

    /// Sets the response status code. Defaults to `200`.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().path("/created").response().status(201);
    /// ```
    pub fn status(self, status: u16) -> Self {
        self.inner
            .lock()
            .unwrap()
            .set_response_status(&self.inner.id, status);
        self
    }

    /// Adds a response header.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().path("/data").response()
    ///     .header("content-type", "application/json");
    /// ```
    pub fn header<K: Into<String>, V: Into<String>>(self, key: K, value: V) -> Self {
        self.inner
            .lock()
            .unwrap()
            .set_response_header(&self.inner.id, key.into(), value.into());
        self
    }

    /// Sets the response body.
    ///
    /// ```no_run
    /// let server = whyhttp::Whyhttp::run();
    /// server.when().path("/hello").response().body("world");
    /// ```
    pub fn body<S: Into<String>>(self, body: S) -> Self {
        self.inner
            .lock()
            .unwrap()
            .set_response_body(&self.inner.id, body.into());
        self
    }
}

macro_rules! impl_helpers {
    (url => $($base:ident),+) => {
        $(
            impl $base {
                /// Returns the server's socket address.
                pub fn addr(&self) -> SocketAddr {
                    self.inner.addr()
                }

                /// Returns the server's base URL (e.g. `http://127.0.0.1:PORT`).
                pub fn url(&self) -> String {
                    format!("http://{}", self.addr())
                }
            }
        )+
    };
    (when => $($base:ident),+) => {
        $(
            impl $base {
                /// Shorthand for `self.when().path(path)`.
                pub fn when_path<S: Into<String>>(&self, path: S) -> WhenWhyhttpRequest {
                    self.when().path(path)
                }

                /// Shorthand for `self.when().method(method)`.
                pub fn when_method<S: Into<String>>(&self, method: S) -> WhenWhyhttpRequest {
                    self.when().method(method)
                }

                /// Shorthand for `self.when().query(key, value)`.
                pub fn when_query<K: Into<String>, V: Into<String>>(&self, key: K, value: V) -> WhenWhyhttpRequest {
                    self.when().query(key, value)
                }

                /// Shorthand for `self.when().query_exists(key)`.
                pub fn when_query_exists<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest {
                    self.when().query_exists(key)
                }

                /// Shorthand for `self.when().without_query(key)`.
                pub fn when_without_query<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest {
                    self.when().without_query(key)
                }

                /// Shorthand for `self.when().header(key, value)`.
                pub fn when_header<K: Into<String>, V: Into<String>>(&self, key: K, value: V) -> WhenWhyhttpRequest {
                    self.when().header(key, value)
                }

                /// Shorthand for `self.when().header_exists(key)`.
                pub fn when_header_exists<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest {
                    self.when().header_exists(key)
                }

                /// Shorthand for `self.when().without_header(key)`.
                pub fn when_without_header<K: Into<String>>(&self, key: K) -> WhenWhyhttpRequest {
                    self.when().without_header(key)
                }

                /// Shorthand for `self.when().body(body)`.
                pub fn when_body<S: Into<String>>(&self, body: S) -> WhenWhyhttpRequest {
                    self.when().body(body)
                }

                /// Shorthand for `self.when().without_body()`.
                pub fn when_without_body(&self) -> WhenWhyhttpRequest {
                    self.when().without_body()
                }
            }
        )+
    };
    (should => $($base:ident),+) => {
        $(
            impl $base {
                /// Shorthand for `self.should().path(path)`.
                pub fn should_path<S: Into<String>>(&self, path: S) -> ShouldWhyhttpRequest {
                    self.should().path(path)
                }

                /// Shorthand for `self.should().method(method)`.
                pub fn should_method<S: Into<String>>(&self, method: S) -> ShouldWhyhttpRequest {
                    self.should().method(method)
                }

                /// Shorthand for `self.should().query(key, value)`.
                pub fn should_query<K: Into<String>, V: Into<String>>(&self, key: K, value: V) -> ShouldWhyhttpRequest {
                    self.should().query(key, value)
                }

                /// Shorthand for `self.should().query_exists(key)`.
                pub fn should_query_exists<K: Into<String>>(&self, key: K) -> ShouldWhyhttpRequest {
                    self.should().query_exists(key)
                }

                /// Shorthand for `self.should().without_query(key)`.
                pub fn should_without_query<K: Into<String>>(&self, key: K) -> ShouldWhyhttpRequest {
                    self.should().without_query(key)
                }

                /// Shorthand for `self.should().header(key, value)`.
                pub fn should_header<K: Into<String>, V: Into<String>>(&self, key: K, value: V) -> ShouldWhyhttpRequest {
                    self.should().header(key, value)
                }

                /// Shorthand for `self.should().header_exists(key)`.
                pub fn should_header_exists<K: Into<String>>(&self, key: K) -> ShouldWhyhttpRequest {
                    self.should().header_exists(key)
                }

                /// Shorthand for `self.should().without_header(key)`.
                pub fn should_without_header<K: Into<String>>(&self, key: K) -> ShouldWhyhttpRequest {
                    self.should().without_header(key)
                }

                /// Shorthand for `self.should().body(body)`.
                pub fn should_body<S: Into<String>>(&self, body: S) -> ShouldWhyhttpRequest {
                    self.should().body(body)
                }

                /// Shorthand for `self.should().without_body()`.
                pub fn should_without_body(&self) -> ShouldWhyhttpRequest {
                    self.should().without_body()
                }

                /// Shorthand for `self.should().times(times)`.
                pub fn should_times(&self, times: u16) -> ShouldWhyhttpRequest {
                    self.should().times(times)
                }
            }
        )+
    };
    (response => $($base:ident),+) => {
        $(
            impl $base {
                /// Shorthand for `self.response().status(status)`.
                pub fn response_status(&self, status: u16) -> WhyhttpResponse {
                    self.response().status(status)
                }

                /// Shorthand for `self.response().header(key, value)`.
                pub fn response_header<K: Into<String>, V: Into<String>>(&self, key: K, value: V) -> WhyhttpResponse {
                    self.response().header(key, value)
                }

                /// Shorthand for `self.response().body(body)`.
                pub fn response_body<S: Into<String>>(&self, body: S) -> WhyhttpResponse {
                    self.response().body(body)
                }
            }
        )+
    };
}

impl_helpers!(url => Whyhttp, WhenWhyhttpRequest, ShouldWhyhttpRequest, WhyhttpResponse);
impl_helpers!(when => Whyhttp, ShouldWhyhttpRequest, WhyhttpResponse);
impl_helpers!(should => Whyhttp, WhenWhyhttpRequest);
impl_helpers!(response => Whyhttp, WhenWhyhttpRequest, ShouldWhyhttpRequest);