memory-serve 2.4.0

Fast static file serving for axum web applications
Documentation
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
#![allow(clippy::needless_doctest_main)]
#![doc = include_str!("../README.md")]
use axum::{
    http::{HeaderMap, StatusCode},
    routing::get,
};
use std::future::ready;
use tracing::{info, warn};

mod asset;
mod build;
mod cache_control;
mod load;
mod manifest;
mod options;
mod util;

use crate::util::route::hashed_route;
pub use crate::{
    asset::Asset,
    build::{assets_to_code, load_directory, load_directory_with_embed, load_names_directories},
    cache_control::CacheControl,
    manifest::Manifest,
};

/// Helper struct to create and configure an axum router to serve static
/// files from memory.
#[derive(Debug, Default)]
pub struct MemoryServe {
    options: options::ServeOptions,
    assets: &'static [Asset],
    aliases: Vec<(&'static str, &'static str)>,
}

impl MemoryServe {
    /// Initiate a `MemoryServe` instance, taking the output of the `load!`
    /// macro as an argument. `load!` selects the assets prepared during the
    /// build step.
    pub fn new(assets: &'static [Asset]) -> Self {
        Self {
            assets,
            ..Default::default()
        }
    }

    /// Which static file to serve on the route "/" (the index)
    /// The path (or route) should be relative to the asset directory passed
    /// to `load_directory` (or similar) in your `build.rs`, but prepended with
    /// a slash.
    /// By default this is `Some("/index.html")`
    pub fn index_file(mut self, index_file: Option<&'static str>) -> Self {
        self.options.index_file = index_file;

        self
    }

    /// Whether to serve the corresponding index.html file when a route
    /// matches a subdirectory
    pub fn index_on_subdirectories(mut self, enable: bool) -> Self {
        self.options.index_on_subdirectories = enable;

        self
    }

    /// Which static file to serve when no other routes are matched, also see
    /// [fallback](https://docs.rs/axum/latest/axum/routing/struct.Router.html#method.fallback)
    /// The path (or route) should be relative to the asset directory passed
    /// to `load_directory` (or similar) in your `build.rs`, but prepended with
    /// a slash.
    /// By default this is `None`, which means axum will return an empty
    /// response with a HTTP 404 status code when no route matches.
    pub fn fallback(mut self, fallback: Option<&'static str>) -> Self {
        self.options.fallback = fallback;

        self
    }

    /// What HTTP status code to return when a static file is returned by the
    /// fallback handler.
    pub fn fallback_status(mut self, fallback_status: StatusCode) -> Self {
        self.options.fallback_status = fallback_status;

        self
    }

    /// Whether to enable gzip compression. When set to `true`, clients that
    /// accept gzip compressed files, but not brotli compressed files,
    /// are served gzip compressed files.
    pub fn enable_gzip(mut self, enable_gzip: bool) -> Self {
        self.options.enable_gzip = enable_gzip;

        self
    }

    /// Whether to enable brotli compression. When set to `true`, clients that
    /// accept brotli compressed files are served brotli compressed files.
    pub fn enable_brotli(mut self, enable_brotli: bool) -> Self {
        self.options.enable_brotli = enable_brotli;

        self
    }

    /// Whether to enable clean URLs. When set to `true`, the routing path for
    /// HTML files will not include the extension so that a file located at
    /// "/about.html" maps to "/about" instead of "/about.html".
    pub fn enable_clean_url(mut self, enable_clean_url: bool) -> Self {
        self.options.enable_clean_url = enable_clean_url;

        self
    }

    /// The Cache-Control header to set for HTML files.
    /// See [Cache control](index.html#cache-control) for options.
    pub fn html_cache_control(mut self, html_cache_control: CacheControl) -> Self {
        self.options.html_cache_control = html_cache_control;

        self
    }

    /// The Cache-Control header to set for non-HTML files.
    /// See [Cache control](index.html#cache-control) for options.
    pub fn cache_control(mut self, cache_control: CacheControl) -> Self {
        self.options.cache_control = cache_control;

        self
    }

    /// Create an alias for a route / file
    pub fn add_alias(mut self, from: &'static str, to: &'static str) -> Self {
        self.aliases.push((from, to));

        self
    }

    /// Whether to also serve non-HTML assets on a cache-busted route containing
    /// a content hash, e.g. `/assets/index.3f9a1c2b7d84e6a0.css`. Embedded
    /// assets are served there with [`CacheControl::Long`].
    /// Use [`MemoryServe::manifest`] to look up the hashed routes.
    /// See [Cache busting](index.html#cache-busting).
    /// By default this is `false`.
    pub fn enable_hashed_routes(mut self, enable_hashed_routes: bool) -> Self {
        self.options.enable_hashed_routes = enable_hashed_routes;

        self
    }

    /// Create a [`Manifest`] mapping each asset route to the route it is served
    /// on: the hashed route when [`MemoryServe::enable_hashed_routes`] is set
    /// (except for HTML), the plain route otherwise.
    /// Dynamically served assets are hashed by reading the file, so call this
    /// once at startup and keep the result.
    pub fn manifest(&self) -> Manifest {
        let routes = self
            .assets
            .iter()
            .map(|asset| {
                let served = self
                    .hashed_route(asset)
                    .unwrap_or_else(|| asset.route.to_string());

                (asset.route, served)
            })
            .collect();

        Manifest::new(routes)
    }

    /// The hashed route for a non-HTML asset, if enabled and hashable.
    fn hashed_route(&self, asset: &Asset) -> Option<String> {
        if !self.options.enable_hashed_routes || asset.is_html() {
            return None;
        }

        let hash = asset.content_hash()?;

        Some(hashed_route(asset.route, &hash))
    }

    /// Create an axum `Router` instance that will serve the included static assets
    /// Caution! This method leaks memory. It should only be called once (at startup).
    pub fn into_router<S>(self) -> axum::Router<S>
    where
        S: Clone + Send + Sync + 'static,
    {
        let mut router = axum::Router::new();
        let options: &'static options::ServeOptions = Box::leak(Box::new(self.options));

        // hashed routes are immutable, so embedded assets get a long cache
        let hashed_options: &'static options::ServeOptions =
            Box::leak(Box::new(options::ServeOptions {
                cache_control: CacheControl::Long,
                ..*options
            }));

        // Warn about configuration that silently does nothing because it
        // references a route that no asset provides.
        let route_exists = |route: &str| self.assets.iter().any(|a| a.route == route);

        if let Some(index) = options.index_file
            && !route_exists(index)
        {
            warn!("index_file {index} does not match any asset route, \"/\" will not be served");
        }
        if let Some(fallback) = options.fallback
            && !route_exists(fallback)
        {
            warn!("fallback {fallback} does not match any asset route, it will be ignored");
        }
        for (from, to) in self.aliases.iter() {
            if !route_exists(to) {
                warn!("alias {from} points to {to}, which does not match any asset route");
            }
        }

        for asset in self.assets {
            let (uncompressed_bytes, brotli_bytes, gzip_bytes) = asset.leak_bytes(options);

            if !uncompressed_bytes.is_empty() {
                if !brotli_bytes.is_empty() {
                    info!(
                        "serving {} {} -> {} bytes (compressed)",
                        asset.route,
                        uncompressed_bytes.len(),
                        brotli_bytes.len()
                    );
                } else {
                    info!("serving {} {} bytes", asset.route, uncompressed_bytes.len());
                }
            } else {
                info!("serving {} (dynamically)", asset.route);
            }

            let handler = |headers: HeaderMap| {
                ready(asset.handler(
                    &headers,
                    StatusCode::OK,
                    uncompressed_bytes,
                    brotli_bytes,
                    gzip_bytes,
                    options,
                ))
            };

            if Some(asset.route) == options.fallback {
                info!("serving {} as fallback", asset.route);

                router = router.fallback(|headers: HeaderMap| {
                    ready(asset.handler(
                        &headers,
                        options.fallback_status,
                        uncompressed_bytes,
                        brotli_bytes,
                        gzip_bytes,
                        options,
                    ))
                });
            }

            if let Some(index) = options.index_file {
                if asset.route == index {
                    info!("serving {} as index on /", asset.route);

                    router = router.route("/", get(handler));
                } else if options.index_on_subdirectories && asset.route.ends_with(index) {
                    let path = &asset.route[..asset.route.len() - index.len()];
                    info!("serving {} as index on {}", asset.route, path);

                    router = router.route(path, get(handler));
                }
            }

            let path = if options.enable_clean_url && asset.route.ends_with(".html") {
                &asset.route[..asset.route.len() - 5]
            } else {
                asset.route
            };
            router = router.route(path, get(handler));

            // add all aliases that point to the asset route
            for (from, to) in self.aliases.iter() {
                if *to == asset.route {
                    info!("serving {} on alias {}", asset.route, from);

                    router = router.route(from, get(handler));
                }
            }

            if let Some(hashed) = self.hashed_route(asset) {
                info!("serving {} on hashed route {}", asset.route, hashed);

                // dynamically served files may change, keep the regular cache control
                let hashed_options = if uncompressed_bytes.is_empty() {
                    options
                } else {
                    hashed_options
                };

                router = router.route(
                    &hashed,
                    get(move |headers: HeaderMap| {
                        ready(asset.handler(
                            &headers,
                            StatusCode::OK,
                            uncompressed_bytes,
                            brotli_bytes,
                            gzip_bytes,
                            hashed_options,
                        ))
                    }),
                );
            }
        }

        router
    }
}

#[cfg(test)]
mod tests {
    use axum::{
        Router,
        body::Body,
        http::{
            self, HeaderMap, HeaderName, HeaderValue, Request, StatusCode,
            header::{self, CACHE_CONTROL, CONTENT_ENCODING, CONTENT_LENGTH},
        },
    };
    use std::sync::LazyLock;
    use tower::ServiceExt;

    use crate::{self as memory_serve, Asset, CacheControl, MemoryServe};

    static ASSETS: LazyLock<&'static [Asset]> =
        LazyLock::new(|| memory_serve::build::load_test_assets("./static"));

    macro_rules! test_load {
        () => {{ MemoryServe::new(*ASSETS) }};
    }

    async fn get(
        router: Router,
        path: &str,
        key: &str,
        value: &str,
    ) -> (StatusCode, HeaderMap<HeaderValue>) {
        let response = router
            .oneshot(
                Request::builder()
                    .method(http::Method::GET)
                    .header(key, value)
                    .uri(path)
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        (response.status(), response.headers().to_owned())
    }

    fn get_header<'s>(headers: &'s HeaderMap, name: &HeaderName) -> &'s str {
        headers.get(name).unwrap().to_str().unwrap()
    }

    #[tokio::test]
    async fn test_load_assets() {
        let routes: Vec<&str> = ASSETS.iter().map(|a| a.route).collect();
        let content_types: Vec<&str> = ASSETS.iter().map(|a| a.content_type).collect();
        let etags: Vec<&str> = ASSETS.iter().map(|a| a.etag).collect();

        assert_eq!(
            routes,
            [
                "/about.html",
                "/assets/icon.jpg",
                "/assets/index.css",
                "/assets/index.js",
                "/assets/stars.svg",
                "/blog/index.html",
                "/index.html"
            ]
        );
        assert_eq!(
            content_types,
            [
                "text/html",
                "image/jpeg",
                "text/css",
                "text/javascript",
                "image/svg+xml",
                "text/html",
                "text/html"
            ]
        );
        if cfg!(debug_assertions) && !cfg!(feature = "force-embed") {
            assert_eq!(etags, ["", "", "", "", "", "", ""]);
        } else {
            assert_eq!(
                etags,
                [
                    "56a0dcb83ec56b6c967966a1c06c7b1392e261069d0844aa4e910ca5c1e8cf58",
                    "e64f4683bf82d854df40b7246666f6f0816666ad8cd886a8e159535896eb03d6",
                    "ec4edeea111c854901385011f403e1259e3f1ba016dcceabb6d566316be3677b",
                    "86a7fdfd19700843e5f7344a63d27e0b729c2554c8572903ceee71f5658d2ecf",
                    "bd9dccc152de48cb7bedc35b9748ceeade492f6f904710f9c5d480bd6299cc7d",
                    "89e9873a8e49f962fe83ad2bfe6ac9b21ef7c1b4040b99c34eb783dccbadebc5",
                    "0639dc8aac157b58c74f65bbb026b2fd42bc81d9a0a64141df456fa23c214537"
                ]
            );
        }
    }

    #[tokio::test]
    async fn if_none_match_handling() {
        let memory_router = test_load!().into_router();
        let (code, headers) =
            get(memory_router.clone(), "/index.html", "accept", "text/html").await;
        let etag: &str = headers.get(header::ETAG).unwrap().to_str().unwrap();

        assert_eq!(code, 200);
        assert_eq!(
            etag,
            "\"0639dc8aac157b58c74f65bbb026b2fd42bc81d9a0a64141df456fa23c214537\""
        );

        let (code, headers) = get(memory_router, "/index.html", "If-None-Match", etag).await;
        let length = get_header(&headers, &CONTENT_LENGTH);

        assert_eq!(code, 304);
        assert_eq!(length.parse::<i32>().unwrap(), 0);
    }

    #[tokio::test]
    async fn brotli_compression() {
        let memory_router = test_load!().enable_brotli(true).into_router();
        let (code, headers) = get(
            memory_router.clone(),
            "/index.html",
            "accept-encoding",
            "br",
        )
        .await;
        let encoding = get_header(&headers, &CONTENT_ENCODING);
        let length = get_header(&headers, &CONTENT_LENGTH);

        assert_eq!(code, 200);
        assert_eq!(encoding, "br");
        assert_eq!(length.parse::<i32>().unwrap(), 178);

        // check disable compression
        let memory_router = test_load!().enable_brotli(false).into_router();
        let (code, headers) = get(
            memory_router.clone(),
            "/index.html",
            "accept-encoding",
            "br",
        )
        .await;
        let length: &str = get_header(&headers, &CONTENT_LENGTH);

        assert_eq!(code, 200);
        assert_eq!(length.parse::<i32>().unwrap(), 437);
    }

    #[tokio::test]
    async fn gzip_compression() {
        let memory_router = test_load!().enable_gzip(true).into_router();
        let (code, headers) = get(
            memory_router.clone(),
            "/index.html",
            "accept-encoding",
            "gzip",
        )
        .await;

        let encoding = get_header(&headers, &CONTENT_ENCODING);
        let length = get_header(&headers, &CONTENT_LENGTH);

        assert_eq!(code, 200);
        assert_eq!(encoding, "gzip");
        assert_eq!(length.parse::<i32>().unwrap(), 274);

        // check disable compression
        let memory_router = test_load!().enable_gzip(false).into_router();
        let (code, headers) = get(
            memory_router.clone(),
            "/index.html",
            "accept-encoding",
            "gzip",
        )
        .await;
        let length: &str = get_header(&headers, &CONTENT_LENGTH);

        assert_eq!(code, 200);
        assert_eq!(length.parse::<i32>().unwrap(), 437);
    }

    #[tokio::test]
    async fn encoding_preference() {
        // Both encodings enabled; the client prefers gzip over brotli.
        let memory_router = test_load!()
            .enable_brotli(true)
            .enable_gzip(true)
            .into_router();
        let (code, headers) = get(
            memory_router.clone(),
            "/index.html",
            "accept-encoding",
            "br;q=0.1, gzip;q=1.0",
        )
        .await;
        assert_eq!(code, 200);
        assert_eq!(get_header(&headers, &CONTENT_ENCODING), "gzip");

        // The reverse preference selects brotli.
        let (code, headers) = get(
            memory_router.clone(),
            "/index.html",
            "accept-encoding",
            "br;q=0.8, gzip;q=0.7",
        )
        .await;
        assert_eq!(code, 200);
        assert_eq!(get_header(&headers, &CONTENT_ENCODING), "br");
    }

    #[tokio::test]
    async fn index_file() {
        let memory_router = test_load!().index_file(None).into_router();

        let (code, _) = get(memory_router.clone(), "/", "accept", "*").await;
        assert_eq!(code, 404);

        let memory_router = test_load!().index_file(Some("/index.html")).into_router();

        let (code, _) = get(memory_router.clone(), "/", "accept", "*").await;
        assert_eq!(code, 200);
    }

    #[tokio::test]
    async fn index_file_on_subdirs() {
        let memory_router = test_load!()
            .index_file(Some("/index.html"))
            .index_on_subdirectories(false)
            .into_router();

        let (code, _) = get(memory_router.clone(), "/blog", "accept", "*").await;
        assert_eq!(code, 404);

        let memory_router = test_load!()
            .index_file(Some("/index.html"))
            .index_on_subdirectories(true)
            .into_router();

        let (code, _) = get(memory_router.clone(), "/blog", "accept", "*").await;
        assert_eq!(code, 200);
    }

    #[tokio::test]
    async fn clean_url() {
        let memory_router = test_load!().enable_clean_url(true).into_router();

        let (code, _) = get(memory_router.clone(), "/about.html", "accept", "*").await;
        assert_eq!(code, 404);

        let (code, _) = get(memory_router.clone(), "/about", "accept", "*").await;
        assert_eq!(code, 200);
    }

    #[tokio::test]
    async fn fallback() {
        let memory_router = test_load!().into_router();
        let (code, _) = get(memory_router.clone(), "/foobar", "accept", "*").await;
        assert_eq!(code, 404);

        let memory_router = test_load!().fallback(Some("/index.html")).into_router();
        let (code, headers) = get(memory_router.clone(), "/foobar", "accept", "*").await;
        let length = get_header(&headers, &CONTENT_LENGTH);
        assert_eq!(code, 404);
        assert_eq!(length.parse::<i32>().unwrap(), 437);

        let memory_router = test_load!()
            .fallback(Some("/index.html"))
            .fallback_status(StatusCode::OK)
            .into_router();
        let (code, headers) = get(memory_router.clone(), "/foobar", "accept", "*").await;
        let length = get_header(&headers, &CONTENT_LENGTH);
        assert_eq!(code, 200);
        assert_eq!(length.parse::<i32>().unwrap(), 437);
    }

    #[tokio::test]
    async fn cache_control() {
        async fn check_cache_control(cache_control: CacheControl, expected: &str) {
            let memory_router = test_load!().cache_control(cache_control).into_router();

            let (code, headers) =
                get(memory_router.clone(), "/assets/icon.jpg", "accept", "*").await;

            let cache_control = get_header(&headers, &CACHE_CONTROL);
            assert_eq!(code, 200);
            assert_eq!(cache_control, expected);
        }

        check_cache_control(
            CacheControl::NoCache,
            CacheControl::NoCache.as_header().1.to_str().unwrap(),
        )
        .await;
        check_cache_control(
            CacheControl::Short,
            CacheControl::Short.as_header().1.to_str().unwrap(),
        )
        .await;
        check_cache_control(
            CacheControl::Medium,
            CacheControl::Medium.as_header().1.to_str().unwrap(),
        )
        .await;
        check_cache_control(
            CacheControl::Long,
            CacheControl::Long.as_header().1.to_str().unwrap(),
        )
        .await;

        async fn check_html_cache_control(cache_control: CacheControl, expected: &str) {
            let memory_router = test_load!().html_cache_control(cache_control).into_router();

            let (code, headers) = get(memory_router.clone(), "/index.html", "accept", "*").await;
            let cache_control = get_header(&headers, &CACHE_CONTROL);
            assert_eq!(code, 200);
            assert_eq!(cache_control, expected);
        }

        check_html_cache_control(
            CacheControl::NoCache,
            CacheControl::NoCache.as_header().1.to_str().unwrap(),
        )
        .await;
        check_html_cache_control(
            CacheControl::Short,
            CacheControl::Short.as_header().1.to_str().unwrap(),
        )
        .await;
        check_html_cache_control(
            CacheControl::Medium,
            CacheControl::Medium.as_header().1.to_str().unwrap(),
        )
        .await;
        check_html_cache_control(
            CacheControl::Long,
            CacheControl::Long.as_header().1.to_str().unwrap(),
        )
        .await;
    }

    #[tokio::test]
    async fn hashed_routes() {
        let memory_serve = test_load!().enable_hashed_routes(true);
        let manifest = memory_serve.manifest();
        let memory_router = memory_serve.into_router();

        // non-HTML assets map to a route with a 16 character hex hash before
        // the extension
        let hashed = manifest.get("/assets/index.css").unwrap();
        let hash = hashed
            .strip_prefix("/assets/index.")
            .and_then(|rest| rest.strip_suffix(".css"))
            .unwrap();
        assert_eq!(hash.len(), 16);
        assert!(hash.chars().all(|c| c.is_ascii_hexdigit()));

        if !cfg!(debug_assertions) || cfg!(feature = "force-embed") {
            assert_eq!(hashed, "/assets/index.ec4edeea111c8549.css");
        }

        // HTML files keep their plain route, unknown routes are absent
        assert_eq!(manifest.get("/index.html"), Some("/index.html"));
        assert_eq!(manifest.get("/blog/index.html"), Some("/blog/index.html"));
        assert_eq!(manifest.get("/missing.css"), None);
        assert_eq!(manifest.len(), ASSETS.len());

        // the hashed route serves the same file
        let (code, headers) = get(memory_router.clone(), hashed, "accept", "*").await;
        assert_eq!(code, 200);
        assert_eq!(get_header(&headers, &header::CONTENT_TYPE), "text/css");
        assert_eq!(get_header(&headers, &CONTENT_LENGTH), "1552");

        // embedded assets are immutable on the hashed route, dynamically
        // served assets keep the regular cache control
        let expected_cache_control = if !cfg!(debug_assertions) || cfg!(feature = "force-embed") {
            CacheControl::Long
        } else {
            CacheControl::Medium
        };
        assert_eq!(
            get_header(&headers, &CACHE_CONTROL),
            expected_cache_control.as_header().1.to_str().unwrap()
        );

        // the plain route is still served with the configured cache control
        let (code, headers) = get(memory_router.clone(), "/assets/index.css", "accept", "*").await;
        assert_eq!(code, 200);
        assert_eq!(
            get_header(&headers, &CACHE_CONTROL),
            CacheControl::Medium.as_header().1.to_str().unwrap()
        );

        // every manifest entry resolves
        for (_, served) in manifest.iter() {
            let (code, _) = get(memory_router.clone(), served, "accept", "*").await;
            assert_eq!(code, 200, "{served} should be served");
        }
    }

    #[tokio::test]
    async fn hashed_routes_disabled() {
        let memory_serve = test_load!();
        let manifest = memory_serve.manifest();
        let memory_router = memory_serve.into_router();

        // without hashed routes every asset maps to its plain route
        assert_eq!(manifest.get("/assets/index.css"), Some("/assets/index.css"));
        assert_eq!(manifest.get("/index.html"), Some("/index.html"));
        assert_eq!(manifest.len(), ASSETS.len());

        let (code, _) = get(
            memory_router.clone(),
            "/assets/index.ec4edeea111c8549.css",
            "accept",
            "*",
        )
        .await;
        assert_eq!(code, 404);
    }

    #[tokio::test]
    async fn aliases() {
        let memory_router = test_load!()
            .add_alias("/foobar", "/index.html")
            .add_alias("/baz", "/index.html")
            .into_router();
        let (code, _) = get(memory_router.clone(), "/foobar", "accept", "*").await;
        assert_eq!(code, 200);

        let (code, _) = get(memory_router.clone(), "/baz", "accept", "*").await;
        assert_eq!(code, 200);

        let (code, _) = get(memory_router.clone(), "/index.html", "accept", "*").await;
        assert_eq!(code, 200);

        let (code, _) = get(memory_router.clone(), "/barfoo", "accept", "*").await;
        assert_eq!(code, 404);
    }
}