diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Cargo.toml | 1 | ||||
-rw-r--r-- | tests/integration/mod.rs | 2 | ||||
-rw-r--r-- | tests/integration/serve_tests.rs | 51 | ||||
-rw-r--r-- | tests/testdata/serve.ts | 5 |
4 files changed, 59 insertions, 0 deletions
diff --git a/tests/Cargo.toml b/tests/Cargo.toml index fa633f607..af220aae7 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -52,6 +52,7 @@ hyper-util.workspace = true once_cell.workspace = true os_pipe.workspace = true pretty_assertions.workspace = true +regex.workspace = true serde.workspace = true test_util.workspace = true tokio.workspace = true diff --git a/tests/integration/mod.rs b/tests/integration/mod.rs index 59bf0db37..d35fabc02 100644 --- a/tests/integration/mod.rs +++ b/tests/integration/mod.rs @@ -60,6 +60,8 @@ mod publish; mod repl; #[path = "run_tests.rs"] mod run; +#[path = "serve_tests.rs"] +mod serve; #[path = "shared_library_tests.rs"] mod shared_library_tests; #[path = "task_tests.rs"] diff --git a/tests/integration/serve_tests.rs b/tests/integration/serve_tests.rs new file mode 100644 index 000000000..92b0576ed --- /dev/null +++ b/tests/integration/serve_tests.rs @@ -0,0 +1,51 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +use std::io::Read; + +use deno_fetch::reqwest; +use pretty_assertions::assert_eq; +use regex::Regex; +use test_util as util; + +#[tokio::test] +async fn deno_serve_port_0() { + let mut child = util::deno_cmd() + .current_dir(util::testdata_path()) + .arg("serve") + .arg("--port") + .arg("0") + .arg("./serve.ts") + .stdout_piped() + .spawn() + .unwrap(); + let stdout = child.stdout.as_mut().unwrap(); + let mut buffer = [0; 50]; + let _read = stdout.read(&mut buffer).unwrap(); + let msg = std::str::from_utf8(&buffer).unwrap(); + let port_regex = Regex::new(r"(\d+)").unwrap(); + let port = port_regex.find(msg).unwrap().as_str(); + + let cert = reqwest::Certificate::from_pem(include_bytes!( + "../testdata/tls/RootCA.crt" + )) + .unwrap(); + + let client = reqwest::Client::builder() + .add_root_certificate(cert) + .http2_prior_knowledge() + .build() + .unwrap(); + + let res = client + .get(&format!("http://127.0.0.1:{port}")) + .send() + .await + .unwrap(); + assert_eq!(200, res.status()); + + let body = res.text().await.unwrap(); + assert_eq!(body, "deno serve --port 0 works!"); + + child.kill().unwrap(); + child.wait().unwrap(); +} diff --git a/tests/testdata/serve.ts b/tests/testdata/serve.ts new file mode 100644 index 000000000..cdd8476eb --- /dev/null +++ b/tests/testdata/serve.ts @@ -0,0 +1,5 @@ +export default { + fetch(_req: Request) { + return new Response("deno serve --port 0 works!"); + }, +}; |