message: web: add test for getting files too big
diff: @@ -5,6 +5,7 @@ use crate::{db, error::Result, Config};
  use iron::{status, Handler, IronError, IronResult, Request, Response};
  use postgres::Connection;
  +#[derive(Debug)]
  pub(crate) struct File(pub(crate) db::file::Blob);
  impl File {
  @@ -74,6 +75,7 @@ impl Handler for DatabaseFileHandler {
  mod tests {
  use super::*;
  use crate::test::wrapper;
  + use crate::Config;
  use chrono::Utc;
  #[test]
  @@ -101,4 +103,60 @@ mod tests {
  Ok(())
  });
  }
  +
  + #[test]
  + fn test_max_size() {
  + const MAX_SIZE: usize = 1024;
  + const MAX_HTML_SIZE: usize = 128;
  +
  + wrapper(|env| {
  + // Ensure the enviornment is not overriding the configuration.
  + let mut config = Config::from_env().expect("failed to get base config");
  + config.max_file_size = MAX_SIZE;
  + config.max_file_size_html = MAX_HTML_SIZE;
  + env.set_config(config);
  +
  + let db = env.db();
  +
  + db.fake_release()
  + .name("dummy")
  + .version("0.1.0")
  + .rustdoc_file("small.html", &[b'A'; MAX_HTML_SIZE / 2] as &[u8])
  + .rustdoc_file("exact.html", &[b'A'; MAX_HTML_SIZE] as &[u8])
  + .rustdoc_file("big.html", &[b'A'; MAX_HTML_SIZE * 2] as &[u8])
  + .rustdoc_file("small.js", &[b'A'; MAX_SIZE / 2] as &[u8])
  + .rustdoc_file("exact.js", &[b'A'; MAX_SIZE] as &[u8])
  + .rustdoc_file("big.js", &[b'A'; MAX_SIZE * 2] as &[u8])
  + .create()?;
  +
  + let file = |path| {
  + File::from_path(
  + &db.conn(),
  + &format!("rustdoc/dummy/0.1.0/{}", path),
  + &env.config(),
  + )
  + };
  + let assert_len = |len, path| {
  + assert_eq!(len, file(path).unwrap().0.content.len());
  + };
  + let assert_too_big = |path| {
  + file(path)
  + .unwrap_err()
  + .downcast_ref::<std::io::Error>()
  + .and_then(|io| io.get_ref())
  + .and_then(|err| err.downcast_ref::<crate::error::SizeLimitReached>())
  + .is_some()
  + };
  +
  + assert_len(MAX_HTML_SIZE / 2, "small.html");
  + assert_len(MAX_HTML_SIZE, "exact.html");
  + assert_len(MAX_SIZE / 2, "small.js");
  + assert_len(MAX_SIZE, "exact.js");
  +
  + assert_too_big("big.html");
  + assert_too_big("big.js");
  +
  + Ok(())
  + })
  + }
  }
  