forked from rust-lang/crates.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaching.rs
More file actions
204 lines (177 loc) · 7.77 KB
/
Copy pathcaching.rs
File metadata and controls
204 lines (177 loc) · 7.77 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
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
204
//! Tests for the HTTP caching headers that control CDN and browser caching.
//!
//! Responses that depend on the authenticated identity must carry
//! `Cache-Control: no-store` so that no shared cache (CDN) or browser cache
//! stores them. Identity can come from a session cookie or an API token, so
//! `no-store` is used instead of relying on `Vary: Cookie`.
use crate::builders::CrateBuilder;
use crate::util::{MockRequestExt, RequestHelper, TestApp};
use http::{StatusCode, header};
#[tokio::test(flavor = "multi_thread")]
async fn me_is_not_cached() {
let (_, _, user) = TestApp::init().with_user().await;
let response = user.get::<()>("/api/v1/me").await;
response.assert_cache_control("no-store");
}
#[tokio::test(flavor = "multi_thread")]
async fn me_updates_is_not_cached() {
let (_, _, user) = TestApp::init().with_user().await;
let response = user.get::<()>("/api/v1/me/updates").await;
response.assert_cache_control("no-store");
}
#[tokio::test(flavor = "multi_thread")]
async fn me_tokens_is_not_cached() {
let (_, _, user) = TestApp::init().with_user().await;
let response = user.get::<()>("/api/v1/me/tokens").await;
response.assert_cache_control("no-store");
}
#[tokio::test(flavor = "multi_thread")]
async fn me_token_is_not_cached() {
let (_, _, user, token) = TestApp::init().with_token().await;
let url = format!("/api/v1/me/tokens/{}", token.as_model().id);
let response = user.get::<()>(&url).await;
response.assert_cache_control("no-store");
}
#[tokio::test(flavor = "multi_thread")]
async fn me_crate_owner_invitations_is_not_cached() {
let (_, _, user) = TestApp::init().with_user().await;
let response = user.get::<()>("/api/v1/me/crate_owner_invitations").await;
response.assert_cache_control("no-store");
}
#[tokio::test(flavor = "multi_thread")]
async fn private_crate_owner_invitations_is_not_cached() {
let (_, _, user) = TestApp::init().with_user().await;
let id = user.as_model().id;
let url = format!("/api/private/crate_owner_invitations?invitee_id={id}");
let response = user.get::<()>(&url).await;
response.assert_cache_control("no-store");
}
#[tokio::test(flavor = "multi_thread")]
async fn trustpub_github_configs_is_not_cached() {
let (_, _, user) = TestApp::init().with_user().await;
let id = user.as_model().id;
let url = format!("/api/v1/trusted_publishing/github_configs?user_id={id}");
let response = user.get::<()>(&url).await;
response.assert_cache_control("no-store");
}
#[tokio::test(flavor = "multi_thread")]
async fn trustpub_gitlab_configs_is_not_cached() {
let (_, _, user) = TestApp::init().with_user().await;
let id = user.as_model().id;
let url = format!("/api/v1/trusted_publishing/gitlab_configs?user_id={id}");
let response = user.get::<()>(&url).await;
response.assert_cache_control("no-store");
}
#[tokio::test(flavor = "multi_thread")]
async fn crate_following_status_is_not_cached() {
let (app, _, user) = TestApp::init().with_user().await;
let mut conn = app.db_conn().await;
CrateBuilder::new("foo", user.as_model().id)
.expect_build(&mut conn)
.await;
let response = user.get::<()>("/api/v1/crates/foo/following").await;
response.assert_cache_control("no-store");
}
#[tokio::test(flavor = "multi_thread")]
async fn search_with_following_is_not_cached() {
let (_, _, user) = TestApp::init().with_user().await;
let response = user.get::<()>("/api/v1/crates?following=yes").await;
response.assert_cache_control("no-store");
}
#[tokio::test(flavor = "multi_thread")]
async fn admin_list_is_not_cached() {
use crates_io::schema::users;
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
let (app, _, user) = TestApp::init().with_user().await;
let mut conn = app.db_conn().await;
diesel::update(user.as_model())
.set(users::is_admin.eq(true))
.execute(&mut conn)
.await
.unwrap();
let response = user.admin_list::<()>(&user.as_model().username).await;
response.assert_cache_control("no-store");
}
#[tokio::test(flavor = "multi_thread")]
async fn metrics_is_not_cached() {
let (_, anon) = TestApp::init()
.with_config(|config| config.metrics.authorization_token = Some("secret".into()))
.empty()
.await;
let mut request = anon.get_request("/api/private/metrics/service");
request.header("Authorization", "Bearer secret");
let response = anon.run::<()>(request).await;
response.assert_cache_control("no-store");
}
#[tokio::test(flavor = "multi_thread")]
async fn search_without_following_is_cacheable() {
let (_, anon) = TestApp::init().empty().await;
let response = anon.get::<()>("/api/v1/crates").await;
response.assert_no_cache_control();
}
#[tokio::test(flavor = "multi_thread")]
async fn site_metadata_is_cached_for_15_seconds() {
let (_, anon) = TestApp::init().empty().await;
let response = anon.get::<()>("/api/v1/site_metadata").await;
response.assert_cache_control("public, max-age=15");
}
#[tokio::test(flavor = "multi_thread")]
async fn summary_is_cached_for_15_seconds() {
let (_, anon) = TestApp::init().empty().await;
let response = anon.get::<()>("/api/v1/summary").await;
response.assert_cache_control("public, max-age=15");
}
#[tokio::test(flavor = "multi_thread")]
async fn download_varies_on_accept() {
let (_, anon) = TestApp::init().empty().await;
// The default `Accept` header redirects (302) to the crate file.
let response = anon.get::<()>("/api/v1/crates/foo/1.0.0/download").await;
assert_eq!(response.status(), StatusCode::FOUND);
response.assert_redirect_ends_with("/crates/foo/foo-1.0.0.crate");
response.assert_vary(&["accept"]);
// `Accept: application/json` returns a 200 with the URL as JSON.
let mut request = anon.get_request("/api/v1/crates/foo/1.0.0/download");
request.header(header::ACCEPT, "application/json");
let response = anon.run::<()>(request).await;
assert_eq!(response.status(), StatusCode::OK);
assert!(response.json().get("url").is_some());
response.assert_vary(&["accept", "accept-encoding"]);
}
#[tokio::test(flavor = "multi_thread")]
async fn readme_varies_on_accept() {
let (_, anon) = TestApp::init().empty().await;
// The default `Accept` header redirects (302) to the rendered readme.
let response = anon.get::<()>("/api/v1/crates/foo/1.0.0/readme").await;
assert_eq!(response.status(), StatusCode::FOUND);
response.assert_redirect_ends_with("/readmes/foo/foo-1.0.0.html");
response.assert_vary(&["accept"]);
let mut request = anon.get_request("/api/v1/crates/foo/1.0.0/readme");
request.header(header::ACCEPT, "application/json");
let response = anon.run::<()>(request).await;
assert_eq!(response.status(), StatusCode::OK);
assert!(response.json().get("url").is_some());
response.assert_vary(&["accept", "accept-encoding"]);
}
#[tokio::test(flavor = "multi_thread")]
async fn public_endpoint_is_cacheable() {
let (_, anon) = TestApp::init().empty().await;
let response = anon.get::<()>("/api/v1/categories").await;
response.assert_no_cache_control();
// The only `Vary` value is `Accept-Encoding`, added by the compression
// middleware.
response.assert_vary(&["accept-encoding"]);
}
#[tokio::test(flavor = "multi_thread")]
async fn public_endpoint_is_cacheable_for_authenticated_users() {
let (_, _, user) = TestApp::init().with_user().await;
let response = user.get::<()>("/api/v1/categories").await;
response.assert_no_cache_control();
}
#[tokio::test(flavor = "multi_thread")]
async fn missing_immutable_asset_is_not_cached() {
let (_, anon) = TestApp::init().empty().await;
let response = anon.get::<()>("/_app/immutable/missing.js").await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
response.assert_no_cache_control();
}