-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathnet_test.rs
More file actions
132 lines (112 loc) · 3.71 KB
/
net_test.rs
File metadata and controls
132 lines (112 loc) · 3.71 KB
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
use starbase_sandbox::create_empty_sandbox;
use starbase_utils::net;
use std::collections::HashMap;
fn headers_with_custom_header() -> HashMap<String, String> {
let mut headers = HashMap::new();
headers.insert(
"X-Proto-Test-Header".to_string(),
"proto-starbase-headers-test".to_string(),
);
headers
}
mod download {
use super::*;
#[test]
fn checks_online() {
assert!(!net::is_offline_with_options(Default::default()));
}
#[should_panic(expected = "UrlParseFailed")]
#[tokio::test]
async fn errors_invalid_url() {
let sandbox = create_empty_sandbox();
net::download_from_url(
"raw.githubusercontent.com/moonrepo/starbase/master/README.md",
sandbox.path().join("README.md"),
)
.await
.unwrap();
}
#[should_panic(expected = "UrlNotFound")]
#[tokio::test]
async fn errors_not_found() {
let sandbox = create_empty_sandbox();
net::download_from_url(
"https://raw.githubusercontent.com/moonrepo/starbase/master/UNKNOWN_FILE.md",
sandbox.path().join("README.md"),
)
.await
.unwrap();
}
#[tokio::test]
async fn downloads_a_file() {
let sandbox = create_empty_sandbox();
let dest_file = sandbox.path().join("README.md");
assert!(!dest_file.exists());
net::download_from_url(
"https://raw.githubusercontent.com/moonrepo/starbase/master/README.md",
&dest_file,
)
.await
.unwrap();
assert!(dest_file.exists());
assert_ne!(dest_file.metadata().unwrap().len(), 0);
}
#[tokio::test]
async fn downloads_with_headers() {
let sandbox = create_empty_sandbox();
let dest_file = sandbox.path().join("headers.json");
net::download_from_url_with_headers(
"https://httpbin.org/headers",
&dest_file,
headers_with_custom_header(),
)
.await
.unwrap();
let body = std::fs::read_to_string(&dest_file).unwrap();
assert!(
body.contains("proto-starbase-headers-test"),
"expected response to contain custom header value, got: {body}"
);
}
#[tokio::test]
async fn downloads_with_client_and_headers() {
let sandbox = create_empty_sandbox();
let dest_file = sandbox.path().join("headers.json");
let client = reqwest::Client::new();
net::download_from_url_with_client_and_headers(
"https://httpbin.org/headers",
&dest_file,
&client,
headers_with_custom_header(),
)
.await
.unwrap();
let body = std::fs::read_to_string(&dest_file).unwrap();
assert!(
body.contains("proto-starbase-headers-test"),
"expected response to contain custom header value, got: {body}"
);
}
#[tokio::test]
async fn downloads_with_options_headers() {
let sandbox = create_empty_sandbox();
let dest_file = sandbox.path().join("headers.json");
net::download_from_url_with_options(
"https://httpbin.org/headers",
&dest_file,
net::DownloadOptions {
downloader: Some(Box::new(
net::DefaultDownloader::new_with_headers(headers_with_custom_header()).unwrap(),
)),
..Default::default()
},
)
.await
.unwrap();
let body = std::fs::read_to_string(&dest_file).unwrap();
assert!(
body.contains("proto-starbase-headers-test"),
"expected response to contain custom header value, got: {body}"
);
}
}