Skip to content

Commit 593604e

Browse files
marvinhagemeistersatyarohith
authored andcommitted
fix: serve handler error with 0 arguments (#23652)
Fixes #23651 Co-authored-by: Satya Rohith <[email protected]>
1 parent 65a7d34 commit 593604e

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

ext/http/00_serve.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -793,9 +793,9 @@ internals.serveHttpOnConnection = serveHttpOnConnection;
793793

794794
function registerDeclarativeServer(exports) {
795795
if (ObjectHasOwn(exports, "fetch")) {
796-
if (typeof exports.fetch !== "function" || exports.fetch.length !== 1) {
796+
if (typeof exports.fetch !== "function") {
797797
throw new TypeError(
798-
"Invalid type for fetch: must be a function with a single parameter",
798+
"Invalid type for fetch: must be a function with a single or no parameter",
799799
);
800800
}
801801
return ({ servePort, serveHost }) => {

tests/integration/serve_tests.rs

+45-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ async fn deno_serve_port_0() {
1414
.arg("serve")
1515
.arg("--port")
1616
.arg("0")
17-
.arg("./serve.ts")
17+
.arg("./serve/port_0.ts")
1818
.stdout_piped()
1919
.spawn()
2020
.unwrap();
2121
let stdout = child.stdout.as_mut().unwrap();
22-
let mut buffer = [0; 50];
22+
let mut buffer = [0; 52];
2323
let _read = stdout.read(&mut buffer).unwrap();
2424
let msg = std::str::from_utf8(&buffer).unwrap();
2525
let port_regex = Regex::new(r"(\d+)").unwrap();
@@ -49,3 +49,46 @@ async fn deno_serve_port_0() {
4949
child.kill().unwrap();
5050
child.wait().unwrap();
5151
}
52+
53+
#[tokio::test]
54+
async fn deno_serve_no_args() {
55+
let mut child = util::deno_cmd()
56+
.current_dir(util::testdata_path())
57+
.arg("serve")
58+
.arg("--port")
59+
.arg("0")
60+
.arg("./serve/no_args.ts")
61+
.stdout_piped()
62+
.spawn()
63+
.unwrap();
64+
let stdout = child.stdout.as_mut().unwrap();
65+
let mut buffer = [0; 52];
66+
let _read = stdout.read(&mut buffer).unwrap();
67+
let msg = std::str::from_utf8(&buffer).unwrap();
68+
let port_regex = Regex::new(r"(\d+)").unwrap();
69+
let port = port_regex.find(msg).unwrap().as_str();
70+
71+
let cert = reqwest::Certificate::from_pem(include_bytes!(
72+
"../testdata/tls/RootCA.crt"
73+
))
74+
.unwrap();
75+
76+
let client = reqwest::Client::builder()
77+
.add_root_certificate(cert)
78+
.http2_prior_knowledge()
79+
.build()
80+
.unwrap();
81+
82+
let res = client
83+
.get(&format!("http://127.0.0.1:{port}"))
84+
.send()
85+
.await
86+
.unwrap();
87+
assert_eq!(200, res.status());
88+
89+
let body = res.text().await.unwrap();
90+
assert_eq!(body, "deno serve with no args in fetch() works!");
91+
92+
child.kill().unwrap();
93+
child.wait().unwrap();
94+
}

tests/testdata/serve/no_args.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
fetch() {
3+
return new Response("deno serve with no args in fetch() works!");
4+
},
5+
};
File renamed without changes.

0 commit comments

Comments
 (0)