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
use httpmock::prelude::*;
use reqwest::blocking::Client;
// @example-start: forwarding
#[test]
fn forwarding_test() {
// We will create this mock server to simulate a real service (e.g., GitHub, AWS, etc.).
let target_server = MockServer::start();
target_server.mock(|when, then| {
when.any_request();
then.status(200).body("Hi from fake GitHub!");
});
// Let's create our mock server for the test
let server = MockServer::start();
// We configure our server to forward the request to the target host instead of
// answering with a mocked response. The 'when' variable lets you configure
// rules under which forwarding should take place.
server.forward_to(target_server.base_url(), |rule| {
rule.filter(|when| {
when.any_request(); // We want all requests to be forwarded.
});
});
// Now let's send an HTTP request to the mock server. The request will be forwarded
// to the target host, as we configured before.
let client = Client::new();
// Since the request was forwarded, we should see the target host's response.
let response = client.get(server.url("/get")).send().unwrap();
assert_eq!(response.status().as_u16(), 200);
assert_eq!(response.text().unwrap(), "Hi from fake GitHub!");
}
// @example-end
#[test]
fn forward_to_website() {
// Let's create our mock server for the test
let server = MockServer::start();
// We configure our server to forward the request to the target
// host instead of answering with a mocked response. The 'when'
// variable lets you configure rules under which forwarding
// should take place.
server.forward_to("https://httpmock.rs", |rule| {
rule.filter(|when| {
when.any_request(); // Ensure all requests are forwarded.
});
});
// Now let's send an HTTP request to the mock server. The request
// will be forwarded to the GitHub API, as we configured before.
let client = Client::new();
let response = client.get(server.base_url()).send().unwrap();
// Since the request was forwarded, we should see a GitHub API response.
assert_eq!(response.status().as_u16(), 200);
assert!(response
.text()
.unwrap()
.contains("Simple yet powerful HTTP mocking library for Rust"));
}