diff --git a/Cargo.lock b/Cargo.lock index 1ed2edd6b716fcad485fe45ec5979810872714df..b084fa9e4b12cfd4b2fc6d40cf3350029df31ddd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -235,6 +235,11 @@ dependencies = [ "unicode-normalization 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "interpolate_idents" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "isatty" version = "0.1.7" @@ -639,6 +644,7 @@ dependencies = [ "docopt 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", "formdata 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)", + "interpolate_idents 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", "rhai 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "rocket 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -858,6 +864,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum httparse 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c2f407128745b78abc95c0ffbe4e5d37427fdc0d45470710cfef8c44522a2e37" "checksum hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)" = "368cb56b2740ebf4230520e2b90ebb0461e69034d85d1945febd9b3971426db2" "checksum idna 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "014b298351066f1512874135335d62a789ffe78a9974f94b43ed5621951eaf7d" +"checksum interpolate_idents 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c038526b1556151b78f71b3e4cb107cf58c4dfc426a64a398c61f76a42a4e08" "checksum isatty 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a118a53ba42790ef25c82bb481ecf36e2da892646cccd361e69a6bb881e19398" "checksum itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c069bbec61e1ca5a596166e55dfe4773ff745c3d16b700013bcaff9a6df2c682" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" diff --git a/Cargo.toml b/Cargo.toml index f181189886d045f97ae2617cb6e62dd371954fc7..f92735d5f80859c70b91584235cf589435caeec0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,3 +37,6 @@ hyper = "0.10" rand = "0.3" rhai = "0.7" serde_yaml = "0.7.3" + +[dev-dependencies] +interpolate_idents = "0.2.4" diff --git a/example/files/adorable-puppy.jpg b/example/files/adorable-puppy.jpg new file mode 100644 index 0000000000000000000000000000000000000000..caa08afbae4082a0efccfa24060f06485d8009e9 Binary files /dev/null and b/example/files/adorable-puppy.jpg differ diff --git a/example/files/attribution.csv b/example/files/attribution.csv new file mode 100644 index 0000000000000000000000000000000000000000..66bd3e19ebd286eef77994042c0b72185eaf4cfa --- /dev/null +++ b/example/files/attribution.csv @@ -0,0 +1,3 @@ +file,author/attribution, link +adorable-puppy.jpg,Photo by mentatdgt from Pexels,https://www.pexels.com/photo/white-brown-and-black-shih-tzu-puppy-936317/ +math.aswm,Github: Hanks10100,https://github.com/Hanks10100/wasm-examples diff --git a/example/files/math.wasm b/example/files/math.wasm new file mode 100644 index 0000000000000000000000000000000000000000..4140e01798748e33698b7f0eb0b0ef8c0bcc52df Binary files /dev/null and b/example/files/math.wasm differ diff --git a/example/index.html b/example/index.html index 24ac4ba12acf44383c15860708aaaecbe5a2d922..080c0fc49cfb6a8e4ab7847645034944714928e4 100644 --- a/example/index.html +++ b/example/index.html @@ -3,10 +3,11 @@ <head> <meta charset="UTF-8"> <title>Swerve Example</title> - <link rel="stylesheet" href="/css/styles.css"> + <link rel="stylesheet" href="css/styles.css"> </head> <body> <h1>It's Swervin' Time</h1> <p>This page is part of the swerve example, and includes a stylesheet and stuff.</p> + <script async src="js/say_hello.js"></script> </body> </html> \ No newline at end of file diff --git a/example/js/say_hello.js b/example/js/say_hello.js new file mode 100644 index 0000000000000000000000000000000000000000..d0e38b2bfa1dd85d51824d730d50606e193c6c01 --- /dev/null +++ b/example/js/say_hello.js @@ -0,0 +1,3 @@ +document.addEventListener('DOMContentLoaded', function() { + console.log("The dom has loaded!") +}) \ No newline at end of file diff --git a/tests/basic_operations.rs b/tests/basic_operations.rs new file mode 100644 index 0000000000000000000000000000000000000000..b98a39a44ccf09c06ac705802eea02fa8affaf9e --- /dev/null +++ b/tests/basic_operations.rs @@ -0,0 +1,28 @@ + +extern crate rocket; +extern crate swerve; + +use swerve::cli::{Args, SwerveConfig}; +use swerve::server::create_server; + +use rocket::local::Client; +use rocket::http::Status; + +const INDEX_PAGE: &'static str = include_str!("../example/index.html"); + +#[test] +fn test_serves_index() { + let args = Args { + flag_dir: Some(String::from("example")), + ..Args::default() + }; + let config = SwerveConfig::default(); + + let server = create_server(args, config); + + let client = Client::new(server).expect("valid server instance"); + let mut response = client.get("/").dispatch(); + + assert_eq!(response.status(), Status::Ok); + assert_eq!(response.body_string(), Some(INDEX_PAGE.into())); +} \ No newline at end of file diff --git a/tests/content_types.rs b/tests/content_types.rs new file mode 100644 index 0000000000000000000000000000000000000000..c87a97191b0009fb507996fb9904a8717f126815 --- /dev/null +++ b/tests/content_types.rs @@ -0,0 +1,56 @@ +#![feature(plugin)] +#![plugin(interpolate_idents)] + +extern crate rocket; +extern crate swerve; + +use swerve::cli::{Args, SwerveConfig}; +use swerve::server::create_server; + +use rocket::local::Client; +use rocket::http::ContentType; + +macro_rules! test_type { + ($name:ident, $path:expr, $content_path:expr) => (interpolate_idents! { + #[test] + fn [returns_some_type_for_ $name]() { + let args = Args { + flag_dir: Some(String::from("example")), + flag_quiet: true, + ..Args::default() + }; + let config = SwerveConfig::default(); + + let server = create_server(args, config); + let client = Client::new(server).expect("valid server instance"); + let response = client.get($path).dispatch(); + + assert_eq!(response.content_type(), Some($content_path)); + } + }); + + ($name:ident, $path:expr) => (interpolate_idents! { + #[test] + fn [returns_no_type_for_ $name]() { + let args = Args { + flag_dir: Some(String::from("example")), + flag_quiet: true, + ..Args::default() + }; + let config = SwerveConfig::default(); + + let server = create_server(args, config); + let client = Client::new(server).expect("valid server instance"); + let response = client.get($path).dispatch(); + + assert_eq!(response.content_type(), None); + } + }); +} + +test_type!(html, "/index.html", ContentType::HTML); +test_type!(css, "/css/styles.css", ContentType::CSS); +test_type!(javascript, "/js/say_hello.js", ContentType::JavaScript); +test_type!(csv, "/files/attribution.csv", ContentType::CSV); +test_type!(image_jpeg, "/files/adorable-puppy.jpg", ContentType::JPEG); +test_type!(web_assembly, "/files/math.wasm", ContentType::new("application", "wasm"));