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
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")
);
}
/// Deletes a forwarding rule through the admin API of a remote server and
/// verifies that deleting an unknown rule id returns 404.
#[cfg(feature = "remote")]
#[test]
fn delete_forwarding_rule_on_remote_server_test() {
use crate::with_standalone_server;
// Arrange: connect to a standalone server so the deletion goes through
// the HTTP admin API rather than the in-process state manager.
with_standalone_server();
let server = MockServer::connect("localhost:5050");
let mut rule = server.forward_to("http://localhost:12345", |rule| {
rule.filter(|when| {
when.any_request();
});
});
// Act + Assert: panics if the server does not respond with 204.
rule.delete();
// Deleting an unknown rule id must return 404.
let response = Client::new()
.delete(format!("{}/__httpmock__/forwarding_rules/4242", server.base_url()))
.send()
.unwrap();
assert_eq!(response.status().as_u16(), 404);
}