Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add outbound-wasi-http-0.2.0 tests #2

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
conformance-tests
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions components/outbound-wasi-http-v0.2.0/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
.spin/
11 changes: 11 additions & 0 deletions components/outbound-wasi-http-v0.2.0/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "wasi-http-rust-0-2-0"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
url = "2.4.0"
wit-bindgen = { workspace = true }
139 changes: 139 additions & 0 deletions components/outbound-wasi-http-v0.2.0/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
wit_bindgen::generate!({
path: "../../wit",
world: "wasi:http/proxy@0.2.0",
});

use exports::wasi::http0_2_0::incoming_handler;
use url::Url;
use wasi::{
http0_2_0::{
outgoing_handler,
types::{
Headers, IncomingRequest, Method, OutgoingBody, OutgoingRequest, OutgoingResponse,
ResponseOutparam, Scheme,
},
},
io0_2_0::streams::StreamError,
};

struct Component;

export!(Component);

impl incoming_handler::Guest for Component {
fn handle(request: IncomingRequest, outparam: ResponseOutparam) {
// The request must have a "url" header.
let Some(url) = request.headers().entries().iter().find_map(|(k, v)| {
(k == "url")
.then_some(v)
.and_then(|v| std::str::from_utf8(v).ok())
.and_then(|v| Url::parse(v).ok())
}) else {
// Otherwise, return a 400 Bad Request response.
return_response(outparam, 400, b"Bad Request");
return;
};

let headers = Headers::new();
headers
.append(&"Content-Length".into(), &"13".into())
.unwrap();
let outgoing_request = OutgoingRequest::new(headers);
outgoing_request.set_method(&Method::Post).unwrap();
outgoing_request
.set_path_with_query(Some(url.path()))
.unwrap();
outgoing_request
.set_scheme(Some(&match url.scheme() {
"http" => Scheme::Http,
"https" => Scheme::Https,
scheme => Scheme::Other(scheme.into()),
}))
.unwrap();
outgoing_request
.set_authority(Some(url.authority()))
.unwrap();

// Write the request body.
write_outgoing_body(outgoing_request.body().unwrap(), b"Hello, world!");

// Get the incoming response.
let response = match outgoing_handler::handle(outgoing_request, None) {
Ok(r) => r,
Err(e) => {
return_response(outparam, 500, e.to_string().as_bytes());
return;
}
};

let response = loop {
if let Some(response) = response.get() {
break response.unwrap().unwrap();
} else {
response.subscribe().block()
}
};
let incoming_body = response.consume().unwrap();
let incoming_stream = incoming_body.stream().unwrap();
let status = response.status();

// Create the outgoing response from the incoming response.
let response = OutgoingResponse::new(response.headers().clone());
response.set_status_code(status).unwrap();
let outgoing_body = response.body().unwrap();
{
let outgoing_stream = outgoing_body.write().unwrap();
ResponseOutparam::set(outparam, Ok(response));

loop {
match incoming_stream.read(1024) {
Ok(buffer) => {
if buffer.is_empty() {
incoming_stream.subscribe().block();
} else {
outgoing_stream.blocking_write_and_flush(&buffer).unwrap();
}
}
Err(StreamError::Closed) => break,
Err(StreamError::LastOperationFailed(error)) => {
panic!("{}", error.to_debug_string())
}
}
}
// The outgoing stream must be dropped before the outgoing body is finished.
}

OutgoingBody::finish(outgoing_body, None).unwrap();
}
}

fn write_outgoing_body(outgoing_body: OutgoingBody, message: &[u8]) {
{
let outgoing_stream = outgoing_body.write().unwrap();
let mut offset = 0;
loop {
let write = outgoing_stream.check_write().unwrap();
if write == 0 {
outgoing_stream.subscribe().block();
} else {
let count = (write as usize).min(message.len() - offset);
outgoing_stream.write(&message[offset..][..count]).unwrap();
offset += count;
if offset == message.len() {
outgoing_stream.flush().unwrap();
break;
}
}
}
// The outgoing stream must be dropped before the outgoing body is finished.
}
OutgoingBody::finish(outgoing_body, None).unwrap();
}

fn return_response(outparam: ResponseOutparam, status: u16, body: &[u8]) {
let response = OutgoingResponse::new(Headers::new());
response.set_status_code(status).unwrap();
write_outgoing_body(response.body().unwrap(), body);

ResponseOutparam::set(outparam, Ok(response));
}
53 changes: 45 additions & 8 deletions crates/conformance-tests/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use anyhow::Context;

/// The configuration of a conformance test
#[derive(Debug, serde::Deserialize)]
#[derive(Debug, Clone, serde::Deserialize)]
pub struct TestConfig {
pub invocations: Vec<Invocation>,
#[serde(default)]
pub services: Vec<String>,
}

#[derive(Debug, serde::Deserialize)]
#[derive(Debug, Clone, serde::Deserialize)]
#[serde(untagged)]
pub enum Invocation {
Http(HttpInvocation),
}

/// An invocation of the runtime
#[derive(Debug, serde::Deserialize)]
#[derive(Debug, Clone, serde::Deserialize)]
pub struct HttpInvocation {
pub request: Request,
pub response: Response,
Expand All @@ -33,7 +37,7 @@ impl HttpInvocation {
}
}

#[derive(Debug, serde::Deserialize)]
#[derive(Debug, Clone, serde::Deserialize)]
pub struct Request {
#[serde(default)]
pub method: Method,
Expand All @@ -45,6 +49,39 @@ pub struct Request {
}

impl Request {
/// Substitute template variables in the request with well known env variables
///
/// Supported variables:
/// - port: map a well known guest port to the port exposed by the service on the host
pub fn substitute_from_env<R>(
&mut self,
env: &mut test_environment::TestEnvironment<R>,
) -> anyhow::Result<()> {
self.substitute(move |key, value| {
if key != "port" {
anyhow::bail!("unknown template key: {key}")
}
let port = env
.get_port(value.parse().context("port must be a number")?)?
.with_context(|| format!("no port {value} exposed by any service"))?;
Ok(Some(port.to_string()))
})
}

/// Substitute template variables in the request
pub fn substitute(
&mut self,
mut replacement: impl FnMut(&str, &str) -> anyhow::Result<Option<String>>,
) -> anyhow::Result<()> {
for header in &mut self.headers {
test_environment::manifest_template::replace_template(
&mut header.value,
&mut replacement,
)?;
}
Ok(())
}

/// Send the request
pub fn send<F>(self, send: F) -> anyhow::Result<test_environment::http::Response>
where
Expand All @@ -70,7 +107,7 @@ impl Request {
}
}

#[derive(Debug, serde::Deserialize)]
#[derive(Debug, Clone, serde::Deserialize)]
pub struct Response {
#[serde(default = "default_response_status")]
pub status: u16,
Expand All @@ -82,21 +119,21 @@ fn default_response_status() -> u16 {
200
}

#[derive(Debug, serde::Deserialize)]
#[derive(Debug, Clone, serde::Deserialize)]
pub struct RequestHeader {
pub name: String,
pub value: String,
}

#[derive(Debug, serde::Deserialize)]
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ResponseHeader {
pub name: String,
pub value: Option<String>,
#[serde(default)]
pub optional: bool,
}

#[derive(Debug, serde::Deserialize, Default)]
#[derive(Debug, Clone, serde::Deserialize, Default)]
pub enum Method {
#[default]
GET,
Expand Down
10 changes: 7 additions & 3 deletions crates/conformance-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn tests(tests_dir: &Path) -> anyhow::Result<impl Iterator<Item = Test>> {
let config = r#try!(json5::from_str::<config::TestConfig>(&config)
.context("test config could not be parsed"));

let component_name = format!("{name}.wasm");
let component_name = "component.wasm";
kate-goldenring marked this conversation as resolved.
Show resolved Hide resolved
Some(Ok(Test {
name,
config,
Expand All @@ -75,6 +75,7 @@ pub fn tests(tests_dir: &Path) -> anyhow::Result<impl Iterator<Item = Test>> {
Ok(items.into_iter())
}

#[derive(Debug, Clone)]
pub struct Test {
pub name: String,
pub config: config::TestConfig,
Expand All @@ -92,9 +93,12 @@ pub mod assertions {
) -> anyhow::Result<()> {
anyhow::ensure!(
actual.status() == expected.status,
"actual status {} != expected status {}",
"actual status {} != expected status {}\nbody:\n{}",
actual.status(),
expected.status
expected.status,
actual
.text()
.unwrap_or_else(|_| String::from("<invalid utf-8>"))
);

let mut actual_headers = actual
Expand Down
1 change: 1 addition & 0 deletions crates/test-environment/services/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.lock
44 changes: 44 additions & 0 deletions crates/test-environment/src/manifest_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,51 @@ impl EnvTemplate {
Ok(())
}

pub fn substitute_value(
&mut self,
key: &str,
replacement: impl Fn(&str) -> String,
) -> anyhow::Result<()> {
replace_template(&mut self.content, |k, v| {
if k == key {
Ok(Some(replacement(v)))
} else {
Ok(None)
}
})
}

pub fn contents(&self) -> &str {
&self.content
}
}

/// Replace template variables in a string.
///
/// Every time a template is found, the `replacement` function is called with the template key and value.
pub fn replace_template(
content: &mut String,
mut replacement: impl FnMut(&str, &str) -> anyhow::Result<Option<String>>,
) -> Result<(), anyhow::Error> {
let regex = TEMPLATE_REGEX.get_or_init(|| regex::Regex::new(r"%\{(.*?)\}").unwrap());
'outer: loop {
'inner: for captures in regex.captures_iter(content) {
Comment on lines +97 to +98
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be less confusing if you break out the iterator, e.g.

Suggested change
'outer: loop {
'inner: for captures in regex.captures_iter(content) {
'outer: loop {
let mut captures_iter = regex.captures_iter(content);
'inner: for captures in captures_iter {

let (Some(full), Some(capture)) = (captures.get(0), captures.get(1)) else {
continue 'inner;
};
let template = capture.as_str();
let (template_key, template_value) = template.split_once('=').with_context(|| {
format!("invalid template '{template}'(template should be in the form $KEY=$VALUE)")
})?;
let (template_key, template_value) = (template_key.trim(), template_value.trim());
if let Some(replacement) = replacement(template_key, template_value)? {
content.replace_range(full.range(), &replacement);
// Restart the search after a substitution
continue 'outer;
}
}
// Break the outer loop if no substitutions were made
break 'outer;
}
Ok(())
}
Loading