MCPcopy Create free account
hub / github.com/bytecodealliance/wstd / http_server

Function http_server

test-programs/tests/http_server.rs:5–160  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3
4#[test_log::test]
5fn http_server() -> Result<()> {
6 // Run wasmtime serve.
7 // Enable -Scli because we currently don't have a way to build with the
8 // proxy adapter, so we build with the default adapter.
9 let _serve = test_programs::WasmtimeServe::new(test_programs::HTTP_SERVER)?;
10
11 // Test each path in the server:
12
13 // TEST / http_home
14 // Response body is the hard-coded default
15 let body: String = ureq::get("http://127.0.0.1:8081")
16 .call()?
17 .body_mut()
18 .read_to_string()?;
19 assert_eq!(body, "Hello, wasi:http/proxy world!\n");
20
21 // TEST /wait-response http_wait_response
22 // Sleeps for 1 second, then sends a response with body containing
23 // internally measured sleep time.
24 let start = Instant::now();
25 let body: String = ureq::get("http://127.0.0.1:8081/wait-response")
26 .call()?
27 .body_mut()
28 .read_to_string()?;
29 let duration = start.elapsed();
30 let sleep_report = body
31 .split(' ')
32 .find_map(|s| s.parse::<usize>().ok())
33 .expect("body should print 'slept for 10xx millis'");
34 assert!(
35 sleep_report >= 1000,
36 "should have slept for 1000 or more millis, got {sleep_report}"
37 );
38 assert!(duration >= Duration::from_secs(1));
39
40 // TEST /wait-body http_wait_body
41 // Sends response status and headers, then sleeps for 1 second, then sends
42 // body with internally measured sleep time.
43 // With ureq we can't tell that the response status and headers were sent
44 // with a delay in the body. Additionally, the implementation MAY buffer up the
45 // entire response and body before sending it, though wasmtime does not.
46 let start = Instant::now();
47 let body: String = ureq::get("http://127.0.0.1:8081/wait-body")
48 .call()?
49 .body_mut()
50 .read_to_string()?;
51 let duration = start.elapsed();
52 let sleep_report = body
53 .split(' ')
54 .find_map(|s| s.parse::<usize>().ok())
55 .expect("body should print 'slept for 10xx millis'");
56 assert!(
57 sleep_report >= 1000,
58 "should have slept for 1000 or more millis, got {sleep_report}"
59 );
60 assert!(duration >= Duration::from_secs(1));
61
62 // TEST /stream-body http_stream_body

Callers

nothing calls this directly

Calls 4

getFunction · 0.85
elapsedMethod · 0.80
splitMethod · 0.80
sendMethod · 0.45

Tested by

no test coverage detected