zenwave 0.7.0

Ergonomic HTTP client framework.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! A lightweight local httpbin replacement, shared by the native test suites
//! and by `tests/fixture-server`, the standalone binary the wasm and workerd
//! lanes call over HTTP.
//!
//! Kept free of `crate::`/`super::` paths beyond the modules it declares so it
//! compiles both inside the test crates and inside the standalone binary.
#![allow(dead_code)]

// The production `futures-io` → `hyper::rt` adapter, shared with the test
// server rather than duplicated.
#[path = "../../src/transport/hyper_io.rs"]
mod hyper_io;

use std::{
    convert::Infallible,
    net::TcpListener,
    sync::{
        Arc, OnceLock,
        atomic::{AtomicUsize, Ordering},
    },
    thread,
    time::Duration,
};

use async_io::Async;
use base64::Engine as _;
use base64::engine::general_purpose::STANDARD as BASE64;
use futures_util::{StreamExt as _, stream};
use http_body_util::{BodyExt as _, Full, StreamBody, combinators::BoxBody};
use hyper::{
    body::{Bytes, Frame, Incoming},
    service::service_fn,
};
use url::Url;

use hyper_io::HyperIo;

/// CORS headers on every response: the browser loads the wasm test page from
/// the wasm-bindgen server on one port and calls the fixture on another, so
/// every request the wasm lanes send is cross-origin.
const CORS_ALLOW_ORIGIN: &str = "Access-Control-Allow-Origin";
/// Lets the page read the fixture's headers back, which the wasm tests need
/// to assert on them.
const CORS_EXPOSE_HEADERS: &str = "Access-Control-Expose-Headers";
/// Answered on a preflight: every method the suite uses.
const CORS_ALLOW_METHODS: &str = "Access-Control-Allow-Methods";
/// Any request header — the tests set arbitrary ones.
const CORS_ALLOW_HEADERS: &str = "Access-Control-Allow-Headers";
/// How long the browser may cache the preflight answer.
const CORS_MAX_AGE: &str = "Access-Control-Max-Age";

/// The method list a preflight advertises.
const CORS_METHODS: &str = "GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS";
/// Seconds a preflight answer stays fresh; long enough for a test run.
const CORS_MAX_AGE_SECS: &str = "600";

/// A parsed request, everything the router needs of it.
struct TestRequest {
    /// The request target, origin-form (`/path?query`).
    target: String,
    headers: Vec<(String, String)>,
    /// The drained request body.
    body: Bytes,
}

/// The response a route produces; hyper serializes and frames it.
struct TestResponse {
    status: u16,
    headers: Vec<(String, String)>,
    body: TestBody,
}

/// How a route's body reaches the wire.
enum TestBody {
    /// The whole body, framed with `Content-Length`.
    Full(Vec<u8>),
    /// One chunk, then a body that never ends: the connection can only
    /// finish when the client abandons it, which is what a test of an
    /// abandoned body needs — the bytes it did not read cannot be
    /// drained, whatever the kernel and hyper buffer.
    Stalled(Vec<u8>),
}

impl From<TestBody> for BoxBody<Bytes, Infallible> {
    fn from(body: TestBody) -> Self {
        match body {
            TestBody::Full(bytes) => Self::new(Full::new(Bytes::from(bytes))),
            TestBody::Stalled(first) => Self::new(StreamBody::new(
                stream::once(async move { Ok(Frame::data(Bytes::from(first))) })
                    .chain(stream::pending()),
            )),
        }
    }
}

impl TestResponse {
    fn with_header(mut self, name: &str, value: impl Into<String>) -> Self {
        self.headers.push((name.to_owned(), value.into()));
        self
    }
}

impl From<TestResponse> for hyper::Response<BoxBody<Bytes, Infallible>> {
    fn from(response: TestResponse) -> Self {
        let mut builder = hyper::Response::builder()
            .status(response.status)
            .header(CORS_ALLOW_ORIGIN, "*")
            .header(CORS_EXPOSE_HEADERS, "*");
        for (name, value) in &response.headers {
            builder = builder.header(name.as_str(), value.as_str());
        }
        builder
            .body(response.body.into())
            .expect("response must build")
    }
}

#[derive(Debug)]
pub struct TestServer {
    base: String,
    /// Connections the accept loop has taken; a pooled client reusing a
    /// connection does not add to it.
    accepted: Arc<AtomicUsize>,
    // Keep the thread alive for the duration of the tests.
    _thread: thread::JoinHandle<()>,
}

/// Return the base URL for the local test server, falling back to an env var
/// override so the tests can target another server if needed.
pub fn httpbin_base() -> String {
    if let Ok(base) = std::env::var("ZENWAVE_TEST_BASE_URL") {
        return base.trim_end_matches('/').to_string();
    }
    test_server().base.clone()
}

/// Build a full URL against the local test server.
pub fn httpbin_uri(path: &str) -> String {
    format!("{}/{}", httpbin_base(), path.trim_start_matches('/'))
}

pub fn test_server() -> &'static TestServer {
    static INSTANCE: OnceLock<TestServer> = OnceLock::new();
    INSTANCE.get_or_init(TestServer::start)
}

impl TestServer {
    /// A dedicated server instance: the shared `test_server()` is used by
    /// tests running in parallel, so accepts on it cannot be attributed
    /// to one test.
    pub fn standalone() -> Self {
        Self::start()
    }

    /// Build a full URL against this server instance.
    pub fn uri(&self, path: &str) -> String {
        format!("{}/{}", self.base, path.trim_start_matches('/'))
    }

    /// Connections accepted so far. Requests served on a pooled
    /// connection do not move it.
    pub fn accepted(&self) -> usize {
        self.accepted.load(Ordering::Relaxed)
    }

    fn start() -> Self {
        Self::serve(TcpListener::bind(("127.0.0.1", 0)).expect("start test server"))
    }

    /// Serve on an already-bound listener: the standalone binary picks the
    /// port its harness asked for before handing the socket over.
    pub fn serve(listener: TcpListener) -> Self {
        let base = format!("http://{}", listener.local_addr().expect("server address"));
        let accepted = Arc::new(AtomicUsize::new(0));
        let counter = Arc::clone(&accepted);
        let thread = thread::spawn(move || run_server(&listener, &counter));

        Self {
            base,
            accepted,
            _thread: thread,
        }
    }
}

/// One thread per connection. A pooled client keeps a connection open for
/// its whole keep-alive lifetime, so dispatching connections through a
/// bounded worker pool can strand a task; dedicating a thread per
/// connection cannot.
fn run_server(listener: &TcpListener, accepted: &AtomicUsize) {
    for stream in listener.incoming() {
        let Ok(stream) = stream else { break };
        accepted.fetch_add(1, Ordering::Relaxed);
        thread::spawn(move || {
            let io = HyperIo(Async::new(stream).expect("async socket wrapper"));
            let conn = hyper::server::conn::http1::Builder::new()
                .keep_alive(true)
                .serve_connection(io, service_fn(route));
            let _ = async_io::block_on(conn);
        });
    }
}

/// Route one request: drain the body so the connection stays aligned for
/// keep-alive reuse, then produce the response.
async fn route(
    request: hyper::Request<Incoming>,
) -> Result<hyper::Response<BoxBody<Bytes, Infallible>>, Infallible> {
    let (parts, body) = request.into_parts();
    let body = body
        .collect()
        .await
        .map_or_else(|_| Bytes::new(), http_body_util::Collected::to_bytes);
    // A preflight asks about the route rather than taking it, so it is
    // answered before routing on any path.
    if parts.method == hyper::Method::OPTIONS {
        return Ok(preflight_response().into());
    }
    let request = TestRequest {
        target: parts
            .uri
            .path_and_query()
            .map_or_else(|| parts.uri.path().to_owned(), ToString::to_string),
        headers: parts
            .headers
            .iter()
            .map(|(name, value)| {
                (
                    name.as_str().to_owned(),
                    String::from_utf8_lossy(value.as_bytes()).into_owned(),
                )
            })
            .collect(),
        body,
    };
    Ok(handle_request(&request).into())
}

fn handle_request(request: &TestRequest) -> TestResponse {
    // The request target only provides the path/query, so prefix with a
    // dummy scheme/host.
    let url = Url::parse(&format!("http://localhost{}", request.target)).unwrap();
    let mut path = url.path().to_string();
    // Some clients send absolute-form URLs; strip the leading host portion.
    if let Some(rest) = path.strip_prefix("//") {
        if let Some(pos) = rest.find('/') {
            path = rest[pos..].to_string();
        } else {
            path = "/".to_string();
        }
    }
    let query = url
        .query_pairs()
        .into_owned()
        .collect::<Vec<(String, String)>>();

    match path.as_str() {
        "/bearer" => {
            if let Some(auth) = header_value(&request.headers, "authorization")
                && auth.to_ascii_lowercase().starts_with("bearer ")
            {
                return text_response(200, "authorized");
            }
            text_response(401, "unauthorized")
        }
        "/headers" => json_response(
            200,
            &serde_json::json!({"headers": headers_object(&request.headers)}),
        ),
        "/cookies" => {
            let cookie_header = header_value(&request.headers, "cookie").unwrap_or_default();
            text_response(200, format!("cookies: {cookie_header}"))
        }
        "/json" => json_response(
            200,
            &serde_json::json!({
                "slideshow": {"title": "httpbin local", "author": "zenwave"}
            }),
        ),
        "/user-agent" => {
            let ua = header_value(&request.headers, "user-agent")
                .unwrap_or_else(|| "zenwave-test-agent".to_string());
            text_response(200, format!("user-agent: {ua}"))
        }
        "/get" => json_response(
            200,
            &serde_json::json!({
                "url": request_url(request),
                "origin": "httpbin",
                "headers": headers_object(&request.headers),
            }),
        ),
        "/post" | "/put" | "/delete" | "/patch" => json_response(
            200,
            &serde_json::json!({
                "result": "ok",
                "server": "httpbin-local",
                // httpbin's echo fields: the raw body, the parsed body (null
                // when it is not JSON), the headers, and the URL as seen.
                "data": String::from_utf8_lossy(&request.body),
                "json": serde_json::from_slice::<serde_json::Value>(&request.body)
                    .unwrap_or(serde_json::Value::Null),
                "headers": headers_object(&request.headers),
                "url": request_url(request),
            }),
        ),
        "/gzip" => bytes_response(200, b"gzip response"),
        // A body that arrives in several reads.
        "/stream" => bytes_response(200, vec![0xA5; 256 * 1024]),
        // A body that never completes; see `TestBody::Stalled`.
        "/stream/stalled" => TestResponse {
            status: 200,
            headers: vec![],
            body: TestBody::Stalled(vec![0x5A; 16 * 1024]),
        },
        "/delay/1" => {
            // Small delay to emulate a slow endpoint.
            thread::sleep(Duration::from_millis(10));
            text_response(200, "delayed")
        }
        "/html" => text_response(200, "<html><body>not json</body></html>"),
        _ => {
            if let Some(stripped) = path.strip_prefix("/basic-auth/") {
                return handle_basic_auth(request, stripped);
            }
            if let Some(stripped) = path.strip_prefix("/cookies/set/") {
                return handle_set_cookie(stripped);
            }
            if let Some(stripped) = path.strip_prefix("/status/") {
                return handle_status(stripped);
            }
            if let Some(stripped) = path.strip_prefix("/base64/") {
                return handle_base64(stripped);
            }
            if path.starts_with("/redirect/") {
                return handle_redirect(path.as_str());
            }
            if path == "/redirect-to" {
                return handle_redirect_to(&query);
            }
            text_response(404, format!("no route for {path}"))
        }
    }
}

fn handle_basic_auth(request: &TestRequest, path: &str) -> TestResponse {
    let mut parts = path.split('/');
    let user = parts.next().unwrap_or_default();
    let pass = parts.next().unwrap_or_default();
    let expected = format!("Basic {}", BASE64.encode(format!("{user}:{pass}")));

    if let Some(auth) = header_value(&request.headers, "authorization")
        && auth == expected
    {
        return text_response(200, "authenticated");
    }
    text_response(401, "unauthorized")
}

fn handle_set_cookie(path: &str) -> TestResponse {
    let mut parts = path.split('/');
    let name = parts.next().unwrap_or_default();
    let value = parts.next().unwrap_or_default();
    text_response(200, "cookie set").with_header("Set-Cookie", format!("{name}={value}"))
}

fn handle_status(code: &str) -> TestResponse {
    let status = code.parse::<u16>().unwrap_or(400);
    if status == 204 {
        return TestResponse {
            status,
            headers: vec![],
            body: TestBody::Full(Vec::new()),
        };
    }
    text_response(status, format!("status {status}"))
}

fn handle_base64(data: &str) -> TestResponse {
    BASE64.decode(data).map_or_else(
        |_| text_response(400, "invalid base64"),
        |bytes| bytes_response(200, bytes),
    )
}

fn handle_redirect(path: &str) -> TestResponse {
    let steps = path
        .trim_start_matches("/redirect/")
        .parse::<i32>()
        .unwrap_or(0);
    if steps <= 0 {
        return text_response(200, "redirect complete");
    }

    let next = format!("/redirect/{}", steps - 1);
    redirect_response(&next)
}

fn handle_redirect_to(query: &[(String, String)]) -> TestResponse {
    let target = query
        .iter()
        .find(|(key, _)| key == "url")
        .map_or("/", |(_, value)| value.as_str());
    redirect_response(target)
}

fn redirect_response(location: &str) -> TestResponse {
    text_response(302, "redirect").with_header("Location", location)
}

/// The CORS preflight answer: `Allow-Origin` and `Expose-Headers` are added
/// to every response by the `From<TestResponse>` conversion. No
/// `Access-Control-Allow-Credentials`: the fixture never authenticates
/// cross-origin requests, and `*` origins cannot be combined with it.
fn preflight_response() -> TestResponse {
    TestResponse {
        status: 204,
        headers: vec![
            (CORS_ALLOW_METHODS.to_owned(), CORS_METHODS.to_owned()),
            (CORS_ALLOW_HEADERS.to_owned(), "*".to_owned()),
            (CORS_MAX_AGE.to_owned(), CORS_MAX_AGE_SECS.to_owned()),
        ],
        body: TestBody::Full(Vec::new()),
    }
}

/// The request headers as a JSON object, the lowercase names hyper parsed to
/// string values — the shape httpbin reports under `headers`.
fn headers_object(headers: &[(String, String)]) -> serde_json::Value {
    headers
        .iter()
        .map(|(name, value)| (name.clone(), serde_json::Value::from(value.as_str())))
        .collect()
}

/// The absolute URL the fixture saw: the `Host` header plus the request
/// target.
fn request_url(request: &TestRequest) -> String {
    let host =
        header_value(&request.headers, "host").expect("an HTTP/1.1 request always carries Host");
    format!("http://{host}{}", request.target)
}

fn header_value(headers: &[(String, String)], name: &str) -> Option<String> {
    headers
        .iter()
        .find(|(field, _)| field.eq_ignore_ascii_case(name))
        .map(|(_, value)| value.clone())
}

fn json_response(status: u16, body: &serde_json::Value) -> TestResponse {
    TestResponse {
        status,
        headers: vec![("Content-Type".to_owned(), "application/json".to_owned())],
        body: TestBody::Full(serde_json::to_vec(body).expect("a JSON value serializes")),
    }
}

fn text_response(status: u16, body: impl Into<String>) -> TestResponse {
    TestResponse {
        status,
        headers: vec![(
            "Content-Type".to_owned(),
            "text/plain; charset=UTF-8".to_owned(),
        )],
        body: TestBody::Full(body.into().into_bytes()),
    }
}

fn bytes_response(status: u16, body: impl Into<Vec<u8>>) -> TestResponse {
    TestResponse {
        status,
        headers: vec![],
        body: TestBody::Full(body.into()),
    }
}