From fad6b7b755b788ca0d9113999faa8fb0d6f89ad0 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Fri, 12 Nov 2021 17:26:25 -0700 Subject: some bullshit thrown together for testing --- tests/config-example/domain.test/index.html | 1 + tests/config-example/dynamic.test/hello-there.txt | 0 tests/config-example/main.rs | 50 +++++++++++++++++++++++ tests/config-example/narchttpd.rhai | 17 ++++++++ tests/config-example/sub.domain.test/index.html | 1 + 5 files changed, 69 insertions(+) create mode 100644 tests/config-example/domain.test/index.html create mode 100644 tests/config-example/dynamic.test/hello-there.txt create mode 100644 tests/config-example/main.rs create mode 100644 tests/config-example/narchttpd.rhai create mode 100644 tests/config-example/sub.domain.test/index.html (limited to 'tests') diff --git a/tests/config-example/domain.test/index.html b/tests/config-example/domain.test/index.html new file mode 100644 index 0000000..f583528 --- /dev/null +++ b/tests/config-example/domain.test/index.html @@ -0,0 +1 @@ +

Hello from domain.test

diff --git a/tests/config-example/dynamic.test/hello-there.txt b/tests/config-example/dynamic.test/hello-there.txt new file mode 100644 index 0000000..e69de29 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, + "

Hello from domain.test

" + ); + assert_eq!( + get("http://alternate-domain.test").await, + "

Hello from domain.test

" + ); + assert_eq!( + get("http://sub.domain.test").await, + "

Hello from sub.domain.test

" + ); + 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 new file mode 100644 index 0000000..c876bc1 --- /dev/null +++ b/tests/config-example/narchttpd.rhai @@ -0,0 +1,17 @@ +http_ports = [1337]; +https_ports = []; + +domain(["domain.test", "alternate-domain.test"], serve_static(#{ + root: "./domain.test", +})); +domain("sub.domain.test", serve_static(#{ + root: "./sub.domain.test", +})); +domain("dynamic.test", proxy_child(#{ + command: |port| "python -m http.server " + port, + in_dir: "./dynamic.test", +})); +domain("function.test", |request| #{ + status: 200, + body: "OK", +}); diff --git a/tests/config-example/sub.domain.test/index.html b/tests/config-example/sub.domain.test/index.html new file mode 100644 index 0000000..9031401 --- /dev/null +++ b/tests/config-example/sub.domain.test/index.html @@ -0,0 +1 @@ +

Hello from sub.domain.test

-- cgit v1.2.3