~rjframe/surf-vcr

surf-vcr/examples/simple.rs -rw-r--r-- 1.7 KiB
02fcf3c4Ryan Frame Update documentation for release 2 months ago
                                                                                
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
//! In "record" mode, retrieves the content at example.com or another site and
//! stores it in a YAML document. In "play" mode, intercepts the HTTP request
//! and provides the pre-recorded response instead of obtaining one from the
//! remote server.
//!
//! Example runs:
//!
//! ```
//! cargo run --example=simple -- record
//! cargo run --example=simple -- record https://example.com/some/where
//! cargo run --example=simple -- play https://example.com/some/where
//! cargo run --example=simple -- play
//! cargo run --example=simple -- play https://example.com/no/where
//! ```

use std::env;

use async_std::task;

use surf;
use surf_vcr::{VcrMiddleware, VcrMode};

fn main() {
    let args: Vec<String> = env::args().collect();

    if args.len() < 2 || (args[1] != "record" && args[1] != "play") {
        println!("Usage: {} record|play [URL]", args[0]);
        return;
    }

    let site = if args.len() == 3 { &args[2] } else { "https://example.com" };

    let mode = if args[1] == "record" {
        let _ = std::fs::remove_file("simple-recording-example.yml");
        VcrMode::Record
    } else if args[1] == "play" {
        VcrMode::Replay
    } else {
        panic!()
    };

    task::block_on(async {
        let vcr = VcrMiddleware::new(mode, "simple-recording-example.yml")
            .await.unwrap();

        let client = surf::Client::new().with(vcr);

        let req = surf::get(site)
            .header("User-Agent", "surf-lib-example")
            .build();

        let mut res = client.send(req).await.unwrap();

        if site == "https://example.com" {
            let text = res.body_string().await.unwrap();
            assert!(text.contains("illustrative examples"));
        }

        println!("Status: {}", res.status());
    });
}