aboutsummaryrefslogtreecommitdiffstats
path: root/tests/raw.rs
blob: 95100d2a1a270ce219203d5ab62acac6b521ec20 (plain)
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
mod fixtures;
mod utils;

use crate::fixtures::TestServer;
use fixtures::{server, Error};
use pretty_assertions::assert_eq;
use reqwest::blocking::Client;
use rstest::rstest;
use select::document::Document;
use select::predicate::Class;
use select::predicate::Name;

/// The footer displays the correct wget command to download the folder recursively
// This test can't test all aspects of the wget footer,
// a more detailed unit test is available
#[rstest(
    depth,
    dir,
    case(0, ""),
    case(1, "dira/"),
    case(2, "very/deeply/"),
    case(3, "very/deeply/nested/")
)]
fn ui_displays_wget_element(
    depth: u8,
    dir: &str,
    #[with(&["-W"])] server: TestServer,
) -> Result<(), Error> {
    let client = Client::new();

    let body = client
        .get(format!("{}{}", server.url(), dir))
        .send()?
        .error_for_status()?;
    let parsed = Document::from_read(body)?;
    let wget_url = parsed
        .find(Class("downloadDirectory"))
        .next()
        .unwrap()
        .find(Class("cmd"))
        .next()
        .unwrap()
        .text();
    let cut_dirs = match depth {
        // Put all the files in a folder of this name
        0 => format!(" -P 'localhost:{}'", server.port()),
        1 => String::new(),
        // Avoids putting the files in excessive directories
        x => format!(" --cut-dirs={}", x - 1),
    };
    assert_eq!(
        wget_url,
        format!(
            "wget -rcnHp -R 'index.html*'{} '{}{}?raw=true'",
            cut_dirs,
            server.url(),
            dir
        )
    );

    Ok(())
}

/// All hrefs in raw mode are links to directories or files & directories end with ?raw=true
#[rstest(
    dir,
    case(""),
    case("very/"),
    case("very/deeply/"),
    case("very/deeply/nested/")
)]
fn raw_mode_links_to_directories_end_with_raw_true(
    dir: &str,
    #[with(&["-W"])] server: TestServer,
) -> Result<(), Error> {
    fn verify_a_tags(parsed: Document) {
        // Ensure all links end with ?raw=true or are files
        for node in parsed.find(Name("a")) {
            if let Some(class) = node.attr("class") {
                if class == "root" || class == "directory" {
                    assert!(
                        node.attr("href").unwrap().ends_with("?raw=true"),
                        "doesn't end with ?raw=true"
                    );
                } else if class == "file" {
                    return;
                } else {
                    panic!(
                        "This node is a link and neither of class directory, root or file: {node:?}"
                    );
                }
            }
        }
    }

    let client = Client::new();
    // Ensure the links to the archives are not present
    let body = client
        .get(format!("{}{}?raw=true", server.url(), dir))
        .send()?
        .error_for_status()?;
    let parsed = Document::from_read(body)?;
    verify_a_tags(parsed);

    Ok(())
}