Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_steel/tests/dir_redirect.rs

5.2 KiB, 1 run

created by r1870400018:65941, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! A directory asked for without its trailing slash is moved to the slash form, query string kept.
2//!
3//! Served in place, `/invitation` answered 200 with the directory's index, so the one page lived at
4//! two addresses and its relative links (`reader.css`, `../fonts/x.woff2`) resolved against the
5//! wrong directory when it was opened at the shorter one. Every static server answers a 301 to
6//! `/invitation/` instead, and so does Steel now.
7//!
8//! One vhost over a web root holding a directory with an index, a nested one, a directory without
9//! one, and a plain file goes through `handle_https` over TLS. What is held: the redirect and its
10//! exact `Location`, with the query string as it arrived; no loop (the slash form is a 200); a
11//! directory with no index is the 404 it always was, so no redirect points at nothing; a plain file
12//! is not touched; and no request can make the `Location` leave the site, whether by a leading
13//! `//`, a `..`, or a bare `.`.
14//!
15//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
16//! Anthropic Claude
17
18mod vhost_rig;
19
20use vhost_rig::{
21 Rig,
22 Scratch,
23 Site,
24};
25
26use oxedyne_fe2o3_core::prelude::*;
27
28const HOST: &str = "static.test";
29
30#[test]
31fn a_directory_without_its_slash_is_a_301_to_the_slash_form() -> Outcome<()> {
32 let web = res!(Scratch::new("dir_redirect"));
33 res!(web.put("index.html", "<p>home</p>"));
34 res!(web.put("invitation/index.html", "<p>the invitation</p>"));
35 res!(web.put("invitation/reader.css", "p { margin: 0 }"));
36 res!(web.put("docs/guide/index.html", "<p>the guide</p>"));
37 res!(web.put("bare/readme.txt", "no index here"));
38 res!(web.put("file.txt", "a plain file"));
39 let rig = res!(Rig::new(vec![Site::new(HOST, &web.0)]));
40 let runtime = res!(vhost_rig::runtime());
41 runtime.block_on(async {
42 // The directory, with and without a query, and for a HEAD as for a GET.
43 for method in ["GET", "HEAD"] {
44 let r = res!(rig.fetch(HOST, method, "/invitation", &[], "").await);
45 assert_eq!(r.status(), 301, "{} /invitation: {:?}", method, r);
46 assert_eq!(r.field("location").as_deref(), Some("/invitation/"), "{:?}", r);
47 assert!(r.body.is_empty(), "a redirect carries no page: {:?}", r);
48 }
49 let r = res!(rig.fetch(HOST, "GET", "/invitation?a=1&b=two%20words", &[], "").await);
50 assert_eq!(r.status(), 301, "{:?}", r);
51 assert_eq!(r.field("location").as_deref(), Some("/invitation/?a=1&b=two%20words"),
52 "the query string must arrive as it was sent: {:?}", r);
53 let r = res!(rig.fetch(HOST, "GET", "/docs/guide", &[], "").await);
54 assert_eq!(r.status(), 301, "{:?}", r);
55 assert_eq!(r.field("location").as_deref(), Some("/docs/guide/"), "{:?}", r);
56
57 // The slash form is the page, and the redirect does not loop back to itself.
58 let r = res!(rig.fetch(HOST, "GET", "/invitation/", &[], "").await);
59 assert_eq!(r.status(), 200, "{:?}", r);
60 assert!(r.body.contains("the invitation"), "{:?}", r);
61 assert_eq!(r.field("location"), None, "{:?}", r);
62 let r = res!(rig.fetch(HOST, "GET", "/invitation/?x=1", &[], "").await);
63 assert_eq!(r.status(), 200, "{:?}", r);
64 let r = res!(rig.fetch(HOST, "GET", "/", &[], "").await);
65 assert_eq!(r.status(), 200, "{:?}", r);
66 assert!(r.body.contains("home"), "{:?}", r);
67
68 // A directory with no index is not a page, and no redirect points at nothing: it is the
69 // 404 it was, with the slash or without.
70 for path in ["/bare", "/bare/", "/docs", "/docs/"] {
71 let r = res!(rig.fetch(HOST, "GET", path, &[], "").await);
72 assert_eq!(r.status(), 404, "GET {}: {:?}", path, r);
73 assert_eq!(r.field("location"), None, "GET {}: {:?}", path, r);
74 }
75
76 // Files are files.
77 for (path, want) in [("/file.txt", "a plain file"), ("/invitation/reader.css", "margin")] {
78 let r = res!(rig.fetch(HOST, "GET", path, &[], "").await);
79 assert_eq!(r.status(), 200, "GET {}: {:?}", path, r);
80 assert!(r.body.contains(want), "GET {}: {:?}", path, r);
81 }
82
83 // The Location is built from the normalised path, so it names this site and one place on
84 // it, whatever the request spelled: a doubled slash must not make a scheme-relative link,
85 // and a `..` is resolved, not repeated.
86 for (path, want) in [
87 ("//invitation", "/invitation/"),
88 ("///invitation", "/invitation/"),
89 ("/./invitation", "/invitation/"),
90 ("/docs/../invitation", "/invitation/"),
91 ("/docs/guide/../../invitation", "/invitation/"),
92 ("/.", "/"),
93 ] {
94 let r = res!(rig.fetch(HOST, "GET", path, &[], "").await);
95 assert_eq!(r.status(), 301, "GET {}: {:?}", path, r);
96 assert_eq!(r.field("location").as_deref(), Some(want), "GET {}: {:?}", path, r);
97 }
98 // And one that climbs out of the web root is refused, not redirected.
99 let r = res!(rig.fetch(HOST, "GET", "/../invitation", &[], "").await);
100 assert_ne!(r.status(), 301, "{:?}", r);
101 assert_eq!(r.field("location"), None, "{:?}", r);
102 Ok(())
103 })
104}