forked from ducaale/xh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.rs
146 lines (131 loc) · 3.6 KB
/
logging.rs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use hyper::header::HeaderValue;
use predicates::str::contains;
use crate::prelude::*;
#[test]
fn logs_are_printed_in_debug_mode() {
get_command()
.arg("--debug")
.arg("--offline")
.arg(":")
.env_remove("RUST_LOG")
.assert()
.stderr(contains("DEBUG xh] Cli {"))
.success();
}
#[test]
fn logs_are_not_printed_outside_debug_mode() {
get_command()
.arg("--offline")
.arg(":")
.env_remove("RUST_LOG")
.assert()
.stderr("")
.success();
}
#[test]
fn backtrace_is_printed_in_debug_mode() {
let mut server = server::http(|_req| async move {
panic!("test crash");
});
server.disable_hit_checks();
get_command()
.arg("--debug")
.arg(server.base_url())
.env_remove("RUST_BACKTRACE")
.env_remove("RUST_LIB_BACKTRACE")
.assert()
.stderr(contains("Stack backtrace:"))
.failure();
}
#[test]
fn backtrace_is_not_printed_outside_debug_mode() {
let mut server = server::http(|_req| async move {
panic!("test crash");
});
server.disable_hit_checks();
let cmd = get_command()
.arg(server.base_url())
.env_remove("RUST_BACKTRACE")
.env_remove("RUST_LIB_BACKTRACE")
.assert()
.failure();
assert!(!std::str::from_utf8(&cmd.get_output().stderr)
.unwrap()
.contains("Stack backtrace:"));
}
#[test]
fn checked_status_is_printed_with_single_quiet() {
let server = server::http(|_req| async move {
hyper::Response::builder()
.status(404)
.body("".into())
.unwrap()
});
get_command()
.args(["--quiet", "--check-status", &server.base_url()])
.assert()
.code(4)
.stdout("")
.stderr("xh: warning: HTTP 404 Not Found\n");
}
#[test]
fn checked_status_is_not_printed_with_double_quiet() {
let server = server::http(|_req| async move {
hyper::Response::builder()
.status(404)
.body("".into())
.unwrap()
});
get_command()
.args(["--quiet", "--quiet", "--check-status", &server.base_url()])
.assert()
.code(4)
.stdout("")
.stderr("");
}
#[test]
fn warning_for_invalid_redirect() {
let server = server::http(|_req| async move {
hyper::Response::builder()
.status(302)
.header("location", "//")
.body("".into())
.unwrap()
});
get_command()
.args(["--follow", &server.base_url()])
.assert()
.stderr("xh: warning: Redirect to invalid URL: \"//\"\n");
}
#[test]
fn warning_for_non_utf8_redirect() {
let server = server::http(|_req| async move {
hyper::Response::builder()
.status(302)
.header("location", HeaderValue::from_bytes(b"\xFF").unwrap())
.body("".into())
.unwrap()
});
get_command()
.args(["--follow", &server.base_url()])
.assert()
.stderr("xh: warning: Redirect to invalid URL: \"\\xff\"\n");
}
/// This test should fail if rustls's version gets out of sync in Cargo.toml.
#[cfg(feature = "rustls")]
#[test]
fn rustls_emits_logs() {
let mut server = server::http(|_req| async move {
unreachable!();
});
server.disable_hit_checks();
let cmd = get_command()
.arg("--debug")
.arg(server.base_url().replace("http://", "https://"))
.env_remove("RUST_LOG")
.assert()
.failure();
assert!(std::str::from_utf8(&cmd.get_output().stderr)
.unwrap()
.contains("rustls::"));
}