From 81108a24bd067618b8df3c3e15f971c5ea0ac242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Fri, 1 Mar 2024 16:55:58 +0100 Subject: [PATCH] Example WASM module that reverse the text from HTTP request --- README.md | 7 ++-- examples/modules/reverse-text/Cargo.lock | 7 ++++ examples/modules/reverse-text/Cargo.toml | 3 +- examples/modules/reverse-text/src/lib.rs | 45 ++++++++++++++++++------ 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index c7f871f6..72540cf5 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,10 @@ -# Knative Sample Controller +# Knative WASM [![GoDoc](https://godoc.org/github.com/cardil/knative-serving-wasm?status.svg)](https://godoc.org/github.com/cardil/knative-serving-wasm) [![Go Report Card](https://goreportcard.com/badge/cardil/knative-serving-wasm)](https://goreportcard.com/report/cardil/knative-serving-wasm) -Knative `knative-serving-wasm` defines a few simple resources that are validated by -webhook and managed by a controller to demonstrate the canonical style in which -Knative writes controllers. +The `knative-serving-wasm` allows to run WASM modules that implement the +[wasi-http](https://github.com/WebAssembly/wasi-http) interface on Kubernetes. To learn more about Knative, please visit our [Knative docs](https://github.com/knative/docs) repository. diff --git a/examples/modules/reverse-text/Cargo.lock b/examples/modules/reverse-text/Cargo.lock index 2b6123c1..36170502 100644 --- a/examples/modules/reverse-text/Cargo.lock +++ b/examples/modules/reverse-text/Cargo.lock @@ -99,6 +99,7 @@ name = "reverse-text" version = "0.1.0" dependencies = [ "querystring", + "urlencoding", "wasi", ] @@ -189,6 +190,12 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" +[[package]] +name = "urlencoding" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" + [[package]] name = "wasi" version = "0.12.1+wasi-0.2.0" diff --git a/examples/modules/reverse-text/Cargo.toml b/examples/modules/reverse-text/Cargo.toml index d7abd0e6..6553d214 100644 --- a/examples/modules/reverse-text/Cargo.toml +++ b/examples/modules/reverse-text/Cargo.toml @@ -6,7 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -querystring = "1.1.0" +querystring = "1.1" +urlencoding = "2.1" [dependencies.wasi] git = "https://github.com/bytecodealliance/wasi" diff --git a/examples/modules/reverse-text/src/lib.rs b/examples/modules/reverse-text/src/lib.rs index 5b29381a..c9bef2d1 100644 --- a/examples/modules/reverse-text/src/lib.rs +++ b/examples/modules/reverse-text/src/lib.rs @@ -27,24 +27,49 @@ impl exports::wasi::http::incoming_handler::Guest for Reverse { } /** - Get query parameter named "text", or return "Hello, WASI!" if - it's not present -*/ +Get query parameter named "text", or return "Hello, WASI!" if +it's not present + */ fn fetch_text_query_param(pq: String) -> String { - pq.split_once("?") + urlencoding::decode(&pq) + .unwrap() + .split_once("?") .and_then(|(_, s)| { - return Some(querystring::querify(s)) + return Some(querystring::querify(s)); }) .map(|q| { HashMap::from_iter(q) }) - .map(|q| { - q.get("text") + .and_then(|q: HashMap<&str, &str>| { + q.get("text").cloned() }) - .unwrap_or(Some("Hello, WASI!".to_string())) - .unwrap() + .map(|s| { + s.to_string() + }) + .unwrap_or("Hello, WASI!".to_string()) } fn reverse_text(str: String) -> String { str.chars().rev().collect() -} \ No newline at end of file +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_fetch_text_query_param() { + assert_eq!(fetch_text_query_param("".to_string()), "Hello, WASI!"); + assert_eq!(fetch_text_query_param("?".to_string()), "Hello, WASI!"); + assert_eq!(fetch_text_query_param("?text=Hello".to_string()), "Hello"); + assert_eq!(fetch_text_query_param("?text=Happy%20testing".to_string()), "Happy testing"); + } + + #[test] + fn test_reverse_text() { + assert_eq!(reverse_text("".to_string()), ""); + assert_eq!(reverse_text("a".to_string()), "a"); + assert_eq!(reverse_text("ab".to_string()), "ba"); + assert_eq!(reverse_text("abc".to_string()), "cba"); + } +}