Skip to content

Commit 6037fe0

Browse files
committed
switch testing to fastly, move error-log in cache middleware
1 parent 261c133 commit 6037fe0

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

src/test/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
test::test_metrics::CollectedMetrics,
1818
web::{
1919
build_axum_app,
20-
cache::{self, TargetCdn},
20+
cache::{self, TargetCdn, X_RLNG_SOURCE_CDN},
2121
headers::{IfNoneMatch, SURROGATE_CONTROL},
2222
page::TemplateData,
2323
},
@@ -108,7 +108,7 @@ impl AxumResponseTestExt for axum::response::Response {
108108
// we emit the wrong cache policy in a handler.
109109
//
110110
// The fastly specifics are tested in web::cache unittests.
111-
assert_cache_headers_eq(self, &cache_policy.render(config, TargetCdn::CloudFront));
111+
assert_cache_headers_eq(self, &cache_policy.render(config, TargetCdn::Fastly));
112112
}
113113

114114
fn error_for_status(self) -> Result<Self>
@@ -248,7 +248,13 @@ impl AxumRouterTestExt for axum::Router {
248248
async fn get(&self, path: &str) -> Result<AxumResponse> {
249249
Ok(self
250250
.clone()
251-
.oneshot(Request::builder().uri(path).body(Body::empty()).unwrap())
251+
.oneshot(
252+
Request::builder()
253+
.uri(path)
254+
.header(X_RLNG_SOURCE_CDN, "fastly")
255+
.body(Body::empty())
256+
.unwrap(),
257+
)
252258
.await?)
253259
}
254260

src/web/cache.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use serde::Deserialize;
1818
use std::{convert::Infallible, sync::Arc};
1919
use tracing::error;
2020

21-
const X_RLNG_SOURCE_CDN: HeaderName = HeaderName::from_static("x-rlng-source-cdn");
21+
pub const X_RLNG_SOURCE_CDN: HeaderName = HeaderName::from_static("x-rlng-source-cdn");
2222

2323
#[derive(Debug, Clone, PartialEq)]
2424
pub struct ResponseCacheHeaders {
@@ -305,7 +305,7 @@ pub(crate) async fn cache_middleware(
305305
// It's totally possible that this policy here then states NO_CACHING,
306306
// or FOREVER_IN_CDN_AND_BROWSER, where we wouln't need the surrogate key.
307307
&cache_headers
308-
} else {
308+
} else if cache_headers.needs_cdn_invalidation {
309309
// This theoretically shouldn't happen, all current crate names would be valid
310310
// for surrogate keys, and the `KrateName` validation matches the crates.io crate
311311
// publish validation.
@@ -314,14 +314,13 @@ pub(crate) async fn cache_middleware(
314314
if resp_status.is_success() || resp_status.is_redirection() {
315315
error!(
316316
name = param.name,
317-
"failed to create surrogate key for crate"
317+
?cache_headers,
318+
"failed to create surrogate key for crate, falling back to NO_CACHING"
318319
);
319320
}
320-
if cache_headers.needs_cdn_invalidation {
321-
&NO_CACHING
322-
} else {
323-
&cache_headers
324-
}
321+
&NO_CACHING
322+
} else {
323+
&cache_headers
325324
}
326325
} else {
327326
debug_assert!(

src/web/rustdoc.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,11 +2098,13 @@ mod test {
20982098
.create()
20992099
.await?;
21002100
let web = env.web_app().await;
2101-
let resp = web
2102-
.assert_redirect("/dummy", "/dummy/latest/dummy/")
2103-
.await?;
2104-
assert_eq!(resp.status(), StatusCode::FOUND);
2105-
assert!(resp.headers().get("Cache-Control").is_none());
2101+
web.assert_redirect_cached(
2102+
"/dummy",
2103+
"/dummy/latest/dummy/",
2104+
CachePolicy::ForeverInCdn,
2105+
env.config(),
2106+
)
2107+
.await?;
21062108
Ok(())
21072109
})
21082110
}
@@ -3032,7 +3034,15 @@ mod test {
30323034
#[test_case("search-1234.js")]
30333035
#[test_case("settings-1234.js")]
30343036
fn fallback_to_root_storage_for_some_js_assets(path: &str) {
3035-
// test workaround for https://github.com/rust-lang/docs.rs/issues/1979
3037+
// tests for two separate things needed to serve old rustdoc content
3038+
// 1. `/{crate}/{version}/asset.js`, where we try to find the assets in the rustdoc archive
3039+
// 2. `/asset.js` where we try to find it in RUSTDOC_STATIC_STORAGE_PREFIX
3040+
//
3041+
// For 2), new builds use the assets from RUSTDOC_STATIC_STORAGE_PREFIX via
3042+
// `/-/rustdoc.static/asset.js`.
3043+
//
3044+
// For 1) I'm actually not sure, new builds don't seem to have these assets.
3045+
// ( the logic is special-cased to `search-` and `settings-` prefixes.)
30363046
async_wrapper(|env| async move {
30373047
env.fake_release()
30383048
.await
@@ -3042,21 +3052,23 @@ mod test {
30423052
.create()
30433053
.await?;
30443054

3055+
const ROOT_ASSET: &str = "normalize-20200403-1.44.0-nightly-74bd074ee.css";
3056+
30453057
let storage = env.async_storage();
3046-
storage.store_one("asset.js", *b"content").await?;
3058+
storage.store_one(ROOT_ASSET, *b"content").await?;
30473059
storage.store_one(path, *b"more_content").await?;
30483060

30493061
let web = env.web_app().await;
30503062

3051-
let response = web.get("/dummy/0.1.0/asset.js").await?;
3063+
let response = web.get(&format!("/dummy/0.1.0/{ROOT_ASSET}")).await?;
30523064
assert_eq!(
30533065
response.status(),
30543066
StatusCode::NOT_FOUND,
30553067
"{:?}",
30563068
response.headers().get("Location"),
30573069
);
30583070

3059-
web.assert_success_and_conditional_get("/asset.js", "content")
3071+
web.assert_success_and_conditional_get(&format!("/{ROOT_ASSET}"), "content")
30603072
.await?;
30613073
web.assert_success_and_conditional_get(&format!("/dummy/0.1.0/{path}"), "more_content")
30623074
.await?;

0 commit comments

Comments
 (0)