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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
mod support;
use support::*;
#[tokio::test]
async fn cookie_response_accessor() {
let server = server::http(move |_req| async move {
http::Response::builder()
.header("Set-Cookie", "key=val")
.header(
"Set-Cookie",
"expires=1; Expires=Wed, 21 Oct 2015 07:28:00 GMT",
)
.header("Set-Cookie", "path=1; Path=/the-path")
.header("Set-Cookie", "maxage=1; Max-Age=100")
.header("Set-Cookie", "domain=1; Domain=mydomain")
.header("Set-Cookie", "secure=1; Secure")
.header("Set-Cookie", "httponly=1; HttpOnly")
.header("Set-Cookie", "samesitelax=1; SameSite=Lax")
.header("Set-Cookie", "samesitestrict=1; SameSite=Strict")
.body(Default::default())
.unwrap()
});
let client = reqwest::Client::new();
let url = format!("http://{}/", server.addr());
let res = client.get(&url).send().await.unwrap();
let cookies = res.cookies().collect::<Vec<_>>();
// key=val
assert_eq!(cookies[0].name(), "key");
assert_eq!(cookies[0].value(), "val");
// expires
assert_eq!(cookies[1].name(), "expires");
assert_eq!(
cookies[1].expires().unwrap(),
std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1445412480)
);
// path
assert_eq!(cookies[2].name(), "path");
assert_eq!(cookies[2].path().unwrap(), "/the-path");
// max-age
assert_eq!(cookies[3].name(), "maxage");
assert_eq!(
cookies[3].max_age().unwrap(),
std::time::Duration::from_secs(100)
);
// domain
assert_eq!(cookies[4].name(), "domain");
assert_eq!(cookies[4].domain().unwrap(), "mydomain");
// secure
assert_eq!(cookies[5].name(), "secure");
assert_eq!(cookies[5].secure(), true);
// httponly
assert_eq!(cookies[6].name(), "httponly");
assert_eq!(cookies[6].http_only(), true);
// samesitelax
assert_eq!(cookies[7].name(), "samesitelax");
assert!(cookies[7].same_site_lax());
// samesitestrict
assert_eq!(cookies[8].name(), "samesitestrict");
assert!(cookies[8].same_site_strict());
}
#[tokio::test]
async fn cookie_store_simple() {
let server = server::http(move |req| async move {
if req.uri() == "/2" {
assert_eq!(req.headers()["cookie"], "key=val");
}
http::Response::builder()
.header("Set-Cookie", "key=val; HttpOnly")
.body(Default::default())
.unwrap()
});
let client = reqwest::Client::builder()
.cookie_store(true)
.build()
.unwrap();
let url = format!("http://{}/", server.addr());
client.get(&url).send().await.unwrap();
let url = format!("http://{}/2", server.addr());
client.get(&url).send().await.unwrap();
}
#[tokio::test]
async fn cookie_store_overwrite_existing() {
let server = server::http(move |req| async move {
if req.uri() == "/" {
http::Response::builder()
.header("Set-Cookie", "key=val")
.body(Default::default())
.unwrap()
} else if req.uri() == "/2" {
assert_eq!(req.headers()["cookie"], "key=val");
http::Response::builder()
.header("Set-Cookie", "key=val2")
.body(Default::default())
.unwrap()
} else {
assert_eq!(req.uri(), "/3");
assert_eq!(req.headers()["cookie"], "key=val2");
http::Response::default()
}
});
let client = reqwest::Client::builder()
.cookie_store(true)
.build()
.unwrap();
let url = format!("http://{}/", server.addr());
client.get(&url).send().await.unwrap();
let url = format!("http://{}/2", server.addr());
client.get(&url).send().await.unwrap();
let url = format!("http://{}/3", server.addr());
client.get(&url).send().await.unwrap();
}
#[tokio::test]
async fn cookie_store_max_age() {
let server = server::http(move |req| async move {
assert_eq!(req.headers().get("cookie"), None);
http::Response::builder()
.header("Set-Cookie", "key=val; Max-Age=0")
.body(Default::default())
.unwrap()
});
let client = reqwest::Client::builder()
.cookie_store(true)
.build()
.unwrap();
let url = format!("http://{}/", server.addr());
client.get(&url).send().await.unwrap();
client.get(&url).send().await.unwrap();
}
#[tokio::test]
async fn cookie_store_expires() {
let server = server::http(move |req| async move {
assert_eq!(req.headers().get("cookie"), None);
http::Response::builder()
.header(
"Set-Cookie",
"key=val; Expires=Wed, 21 Oct 2015 07:28:00 GMT",
)
.body(Default::default())
.unwrap()
});
let client = reqwest::Client::builder()
.cookie_store(true)
.build()
.unwrap();
let url = format!("http://{}/", server.addr());
client.get(&url).send().await.unwrap();
client.get(&url).send().await.unwrap();
}
#[tokio::test]
async fn cookie_store_path() {
let server = server::http(move |req| async move {
if req.uri() == "/" {
assert_eq!(req.headers().get("cookie"), None);
http::Response::builder()
.header("Set-Cookie", "key=val; Path=/subpath")
.body(Default::default())
.unwrap()
} else {
assert_eq!(req.uri(), "/subpath");
assert_eq!(req.headers()["cookie"], "key=val");
http::Response::default()
}
});
let client = reqwest::Client::builder()
.cookie_store(true)
.build()
.unwrap();
let url = format!("http://{}/", server.addr());
client.get(&url).send().await.unwrap();
client.get(&url).send().await.unwrap();
let url = format!("http://{}/subpath", server.addr());
client.get(&url).send().await.unwrap();
}
|