Skip to content

Commit 96516cd

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

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.PHONY: test
2+
test: lint test-unit
3+
4+
.PHONY: lint
5+
lint:
6+
cargo clippy --all-targets --all-features -- -D warnings
7+
cargo fmt --all -- --check
8+
9+
.PHONY: test-unit
10+
test-unit:
11+
RUST_LOG=$(LOG_LEVEL) cargo test --all --no-fail-fast --target=$$(rustc -vV | sed -n 's|host: ||p') -- --nocapture --include-ignored

readme.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,17 @@ 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+
70+
Running test cases:
71+
72+
```shell
73+
$ make test
74+
```

src/lib.rs

Lines changed: 113 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
@@ -156,3 +156,115 @@ impl FileServer {
156156
state.finish().to_string()
157157
}
158158
}
159+
160+
#[cfg(test)]
161+
mod tests {
162+
use http::header::{ACCEPT_ENCODING, IF_NONE_MATCH};
163+
164+
use super::*;
165+
166+
#[test]
167+
fn test_best_encoding_none() {
168+
let req = http::Request::builder()
169+
.uri("http://thisistest.com")
170+
.body(Some(bytes::Bytes::default()))
171+
.unwrap();
172+
let enc = ContentEncoding::best_encoding(&req).unwrap();
173+
assert_eq!(enc, ContentEncoding::None);
174+
}
175+
176+
#[test]
177+
fn test_best_encoding_not_br() {
178+
let req = http::Request::builder()
179+
.uri("http://thisistest.com")
180+
.header(ACCEPT_ENCODING, "gzip")
181+
.body(Some(bytes::Bytes::default()))
182+
.unwrap();
183+
let enc = ContentEncoding::best_encoding(&req).unwrap();
184+
assert_eq!(enc, ContentEncoding::None);
185+
}
186+
187+
#[test]
188+
fn test_best_encoding_with_br() {
189+
let req = http::Request::builder()
190+
.uri("http://thisistest.com")
191+
.header(ACCEPT_ENCODING, "gzip,br")
192+
.body(Some(bytes::Bytes::default()))
193+
.unwrap();
194+
let enc = ContentEncoding::best_encoding(&req).unwrap();
195+
assert_eq!(enc, ContentEncoding::Brotli);
196+
}
197+
198+
#[test]
199+
fn test_serve_file_found() {
200+
let req = spin_http::Request {
201+
method: spin_http::Method::Get,
202+
uri: "http://thisistest.com".to_string(),
203+
headers: vec![(
204+
PATH_INFO_HEADER.to_string(),
205+
"./content/hello-test.txt".to_string(),
206+
)],
207+
params: vec![],
208+
body: None,
209+
};
210+
let rsp = <super::SpinHttp as spin_http::SpinHttp>::handle_http_request(req);
211+
assert_eq!(rsp.status, 200);
212+
}
213+
214+
#[test]
215+
fn test_serve_with_etag() {
216+
let req = spin_http::Request {
217+
method: spin_http::Method::Get,
218+
uri: "http://thisistest.com".to_string(),
219+
headers: vec![
220+
(
221+
PATH_INFO_HEADER.to_string(),
222+
"./content/hello-test.txt".to_string(),
223+
),
224+
(
225+
IF_NONE_MATCH.to_string(),
226+
"13946318585003701156".to_string(),
227+
),
228+
],
229+
params: vec![],
230+
body: None,
231+
};
232+
let rsp = <super::SpinHttp as spin_http::SpinHttp>::handle_http_request(req);
233+
assert_eq!(rsp.status, 304);
234+
}
235+
236+
#[test]
237+
fn test_serve_with_not_matched_etag() {
238+
let req = spin_http::Request {
239+
method: spin_http::Method::Get,
240+
uri: "http://thisistest.com".to_string(),
241+
headers: vec![
242+
(
243+
PATH_INFO_HEADER.to_string(),
244+
"./content/hello-test.txt".to_string(),
245+
),
246+
(IF_NONE_MATCH.to_string(), "".to_string()),
247+
],
248+
params: vec![],
249+
body: None,
250+
};
251+
let rsp = <super::SpinHttp as spin_http::SpinHttp>::handle_http_request(req);
252+
assert_eq!(rsp.status, 200);
253+
}
254+
255+
#[test]
256+
fn test_serve_file_not_found() {
257+
let req = spin_http::Request {
258+
method: spin_http::Method::Get,
259+
uri: "http://thisistest.com".to_string(),
260+
headers: vec![(
261+
PATH_INFO_HEADER.to_string(),
262+
"not-existent-file".to_string(),
263+
)],
264+
params: vec![],
265+
body: None,
266+
};
267+
let rsp = <super::SpinHttp as spin_http::SpinHttp>::handle_http_request(req);
268+
assert_eq!(rsp.status, 404);
269+
}
270+
}

0 commit comments

Comments
 (0)