Skip to content

Commit

Permalink
Turns out it was never properly working...
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Apr 10, 2024
1 parent 73e8a10 commit 92bfa82
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 45 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions martin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,4 @@ ctor.workspace = true
indoc.workspace = true
insta = { workspace = true, features = ["yaml"] }
pprof.workspace = true
rstest.workspace = true
3 changes: 1 addition & 2 deletions martin/src/args/srv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ pub struct SrvArgs {
pub preferred_encoding: Option<PreferredEncoding>,
}

#[derive(PartialEq, Eq, Default, Debug, Clone, Copy, Serialize, Deserialize, ValueEnum)]
#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize, ValueEnum)]
#[serde(rename_all = "lowercase")]
pub enum PreferredEncoding {
#[serde(alias = "br")]
#[clap(alias("br"))]
Brotli,
#[default]
Gzip,
}

Expand Down
80 changes: 37 additions & 43 deletions martin/src/srv/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ impl<'a> DynTileSource<'a> {

if tile.info.encoding == Encoding::Uncompressed {
let ordered_encodings = match self.preferred_enc {
Some(PreferredEncoding::Gzip) => PREFER_GZIP_ENC,
Some(PreferredEncoding::Brotli) | None => PREFER_BROTLI_ENC,
Some(PreferredEncoding::Gzip) | None => PREFER_GZIP_ENC,
Some(PreferredEncoding::Brotli) => PREFER_BROTLI_ENC,
};

// only apply compression if the content supports it
Expand Down Expand Up @@ -264,58 +264,52 @@ pub fn to_encoding(val: ContentEncoding) -> Option<Encoding> {

#[cfg(test)]
mod tests {
use rstest::rstest;
use tilejson::tilejson;

use super::*;
use crate::srv::server::tests::TestSource;

#[rstest]
#[trace]
#[case(&["gzip", "deflate", "br", "zstd"], None, Encoding::Gzip)]
#[case(&["gzip", "deflate", "br", "zstd"], Some(PreferredEncoding::Brotli), Encoding::Brotli)]
#[case(&["gzip", "deflate", "br", "zstd"], Some(PreferredEncoding::Gzip), Encoding::Gzip)]
#[case(&["br;q=1", "gzip;q=1"], Some(PreferredEncoding::Gzip), Encoding::Gzip)]
#[case(&["gzip;q=1", "br;q=1"], Some(PreferredEncoding::Brotli), Encoding::Brotli)]
#[case(&["gzip;q=1", "br;q=0.5"], Some(PreferredEncoding::Brotli), Encoding::Gzip)]
#[actix_rt::test]
async fn test_encoding_preference() {
let source = TestSource {
async fn test_enc_preference(
#[case] accept_enc: &[&'static str],
#[case] preferred_enc: Option<PreferredEncoding>,
#[case] expected_enc: Encoding,
) {
let sources = TileSources::new(vec![vec![Box::new(TestSource {
id: "test_source",
tj: tilejson! { tiles: vec![] },
data: vec![1_u8, 2, 3],
};
let sources = TileSources::new(vec![vec![Box::new(source)]]);
})]]);

for (accept_encodings, prefered_encoding, result_encoding) in [
(
Some(AcceptEncoding(vec![
"gzip;q=1".parse().unwrap(),
"br;q=1".parse().unwrap(),
])),
Some(PreferredEncoding::Brotli),
Encoding::Brotli,
),
(
Some(AcceptEncoding(vec![
"gzip;q=1".parse().unwrap(),
"br;q=0.5".parse().unwrap(),
])),
Some(PreferredEncoding::Brotli),
Encoding::Gzip,
),
] {
let src = DynTileSource::new(
&sources,
"test_source",
None,
"",
accept_encodings,
prefered_encoding,
None,
)
.unwrap();
let xyz = TileCoord { z: 0, x: 0, y: 0 };
let data = &src.get_tile_content(xyz).await.unwrap().data;
let decoded = match result_encoding {
Encoding::Gzip => decode_gzip(data),
Encoding::Brotli => decode_brotli(data),
_ => panic!("Unexpected encoding"),
};
assert_eq!(vec![1_u8, 2, 3], decoded.unwrap());
}
let accept_enc = Some(AcceptEncoding(
accept_enc.iter().map(|s| s.parse().unwrap()).collect(),
));

let src = DynTileSource::new(
&sources,
"test_source",
None,
"",
accept_enc,
preferred_enc,
None,
)
.unwrap();

let xyz = TileCoord { z: 0, x: 0, y: 0 };
let tile = src.get_tile_content(xyz).await.unwrap();
assert_eq!(tile.info.encoding, expected_enc);
}

#[actix_rt::test]
async fn test_tile_content() {
let non_empty_source = TestSource {
Expand Down

0 comments on commit 92bfa82

Please sign in to comment.