Skip to content

Commit 0d62047

Browse files
committed
test: add test cases for serve
Signed-off-by: Frank Yang <[email protected]>
1 parent 1368202 commit 0d62047

File tree

5 files changed

+211
-1
lines changed

5 files changed

+211
-1
lines changed

Cargo.lock

Lines changed: 132 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ spin-sdk = { git = "https://github.com/fermyon/spin", rev = "4c6ec40ef8b518c1e90
2323
# The wit-bindgen-rust dependency generates bindings for interfaces.
2424
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "dde4694aaa6acf9370206527a798ac4ba6a8c5b8" }
2525

26+
[dev-dependencies]
27+
wasm-bindgen-test = "0.3.31"
28+
2629
[workspace]

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.PHONY: test
2+
test:
3+
wasm-pack test --node

readme.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,18 @@ Compiling the component:
5858
```shell
5959
$ cargo build --release
6060
```
61+
62+
### Testing
63+
64+
Prerequisites:
65+
66+
- [Rust](https://www.rust-lang.org/) at
67+
[1.56+](https://www.rust-lang.org/tools/install) with the `wasm32-wasi` target
68+
configured
69+
- [wasm-pack](https://github.com/rustwasm/wasm-pack)
70+
71+
Running test cases:
72+
73+
```shell
74+
$ make test
75+
```

src/lib.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const BROTLI_ENCODING: &str = "br";
2727
const PATH_INFO_HEADER: &str = "spin-path-info";
2828

2929
/// Common Content Encodings
30-
#[derive(PartialEq)]
30+
#[derive(PartialEq, Debug)]
3131
pub enum ContentEncoding {
3232
Brotli,
3333
//Deflate, // Could use flate2 for this
@@ -66,6 +66,7 @@ fn serve(req: Request) -> Result<Response> {
6666
.get(PATH_INFO_HEADER)
6767
.expect("PATH_INFO header must be set by the Spin runtime")
6868
.to_str()?;
69+
println!("path: {}", path);
6970
let if_none_match = req
7071
.headers()
7172
.get(IF_NONE_MATCH)
@@ -156,3 +157,59 @@ impl FileServer {
156157
state.finish().to_string()
157158
}
158159
}
160+
161+
#[cfg(test)]
162+
mod tests {
163+
use http::header::ACCEPT_ENCODING;
164+
use wasm_bindgen_test::*;
165+
166+
use super::*;
167+
168+
#[wasm_bindgen_test]
169+
fn test_best_encoding_none() {
170+
let req = http::Request::builder()
171+
.uri("http://thisistest.com")
172+
.body(Some(bytes::Bytes::default()))
173+
.unwrap();
174+
let enc = ContentEncoding::best_encoding(&req).unwrap();
175+
assert_eq!(enc, ContentEncoding::None);
176+
}
177+
178+
#[wasm_bindgen_test]
179+
fn test_best_encoding_not_br() {
180+
let req = http::Request::builder()
181+
.uri("http://thisistest.com")
182+
.header(ACCEPT_ENCODING, "gzip")
183+
.body(Some(bytes::Bytes::default()))
184+
.unwrap();
185+
let enc = ContentEncoding::best_encoding(&req).unwrap();
186+
assert_eq!(enc, ContentEncoding::None);
187+
}
188+
189+
#[wasm_bindgen_test]
190+
fn test_best_encoding_with_br() {
191+
let req = http::Request::builder()
192+
.uri("http://thisistest.com")
193+
.header(ACCEPT_ENCODING, "gzip,br")
194+
.body(Some(bytes::Bytes::default()))
195+
.unwrap();
196+
let enc = ContentEncoding::best_encoding(&req).unwrap();
197+
assert_eq!(enc, ContentEncoding::Brotli);
198+
}
199+
200+
#[wasm_bindgen_test]
201+
fn test_serve_file_not_found() {
202+
let req = spin_http::Request {
203+
method: spin_http::Method::Get,
204+
uri: "http://thisistest.com".to_string(),
205+
headers: vec![(
206+
PATH_INFO_HEADER.to_string(),
207+
"not-existent-file".to_string(),
208+
)],
209+
params: vec![],
210+
body: None,
211+
};
212+
let rsp = <super::SpinHttp as spin_http::SpinHttp>::handle_http_request(req);
213+
assert_eq!(rsp.status, 404);
214+
}
215+
}

0 commit comments

Comments
 (0)