aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2021-11-12 22:38:39 -0700
committerMelody Horn <melody@boringcactus.com>2021-11-12 22:38:39 -0700
commitf79b75710167088e2031a82a94a8d127fbb16a1f (patch)
treeffbbd167c34864d92b1d1e0a3b40da77e0b06874 /tests
parentfad6b7b755b788ca0d9113999faa8fb0d6f89ad0 (diff)
downloadnarchttpd-f79b75710167088e2031a82a94a8d127fbb16a1f.tar.gz
narchttpd-f79b75710167088e2031a82a94a8d127fbb16a1f.zip
god is dead and we have killed him.
Diffstat (limited to 'tests')
-rw-r--r--tests/config-example/main.rs16
-rw-r--r--tests/config-example/narchttpd.rhai21
-rw-r--r--tests/lib.rs19
3 files changed, 42 insertions, 14 deletions
diff --git a/tests/config-example/main.rs b/tests/config-example/main.rs
index ed0f863..4b02c58 100644
--- a/tests/config-example/main.rs
+++ b/tests/config-example/main.rs
@@ -4,10 +4,18 @@ use std::process::Command;
use hyper::{body::aggregate, body::Buf, Body, Client, Method, Request, Uri};
+#[path = "../lib.rs"]
+mod helpers;
+use helpers::ChildExt;
+
#[tokio::test]
async fn main() {
let narchttpd_path = env!("CARGO_BIN_EXE_narchttpd");
- let mut narchttpd = Command::new(narchttpd_path).spawn().unwrap();
+ let _narchttpd = Command::new(narchttpd_path)
+ .current_dir("tests/config-example")
+ .spawn()
+ .unwrap()
+ .kill_on_drop();
let client = Client::new();
@@ -24,11 +32,11 @@ async fn main() {
.body(Body::empty())
.unwrap();
let res = client.request(req).await.unwrap();
- assert!(res.status().is_success());
+ assert!(res.status().is_success(), "{:?}", res);
let body = aggregate(res.into_body()).await.unwrap();
let mut result = String::new();
body.reader().read_to_string(&mut result).unwrap();
- result
+ result.trim().to_string()
};
assert_eq!(
@@ -45,6 +53,4 @@ async fn main() {
);
assert!(get("http://dynamic.test").await.contains("hello-there.txt"));
assert_eq!(get("http://function.test").await, "OK");
-
- narchttpd.kill().unwrap();
}
diff --git a/tests/config-example/narchttpd.rhai b/tests/config-example/narchttpd.rhai
index c876bc1..7c9185d 100644
--- a/tests/config-example/narchttpd.rhai
+++ b/tests/config-example/narchttpd.rhai
@@ -1,17 +1,20 @@
http_ports = [1337];
https_ports = [];
-domain(["domain.test", "alternate-domain.test"], serve_static(#{
+let sample = serve_static(#{
root: "./domain.test",
-}));
-domain("sub.domain.test", serve_static(#{
+});
+domains["domain.test"] = sample;
+domains["alternate-domain.test"] = sample;
+domains["sub.domain.test"] = serve_static(#{
root: "./sub.domain.test",
-}));
-domain("dynamic.test", proxy_child(#{
- command: |port| "python -m http.server " + port,
+});
+domains["dynamic.test"] = proxy_child(#{
+ command: "python -m http.server 6970",
in_dir: "./dynamic.test",
-}));
-domain("function.test", |request| #{
+ port: 6970,
+});
+domains["function.test"] = |request| #{
status: 200,
body: "OK",
-});
+};
diff --git a/tests/lib.rs b/tests/lib.rs
new file mode 100644
index 0000000..50d4fa7
--- /dev/null
+++ b/tests/lib.rs
@@ -0,0 +1,19 @@
+use std::process::Child;
+
+pub struct KillOnDrop(Child);
+
+impl Drop for KillOnDrop {
+ fn drop(&mut self) {
+ self.0.kill().unwrap();
+ }
+}
+
+pub trait ChildExt {
+ fn kill_on_drop(self) -> KillOnDrop;
+}
+
+impl ChildExt for Child {
+ fn kill_on_drop(self) -> KillOnDrop {
+ KillOnDrop(self)
+ }
+}