Skip to content

Commit

Permalink
use pre-compressed data for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ducaale committed Apr 15, 2022
1 parent a51509f commit 5fbe30e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 34 deletions.
68 changes: 34 additions & 34 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
mod server;

use std::collections::{HashMap, HashSet};
use std::fs::{create_dir_all, read_to_string, File, OpenOptions};
use std::fs::{self, File, OpenOptions};
use std::future::Future;
use std::io::Write;
use std::iter::FromIterator;
Expand Down Expand Up @@ -297,7 +297,7 @@ fn download() {
.arg(server.base_url())
.assert()
.success();
assert_eq!(read_to_string(&outfile).unwrap(), "file contents\n");
assert_eq!(fs::read_to_string(&outfile).unwrap(), "file contents\n");
}

#[test]
Expand Down Expand Up @@ -339,9 +339,12 @@ fn download_generated_filename() {
.assert()
.success();

assert_eq!(read_to_string(dir.path().join("bar.json")).unwrap(), "file");
assert_eq!(
read_to_string(dir.path().join("bar.json-1")).unwrap(),
fs::read_to_string(dir.path().join("bar.json")).unwrap(),
"file"
);
assert_eq!(
fs::read_to_string(dir.path().join("bar.json-1")).unwrap(),
"file"
);
}
Expand All @@ -361,7 +364,10 @@ fn download_supplied_filename() {
.current_dir(&dir)
.assert()
.success();
assert_eq!(read_to_string(dir.path().join("foo.bar")).unwrap(), "file");
assert_eq!(
fs::read_to_string(dir.path().join("foo.bar")).unwrap(),
"file"
);
}

#[test]
Expand All @@ -380,7 +386,7 @@ fn download_supplied_unquoted_filename() {
.assert()
.success();
assert_eq!(
read_to_string(dir.path().join("foo bar baz")).unwrap(),
fs::read_to_string(dir.path().join("foo bar baz")).unwrap(),
"file"
);
}
Expand Down Expand Up @@ -1798,7 +1804,7 @@ fn named_sessions() {
.collect(),
);

let session_content = read_to_string(path_to_session).unwrap();
let session_content = fs::read_to_string(path_to_session).unwrap();

assert_eq!(
serde_json::from_str::<serde_json::Value>(&session_content).unwrap(),
Expand Down Expand Up @@ -1840,7 +1846,7 @@ fn anonymous_sessions() {

server.assert_hits(1);

let session_content = read_to_string(path_to_session).unwrap();
let session_content = fs::read_to_string(path_to_session).unwrap();

assert_eq!(
serde_json::from_str::<serde_json::Value>(&session_content).unwrap(),
Expand Down Expand Up @@ -1886,8 +1892,10 @@ fn anonymous_read_only_session() {
.success();

assert_eq!(
serde_json::from_str::<serde_json::Value>(&read_to_string(session_file.path()).unwrap())
.unwrap(),
serde_json::from_str::<serde_json::Value>(
&fs::read_to_string(session_file.path()).unwrap()
)
.unwrap(),
old_session_content
);
}
Expand Down Expand Up @@ -1916,7 +1924,7 @@ fn session_files_are_created_in_read_only_mode() {
.assert()
.success();

let session_content = read_to_string(path_to_session).unwrap();
let session_content = fs::read_to_string(path_to_session).unwrap();
assert_eq!(
serde_json::from_str::<serde_json::Value>(&session_content).unwrap(),
serde_json::json!({
Expand Down Expand Up @@ -1966,7 +1974,7 @@ fn named_read_only_session() {
"hello": "world"
}
});
create_dir_all(path_to_session.parent().unwrap()).unwrap();
fs::create_dir_all(path_to_session.parent().unwrap()).unwrap();
File::create(&path_to_session).unwrap();
std::fs::write(&path_to_session, old_session_content.to_string()).unwrap();

Expand All @@ -1979,7 +1987,7 @@ fn named_read_only_session() {
.success();

assert_eq!(
serde_json::from_str::<serde_json::Value>(&read_to_string(path_to_session).unwrap())
serde_json::from_str::<serde_json::Value>(&fs::read_to_string(path_to_session).unwrap())
.unwrap(),
old_session_content
);
Expand Down Expand Up @@ -2031,7 +2039,7 @@ fn expired_cookies_are_removed_from_session() {
.assert()
.success();

let session_content = read_to_string(session_file.path()).unwrap();
let session_content = fs::read_to_string(session_file.path()).unwrap();
assert_eq!(
serde_json::from_str::<serde_json::Value>(&session_content).unwrap(),
serde_json::json!({
Expand Down Expand Up @@ -2102,7 +2110,7 @@ fn cookies_override_each_other_in_the_correct_order() {

server.assert_hits(1);

let session_content = read_to_string(session_file.path()).unwrap();
let session_content = fs::read_to_string(session_file.path()).unwrap();
assert_eq!(
serde_json::from_str::<serde_json::Value>(&session_content).unwrap(),
serde_json::json!({
Expand Down Expand Up @@ -2212,7 +2220,7 @@ fn auth_netrc_is_not_persisted_in_session() {

server.assert_hits(1);

let session_content = read_to_string(path_to_session).unwrap();
let session_content = fs::read_to_string(path_to_session).unwrap();
assert_eq!(
serde_json::from_str::<serde_json::Value>(&session_content).unwrap(),
serde_json::json!({
Expand Down Expand Up @@ -2753,10 +2761,7 @@ fn tilde_expanded_in_request_items() {
#[test]
fn gzip() {
let server = server::http(|_req| async move {
let mut e = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
e.write_all(b"Hello world").unwrap();
let compressed_bytes = e.finish().unwrap();

let compressed_bytes = fs::read("./tests/fixtures/responses/hello_world.gz").unwrap();
hyper::Response::builder()
.header("date", "N/A")
.header("content-encoding", "gzip")
Expand All @@ -2770,20 +2775,18 @@ fn gzip() {
.stdout(indoc! {r#"
HTTP/1.1 200 OK
Content-Encoding: gzip
Content-Length: 31
Content-Length: 48
Date: N/A
Hello world
"#});
}

#[test]
fn deflate() {
let server = server::http(|_req| async move {
let mut e = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default());
e.write_all(b"Hello world").unwrap();
let compressed_bytes = e.finish().unwrap();

let compressed_bytes = fs::read("./tests/fixtures/responses/hello_world.zz").unwrap();
hyper::Response::builder()
.header("date", "N/A")
.header("content-encoding", "deflate")
Expand All @@ -2797,26 +2800,22 @@ fn deflate() {
.stdout(indoc! {r#"
HTTP/1.1 200 OK
Content-Encoding: deflate
Content-Length: 19
Content-Length: 20
Date: N/A
Hello world
"#});
}

#[test]
fn brotli() {
let server = server::http(|_req| async move {
let mut buffer = Vec::new();
{
let mut writer = brotli::CompressorWriter::new(&mut buffer, 4096, 11, 22);
writer.write_all(b"Hello world").unwrap();
}

let compressed_bytes = fs::read("./tests/fixtures/responses/hello_world.br").unwrap();
hyper::Response::builder()
.header("date", "N/A")
.header("content-encoding", "br")
.body(buffer.into())
.body(compressed_bytes.into())
.unwrap()
});

Expand All @@ -2826,10 +2825,11 @@ fn brotli() {
.stdout(indoc! {r#"
HTTP/1.1 200 OK
Content-Encoding: br
Content-Length: 15
Content-Length: 17
Date: N/A
Hello world
"#});
}

Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/responses/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Test Fixtures: Compressed Responses

```sh
$ echo "Hello world" > hello_world
$ pigz hello_world # hello_world.gz

$ echo "Hello world" > hello_world
$ pigz -z hello_world # hello_world.zz

$ echo "Hello world" > hello_world
$ brotli hello_world # hello_world.br
```
Binary file added tests/fixtures/responses/hello_world.br
Binary file not shown.
Binary file added tests/fixtures/responses/hello_world.gz
Binary file not shown.
Binary file added tests/fixtures/responses/hello_world.zz
Binary file not shown.

0 comments on commit 5fbe30e

Please sign in to comment.