From 1d31834de9cbb765fe50720b154e73c9f63084d5 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Wed, 10 Jul 2024 18:32:56 +0200 Subject: [PATCH] Correctly implement new-request. Previously, if the `incoming-body` override was `none` we set the body to empty instead of defaulting to the existing body in `request`. Signed-off-by: Ryan Levick --- crates/spin-test-virt/src/wasi/http.rs | 10 ++++++++-- crates/spin-test-virt/src/wasi/http_helper.rs | 5 ++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/spin-test-virt/src/wasi/http.rs b/crates/spin-test-virt/src/wasi/http.rs index ae9744b..0692b75 100644 --- a/crates/spin-test-virt/src/wasi/http.rs +++ b/crates/spin-test-virt/src/wasi/http.rs @@ -164,7 +164,7 @@ pub struct OutgoingRequest { pub authority: RefCell>, pub path_with_query: RefCell>, pub headers: Fields, - body: Consumable, + pub body: Consumable, } impl exports::types::GuestOutgoingRequest for OutgoingRequest { @@ -259,6 +259,12 @@ impl OutgoingBody { } } +impl From for OutgoingBody { + fn from(i: IncomingBody) -> Self { + Self(i.0) + } +} + impl exports::types::GuestOutgoingBody for OutgoingBody { fn write(&self) -> Result { Ok(io::exports::streams::OutputStream::new( @@ -274,7 +280,7 @@ impl exports::types::GuestOutgoingBody for OutgoingBody { } } -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct IncomingBody(io::Buffer); impl IncomingBody { diff --git a/crates/spin-test-virt/src/wasi/http_helper.rs b/crates/spin-test-virt/src/wasi/http_helper.rs index 5b5ee87..a2dbd30 100644 --- a/crates/spin-test-virt/src/wasi/http_helper.rs +++ b/crates/spin-test-virt/src/wasi/http_helper.rs @@ -34,7 +34,10 @@ impl exports::Guest for Component { authority, path_with_query, headers, - body: body.unwrap_or_else(IncomingBody::empty).into(), + // Either override the body with `incoming_body`, or use the body from the original request + body: body + .map(Into::into) + .unwrap_or_else(|| request.body.unconsume().map(Into::into)), }) }