diff --git a/cli/tests/integration/args.rs b/cli/tests/integration/args.rs new file mode 100644 index 00000000..a9179320 --- /dev/null +++ b/cli/tests/integration/args.rs @@ -0,0 +1,46 @@ +use crate::common::{Test, TestResult}; +use hyper::{body::to_bytes, StatusCode}; + +/// Run a program that tests its args. This checks that we're populating the argument list with the +/// singleton "compute-app" value. +/// Check that an empty response is sent downstream by default. +/// +/// `args.wasm` is a guest program checks its cli args. +#[tokio::test(flavor = "multi_thread")] +async fn empty_ok_response_by_default_after_args() -> TestResult { + let resp = Test::using_fixture("args.wasm").against_empty().await?; + + assert_eq!(resp.status(), StatusCode::OK); + assert!(to_bytes(resp.into_body()) + .await + .expect("can read body") + .to_vec() + .is_empty()); + + Ok(()) +} + +/// Run a program that tests its args. This checks that we're populating the argument list with the +/// singleton "compute-app" value. +/// Check that an empty response is sent downstream by default. +/// +/// `args.wasm` is a guest program checks its cli args. +#[tokio::test(flavor = "multi_thread")] +// TODO: The adapter needs to plumb through support for argument handling. This was removed +// explicitly when we thought we would target the proxy world only, but we'll need it back to +// simplify adapting programs from languages that need a non-empty args list. +#[should_panic] +async fn empty_ok_response_by_default_after_args_component() { + let resp = Test::using_fixture("args.wasm") + .adapt_component() + .against_empty() + .await + .unwrap(); + + assert_eq!(resp.status(), StatusCode::OK); + assert!(to_bytes(resp.into_body()) + .await + .expect("can read body") + .to_vec() + .is_empty()); +} diff --git a/cli/tests/integration/main.rs b/cli/tests/integration/main.rs index 1a3f3e1c..1896211b 100644 --- a/cli/tests/integration/main.rs +++ b/cli/tests/integration/main.rs @@ -1,3 +1,4 @@ +mod args; mod async_io; mod body; mod client_certs; diff --git a/lib/src/component/http_resp.rs b/lib/src/component/http_resp.rs index 4992ee7b..dd3756d4 100644 --- a/lib/src/component/http_resp.rs +++ b/lib/src/component/http_resp.rs @@ -5,7 +5,6 @@ use { cfg_if::cfg_if, http::{HeaderName, HeaderValue}, hyper::http::response::Response, - std::str::FromStr, }; const MAX_HEADER_NAME_LEN: usize = (1 << 16) - 1; @@ -137,6 +136,7 @@ impl http_resp::Host for Session { let _ = (h, name, max_len, cursor); return Err(Error::FatalError("A fatal error occurred in the test-only implementation of header_values_get".to_string()).into()); } else { + use std::str::FromStr; if name.len() > MAX_HEADER_NAME_LEN { return Err(Error::InvalidArgument.into()); } diff --git a/lib/src/execute.rs b/lib/src/execute.rs index fd490953..7654bf18 100644 --- a/lib/src/execute.rs +++ b/lib/src/execute.rs @@ -454,8 +454,10 @@ impl ExecuteCtx { let req = session.downstream_request(); let body = session.downstream_request_body(); - let mut store = ComponentCtx::create_store(&self, session, None, |_| {}) - .map_err(ExecutionError::Context)?; + let mut store = ComponentCtx::create_store(&self, session, None, |ctx| { + ctx.arg("compute-app"); + }) + .map_err(ExecutionError::Context)?; let (compute, _instance) = compute::Compute::instantiate_pre(&mut store, instance_pre) @@ -511,8 +513,10 @@ impl ExecuteCtx { // due to wasmtime limitations, in particular the fact that `Instance` is not `Send`. // However, the fact that the module itself is created within `ExecuteCtx::new` // means that the heavy lifting happens only once. - let mut store = create_store(&self, session, profiler, |_| {}) - .map_err(ExecutionError::Context)?; + let mut store = create_store(&self, session, profiler, |ctx| { + ctx.arg("compute-app"); + }) + .map_err(ExecutionError::Context)?; let instance = instance_pre .instantiate_async(&mut store) diff --git a/lib/wit/deps/fastly/compute.wit b/lib/wit/deps/fastly/compute.wit index a5243038..1f865a33 100644 --- a/lib/wit/deps/fastly/compute.wit +++ b/lib/wit/deps/fastly/compute.wit @@ -1088,6 +1088,7 @@ world compute { import wasi:io/error@0.2.0; import wasi:io/streams@0.2.0; import wasi:random/random@0.2.0; + import wasi:cli/environment@0.2.0; import wasi:cli/stdout@0.2.0; import wasi:cli/stderr@0.2.0; import wasi:cli/stdin@0.2.0; diff --git a/test-fixtures/src/bin/args.rs b/test-fixtures/src/bin/args.rs new file mode 100644 index 00000000..174a70d8 --- /dev/null +++ b/test-fixtures/src/bin/args.rs @@ -0,0 +1,6 @@ +fn main() { + let args = Vec::from_iter(std::env::args()); + + assert_eq!(args.len(), 1); + assert_eq!(args[0], "compute-app"); +}