aboutsummaryrefslogtreecommitdiff
path: root/tests/config-example/main.rs
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2021-11-12 17:26:25 -0700
committerMelody Horn <melody@boringcactus.com>2021-11-12 17:26:25 -0700
commitfad6b7b755b788ca0d9113999faa8fb0d6f89ad0 (patch)
treef6d76660a219fb78bf2e476b2fc5a7430da8ce47 /tests/config-example/main.rs
parent8556590d77ad9e0729c869f75a3aa202d24b96d5 (diff)
downloadnarchttpd-fad6b7b755b788ca0d9113999faa8fb0d6f89ad0.tar.gz
narchttpd-fad6b7b755b788ca0d9113999faa8fb0d6f89ad0.zip
some bullshit thrown together for testing
Diffstat (limited to 'tests/config-example/main.rs')
-rw-r--r--tests/config-example/main.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/config-example/main.rs b/tests/config-example/main.rs
new file mode 100644
index 0000000..ed0f863
--- /dev/null
+++ b/tests/config-example/main.rs
@@ -0,0 +1,50 @@
+use std::env;
+use std::io::Read;
+use std::process::Command;
+
+use hyper::{body::aggregate, body::Buf, Body, Client, Method, Request, Uri};
+
+#[tokio::test]
+async fn main() {
+ let narchttpd_path = env!("CARGO_BIN_EXE_narchttpd");
+ let mut narchttpd = Command::new(narchttpd_path).spawn().unwrap();
+
+ let client = Client::new();
+
+ let get = |uri: &'static str| async {
+ let uri: Uri = uri.parse().unwrap();
+ let mut uri_parts = uri.into_parts();
+ let original_host = uri_parts.authority.take().unwrap();
+ uri_parts.authority = "127.0.0.1:1337".parse().ok();
+ let real_uri = Uri::from_parts(uri_parts).unwrap();
+ let req = Request::builder()
+ .method(Method::GET)
+ .uri(real_uri)
+ .header(hyper::header::HOST, original_host.as_str())
+ .body(Body::empty())
+ .unwrap();
+ let res = client.request(req).await.unwrap();
+ assert!(res.status().is_success());
+ let body = aggregate(res.into_body()).await.unwrap();
+ let mut result = String::new();
+ body.reader().read_to_string(&mut result).unwrap();
+ result
+ };
+
+ assert_eq!(
+ get("http://domain.test").await,
+ "<p>Hello from domain.test</p>"
+ );
+ assert_eq!(
+ get("http://alternate-domain.test").await,
+ "<p>Hello from domain.test</p>"
+ );
+ assert_eq!(
+ get("http://sub.domain.test").await,
+ "<p>Hello from sub.domain.test</p>"
+ );
+ assert!(get("http://dynamic.test").await.contains("hello-there.txt"));
+ assert_eq!(get("http://function.test").await, "OK");
+
+ narchttpd.kill().unwrap();
+}