rig-http 0.45.0

Rig's transport contracts: the HTTP client and websocket traits a transport implements, and the wasm-compatible bounds they are written with.
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
use super::*;
use crate::test_utils::RecordingHttpClient;
use futures::executor::block_on;

fn request(body: &'static str) -> Request<&'static str> {
    Request::builder()
        .method(http::Method::POST)
        .uri("https://example.test/v1/echo?x=1")
        .header("x-probe", "yes")
        .body(body)
        .expect("valid request")
}

#[test]
fn unary_request_passes_through_unchanged_and_body_converts() {
    let inner = RecordingHttpClient::new(r#"{"ok":true}"#);
    let erased = DynHttpClient::new(inner.clone());

    let response = block_on(erased.send::<_, Vec<u8>>(request("hello"))).expect("send");
    let body = block_on(response.into_body()).expect("body");
    assert_eq!(body, br#"{"ok":true}"#);

    let captured = inner.requests();
    assert_eq!(captured.len(), 1);
    let req = &captured[0];
    assert_eq!(req.uri, "https://example.test/v1/echo?x=1");
    assert_eq!(req.body, Bytes::from_static(b"hello"));
    assert_eq!(
        req.headers.get("x-probe").and_then(|v| v.to_str().ok()),
        Some("yes")
    );
}

#[test]
fn erasing_an_erased_client_is_a_clone_not_a_second_layer() {
    let erased = DynHttpClient::new(RecordingHttpClient::new(""));
    let again = DynHttpClient::new(erased.clone());
    assert!(erased.ptr_eq(&again));
    assert!(erased.ptr_eq(&erased.clone()));
}

#[test]
fn debug_never_prints_the_inner_transport() {
    let erased = DynHttpClient::new(RecordingHttpClient::new("secret-bearing config"));
    assert_eq!(format!("{erased:?}"), "DynHttpClient");
}

mod middleware {
    use super::*;
    use crate::http_client::middleware::{Exchange, HttpMiddleware};
    use crate::test_utils::MockStreamingClient;
    use crate::wasm_compat::WasmBoxedFuture;
    use http::{HeaderMap, HeaderValue, StatusCode};
    use std::sync::{Arc, Mutex};

    /// Appends its tag to a header, to the body, and to a shared log —
    /// making phase ordering across a stack observable.
    struct Tagger {
        tag: &'static str,
        log: Arc<Mutex<Vec<String>>>,
    }

    impl Tagger {
        fn log(&self, phase: &str) {
            self.log
                .lock()
                .expect("log")
                .push(format!("{}:{}", self.tag, phase));
        }
    }

    impl HttpMiddleware for Tagger {
        fn before_request_headers<'a>(
            &'a self,
            _exchange: Exchange<'a>,
            headers: &'a mut HeaderMap,
        ) -> WasmBoxedFuture<'a, Result<()>> {
            Box::pin(async move {
                self.log("headers");
                headers.append("x-tag", HeaderValue::from_static(self.tag));
                Ok(())
            })
        }

        fn before_request_body<'a>(
            &'a self,
            _exchange: Exchange<'a>,
            headers: &'a HeaderMap,
            body: Bytes,
        ) -> WasmBoxedFuture<'a, Result<Bytes>> {
            Box::pin(async move {
                self.log("body");
                // Every body hook sees the fully mutated headers: its own
                // tag was already appended by the headers phase.
                assert!(
                    headers
                        .get_all("x-tag")
                        .iter()
                        .any(|v| v.as_bytes() == self.tag.as_bytes()),
                    "body hooks run after all header hooks"
                );
                let mut bytes = body.to_vec();
                bytes.extend_from_slice(self.tag.as_bytes());
                Ok(Bytes::from(bytes))
            })
        }

        fn after_response<'a>(
            &'a self,
            _exchange: Exchange<'a>,
            status: StatusCode,
            _headers: &'a HeaderMap,
        ) -> WasmBoxedFuture<'a, Result<()>> {
            Box::pin(async move {
                self.log(&format!("response:{}", status.as_u16()));
                Ok(())
            })
        }
    }

    #[test]
    fn phases_run_in_attachment_order_and_mutations_chain() {
        let inner = RecordingHttpClient::new("ok");
        let log = Arc::new(Mutex::new(Vec::new()));
        let erased = DynHttpClient::new(inner.clone())
            .with_middleware(Tagger {
                tag: "a",
                log: log.clone(),
            })
            .with_middleware(Tagger {
                tag: "b",
                log: log.clone(),
            });

        block_on(erased.send::<_, Bytes>(request("body-"))).expect("send");

        let captured = inner.requests();
        assert_eq!(captured.len(), 1);
        // Header mutations chain in attachment order.
        let tags: Vec<_> = captured[0]
            .headers
            .get_all("x-tag")
            .iter()
            .map(|v| v.to_str().expect("ascii"))
            .collect();
        assert_eq!(tags, ["a", "b"]);
        // Body replacements chain in attachment order.
        assert_eq!(captured[0].body, Bytes::from_static(b"body-ab"));
        // All header hooks run before any body hook; response hooks last.
        assert_eq!(
            log.lock().expect("log").as_slice(),
            [
                "a:headers",
                "b:headers",
                "a:body",
                "b:body",
                "a:response:200",
                "b:response:200"
            ]
        );
    }

    /// A middleware that fails the given phase.
    struct FailAt(&'static str);

    impl HttpMiddleware for FailAt {
        fn before_request_headers<'a>(
            &'a self,
            _exchange: Exchange<'a>,
            _headers: &'a mut HeaderMap,
        ) -> WasmBoxedFuture<'a, Result<()>> {
            let fail = self.0 == "headers";
            Box::pin(async move {
                if fail {
                    return Err(crate::http_client::Error::non_success_with_details(
                        StatusCode::BAD_REQUEST,
                        http::HeaderMap::new(),
                        "rejected headers".into(),
                    ));
                }
                Ok(())
            })
        }

        fn after_response<'a>(
            &'a self,
            _exchange: Exchange<'a>,
            _status: StatusCode,
            _headers: &'a HeaderMap,
        ) -> WasmBoxedFuture<'a, Result<()>> {
            let fail = self.0 == "response";
            Box::pin(async move {
                if fail {
                    return Err(crate::http_client::Error::non_success_with_details(
                        StatusCode::TOO_MANY_REQUESTS,
                        http::HeaderMap::new(),
                        "rejected response".into(),
                    ));
                }
                Ok(())
            })
        }
    }

    #[test]
    fn request_side_failure_aborts_before_sending() {
        let inner = RecordingHttpClient::new("ok");
        let erased = DynHttpClient::new(inner.clone()).with_middleware(FailAt("headers"));
        let Err(err) = block_on(erased.send::<_, Bytes>(request("hello"))) else {
            panic!("must abort")
        };
        assert_eq!(err.non_success_status(), Some(StatusCode::BAD_REQUEST));
        assert!(inner.requests().is_empty(), "nothing reached the transport");
    }

    #[test]
    fn response_hook_failure_surfaces_as_the_request_error() {
        let inner = RecordingHttpClient::new("ok");
        let erased = DynHttpClient::new(inner.clone()).with_middleware(FailAt("response"));
        let Err(err) = block_on(erased.send::<_, Bytes>(request("hello"))) else {
            panic!("must fail")
        };
        assert_eq!(
            err.non_success_status(),
            Some(StatusCode::TOO_MANY_REQUESTS)
        );
        // The request itself was sent; only acceptance was refused.
        assert_eq!(inner.requests().len(), 1);
    }

    #[test]
    fn streaming_response_hook_sees_status_and_headers_before_consumption() {
        let seen = Arc::new(Mutex::new(None));

        struct CaptureContentType(Arc<Mutex<Option<String>>>);
        impl HttpMiddleware for CaptureContentType {
            fn after_response<'a>(
                &'a self,
                _exchange: Exchange<'a>,
                status: StatusCode,
                headers: &'a HeaderMap,
            ) -> WasmBoxedFuture<'a, Result<()>> {
                Box::pin(async move {
                    let content_type = headers
                        .get(http::header::CONTENT_TYPE)
                        .and_then(|v| v.to_str().ok())
                        .map(str::to_owned);
                    *self.0.lock().expect("seen") = Some(format!(
                        "{}:{}",
                        status.as_u16(),
                        content_type.unwrap_or_default()
                    ));
                    Ok(())
                })
            }
        }

        let inner = MockStreamingClient {
            sse_bytes: Bytes::from_static(b"data: hi\n\n"),
        };
        let erased = DynHttpClient::new(inner).with_middleware(CaptureContentType(seen.clone()));
        // The hook has run by the time `send_streaming` resolves — before
        // any of the body stream is polled.
        let response = block_on(erased.send_streaming(request(""))).expect("stream");
        assert_eq!(
            seen.lock().expect("seen").as_deref(),
            Some("200:text/event-stream")
        );
        drop(response);
    }

    /// Records each reply's status and `retry-after` header.
    struct SeeReplies(Arc<Mutex<Vec<String>>>);

    impl HttpMiddleware for SeeReplies {
        fn after_response<'a>(
            &'a self,
            _exchange: Exchange<'a>,
            status: StatusCode,
            headers: &'a HeaderMap,
        ) -> WasmBoxedFuture<'a, Result<()>> {
            Box::pin(async move {
                let wait = headers
                    .get(http::header::RETRY_AFTER)
                    .and_then(|v| v.to_str().ok())
                    .unwrap_or("-");
                self.0
                    .lock()
                    .expect("seen")
                    .push(format!("{}:{wait}", status.as_u16()));
                Ok(())
            })
        }
    }

    #[test]
    fn an_error_reply_reaches_the_response_hooks_and_still_fails_the_request() {
        let mut headers = HeaderMap::new();
        headers.insert(http::header::RETRY_AFTER, HeaderValue::from_static("7"));
        let inner =
            RecordingHttpClient::with_error_headers(StatusCode::TOO_MANY_REQUESTS, "slow", headers);
        let seen = Arc::new(Mutex::new(Vec::new()));
        let erased = DynHttpClient::new(inner)
            .with_middleware(SeeReplies(seen.clone()))
            .with_middleware(FailAt("response"));
        let Err(err) = block_on(erased.send::<_, Bytes>(request("hello"))) else {
            panic!("an error reply fails the request")
        };
        assert_eq!(seen.lock().expect("seen").as_slice(), ["429:7"]);
        assert_eq!(
            err.non_success_body(),
            Some("slow"),
            "the reply's own error"
        );
    }

    /// A transport error that kept no reply, such as a reset connection, has
    /// nothing to show a response hook, so none runs and the request fails
    /// with that error.
    #[test]
    fn a_transport_error_without_a_reply_runs_no_response_hook() {
        let seen = Arc::new(Mutex::new(Vec::new()));
        let erased = DynHttpClient::new(RecordingHttpClient::new(""))
            .with_middleware(SeeReplies(seen.clone()));
        let (parts, body) = request("hello").into_parts();
        let failed = block_on(erased.observe(parts, body, |_| async {
            Err::<Response<Bytes>, _>(crate::http_client::Error::instance(std::io::Error::other(
                "connection reset",
            )))
        }));
        let err = failed.err().map(|err| err.to_string());
        assert!(
            err.is_some_and(|err| err.contains("connection reset")),
            "the transport's error"
        );
        assert!(seen.lock().expect("seen").is_empty());
    }

    /// What a caller attached to a request, such as the call it serves.
    #[derive(Clone, Debug, PartialEq)]
    struct Serves(u64);

    /// Records the [`Serves`] each phase's exchange carries.
    struct SeeServes(Arc<Mutex<Vec<String>>>);

    impl SeeServes {
        fn note(&self, phase: &str, exchange: Exchange<'_>) {
            let serves = exchange.extensions.get::<Serves>().map(|serves| serves.0);
            self.0
                .lock()
                .expect("seen")
                .push(format!("{phase}:{} {serves:?}", exchange.method));
        }
    }

    impl HttpMiddleware for SeeServes {
        fn before_request_headers<'a>(
            &'a self,
            exchange: Exchange<'a>,
            _headers: &'a mut HeaderMap,
        ) -> WasmBoxedFuture<'a, Result<()>> {
            self.note("headers", exchange);
            Box::pin(async { Ok(()) })
        }

        fn before_request_body<'a>(
            &'a self,
            exchange: Exchange<'a>,
            _headers: &'a HeaderMap,
            body: Bytes,
        ) -> WasmBoxedFuture<'a, Result<Bytes>> {
            self.note("body", exchange);
            Box::pin(async move { Ok(body) })
        }

        fn after_response<'a>(
            &'a self,
            exchange: Exchange<'a>,
            _status: StatusCode,
            _headers: &'a HeaderMap,
        ) -> WasmBoxedFuture<'a, Result<()>> {
            self.note("response", exchange);
            Box::pin(async { Ok(()) })
        }
    }

    #[test]
    fn every_hook_reads_what_the_request_carries() {
        let seen = Arc::new(Mutex::new(Vec::new()));
        let inner = RecordingHttpClient::with_error(StatusCode::TOO_MANY_REQUESTS, "slow");
        let erased = DynHttpClient::new(inner).with_middleware(SeeServes(seen.clone()));
        let mut marked = request("hello");
        marked.extensions_mut().insert(Serves(7));
        let _ = block_on(erased.send::<_, Bytes>(marked));
        assert_eq!(
            seen.lock().expect("seen").as_slice(),
            [
                "headers:POST Some(7)",
                "body:POST Some(7)",
                "response:POST Some(7)"
            ]
        );
    }

    #[test]
    fn erasing_preserves_middleware_and_ptr_eq_ignores_it() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let plain = DynHttpClient::new(RecordingHttpClient::new("ok"));
        let layered = plain.clone().with_middleware(Tagger {
            tag: "a",
            log: log.clone(),
        });
        // Same transport, so ptr_eq holds despite differing middleware.
        assert!(plain.ptr_eq(&layered));
        // Re-erasing clones the handle, middleware included.
        let reerased = DynHttpClient::new(layered);
        block_on(reerased.send::<_, Bytes>(request("x"))).expect("send");
        assert!(!log.lock().expect("log").is_empty());
    }
}

#[test]
fn transport_errors_surface_unchanged() {
    let inner = RecordingHttpClient::with_error(http::StatusCode::TOO_MANY_REQUESTS, "slow");
    let erased = DynHttpClient::new(inner);
    let Err(err) = block_on(erased.send::<_, Bytes>(request(""))) else {
        panic!("expected a transport error")
    };
    assert_eq!(
        err.non_success_status(),
        Some(http::StatusCode::TOO_MANY_REQUESTS)
    );
}