rama-http 0.3.0-rc1

rama http layers, services and other utilities
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use super::{JsonRequestRewriteLayer, JsonRewriteBody, JsonRewriteLayer};
use crate::{Body, Request, Response, StreamingBody, body::Frame, body::util::BodyExt, header};
use parking_lot::Mutex;
use rama_core::bytes::Bytes;
use rama_core::error::BoxError;
use rama_core::extensions::Extension;
use rama_core::futures::stream;
use rama_core::service::service_fn;
use rama_core::{Layer, Service};
use rama_json::path::JsonPath;
use rama_json::rewrite::{HandlerResult, JsonValue, JsonValueHandler};
use std::collections::VecDeque;
use std::convert::Infallible;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::task::{Context, Poll};

#[derive(Clone)]
struct ReplaceWith(&'static str);

impl JsonValueHandler for ReplaceWith {
    fn handle_value(&mut self, _selector: usize, value: &mut JsonValue<'_>) -> HandlerResult {
        value.replace(self.0)
    }
}

#[derive(Clone)]
struct RemoveValue;

impl JsonValueHandler for RemoveValue {
    fn handle_value(&mut self, _selector: usize, value: &mut JsonValue<'_>) -> HandlerResult {
        value.remove();
        Ok(())
    }
}

fn name_path() -> JsonPath {
    JsonPath::builder().member("name").build()
}

fn user_name_path() -> JsonPath {
    JsonPath::builder().member("user").member("name").build()
}

fn prompt_path() -> JsonPath {
    JsonPath::builder().member("prompt").build()
}

fn descendant_name_path() -> JsonPath {
    JsonPath::builder().descendant_member("name").build()
}

#[tokio::test]
async fn body_rewrites_json_directly() {
    let body = JsonRewriteBody::new(
        Body::from(r#"{"user":{"name":"Ada"}}"#),
        [user_name_path()],
        ReplaceWith("Grace"),
    );
    let out = body.collect().await.expect("collect").to_bytes();
    assert_eq!(&out[..], br#"{"user":{"name":"Grace"}}"#);
}

#[tokio::test]
async fn body_removes_json_object_subtree() {
    let body = JsonRewriteBody::new(
        Body::from(r#"{"id":1,"prompt":{"text":"secret","meta":{"x":1}},"ok":true}"#),
        [prompt_path()],
        RemoveValue,
    );
    let out = body.collect().await.expect("collect").to_bytes();
    assert_eq!(&out[..], br#"{"id":1,"ok":true}"#);
}

#[tokio::test]
async fn body_rewrites_across_multiple_frames() {
    let chunks: Vec<Result<Bytes, std::io::Error>> = vec![
        Ok(Bytes::from_static(br#"{"user":{"na"#)),
        Ok(Bytes::from_static(br#"me":"Ada"}"#)),
        Ok(Bytes::from_static(br#"}"#)),
    ];
    let body = JsonRewriteBody::new(
        Body::from_stream(stream::iter(chunks)),
        [user_name_path()],
        ReplaceWith("Grace"),
    );
    let out = body.collect().await.expect("collect").to_bytes();
    assert_eq!(&out[..], br#"{"user":{"name":"Grace"}}"#);
}

#[tokio::test]
async fn body_rewrites_value_split_across_frames() {
    let chunks: Vec<Result<Bytes, std::io::Error>> = vec![
        Ok(Bytes::from_static(br#"{"user":{"name":"A"#)),
        Ok(Bytes::from_static(br#"da"}}"#)),
    ];
    let body = JsonRewriteBody::new(
        Body::from_stream(stream::iter(chunks)),
        [user_name_path()],
        ReplaceWith("Grace"),
    );
    let out = body.collect().await.expect("collect").to_bytes();
    assert_eq!(&out[..], br#"{"user":{"name":"Grace"}}"#);
}

#[tokio::test]
async fn body_surfaces_handler_error() {
    #[derive(Clone)]
    struct Boom;

    impl JsonValueHandler for Boom {
        fn handle_value(&mut self, _selector: usize, _value: &mut JsonValue<'_>) -> HandlerResult {
            Err(rama_json::JsonError::new(
                rama_json::JsonErrorKind::UnexpectedToken("boom"),
            ))
        }
    }

    let body = JsonRewriteBody::new(Body::from(r#"{"name":"Ada"}"#), [name_path()], Boom);
    body.collect()
        .await
        .expect_err("handler error should surface as a body error");
}

#[tokio::test]
async fn body_surfaces_inner_body_error() {
    let chunks: Vec<Result<Bytes, std::io::Error>> = vec![
        Ok(Bytes::from_static(br#"{"name":"Ada"}"#)),
        Err(std::io::Error::other("inner body failed")),
    ];
    let body = JsonRewriteBody::new(
        Body::from_stream(stream::iter(chunks)),
        [name_path()],
        ReplaceWith("Grace"),
    );

    body.collect()
        .await
        .expect_err("inner body error should surface");
}

#[tokio::test]
async fn body_surfaces_buffered_input_limit() {
    let body = JsonRewriteBody::with_max_buffered_bytes(
        Body::from_stream(stream::iter([
            Ok::<_, std::io::Error>(Bytes::from_static(br#"{"name":"#)),
            Ok(Bytes::from_static(br#""unterminated"#)),
        ])),
        [name_path()],
        ReplaceWith("Grace"),
        8,
    );

    body.collect()
        .await
        .expect_err("buffered input limit should surface as a body error");
}

#[tokio::test]
async fn body_passthrough_forwards_unchanged() {
    let body: JsonRewriteBody<Body, ReplaceWith> =
        JsonRewriteBody::passthrough(Body::from(r#"{"name":"Ada"}"#));
    assert_eq!(body.size_hint().exact(), Some(14));
    let out = body.collect().await.expect("collect").to_bytes();
    assert_eq!(&out[..], br#"{"name":"Ada"}"#);
}

#[tokio::test]
async fn layer_rewrites_json_and_strips_content_length() {
    let svc = JsonRewriteLayer::new([user_name_path()], ReplaceWith("Grace")).into_layer(
        service_fn(async |_: Request| {
            Ok::<_, Infallible>(
                Response::builder()
                    .header(header::CONTENT_TYPE, "application/json")
                    .header(header::CONTENT_LENGTH, "23")
                    .header(header::ETAG, "\"old\"")
                    .body(Body::from(r#"{"user":{"name":"Ada"}}"#))
                    .expect("response"),
            )
        }),
    );

    let res = svc.serve(Request::new(Body::empty())).await.expect("serve");
    assert!(res.headers().get(header::CONTENT_LENGTH).is_none());
    assert!(res.headers().get(header::ETAG).is_none());
    let out = res.into_body().collect().await.expect("collect").to_bytes();
    assert_eq!(&out[..], br#"{"user":{"name":"Grace"}}"#);
    assert_ne!(out.len(), 23);
}

#[tokio::test]
async fn layer_rewrite_can_set_buffered_input_limit() {
    let svc = JsonRewriteLayer::new([name_path()], ReplaceWith("Grace"))
        .with_max_buffered_bytes(8)
        .into_layer(service_fn(async |_: Request| {
            Ok::<_, Infallible>(
                Response::builder()
                    .header(header::CONTENT_TYPE, "application/json")
                    .body(Body::from_stream(stream::iter([
                        Ok::<_, std::io::Error>(Bytes::from_static(br#"{"name":"#)),
                        Ok(Bytes::from_static(br#""unterminated"#)),
                    ])))
                    .expect("response"),
            )
        }));

    let res = svc.serve(Request::new(Body::empty())).await.expect("serve");
    res.into_body()
        .collect()
        .await
        .expect_err("buffered input limit should surface as a body error");
}

#[tokio::test]
async fn layer_rewrites_structured_json_content_type() {
    let svc = JsonRewriteLayer::new([name_path()], ReplaceWith("Grace")).into_layer(service_fn(
        async |_: Request| {
            Ok::<_, Infallible>(
                Response::builder()
                    .header(header::CONTENT_TYPE, "application/problem+json")
                    .body(Body::from(r#"{"name":"Ada"}"#))
                    .expect("response"),
            )
        },
    ));

    let res = svc.serve(Request::new(Body::empty())).await.expect("serve");
    let out = res.into_body().collect().await.expect("collect").to_bytes();
    assert_eq!(&out[..], br#"{"name":"Grace"}"#);
}

#[tokio::test]
async fn layer_with_empty_selectors_is_passthrough() {
    let svc = JsonRewriteLayer::new([], ReplaceWith("Grace")).into_layer(service_fn(
        async |_: Request| {
            Ok::<_, Infallible>(
                Response::builder()
                    .header(header::CONTENT_TYPE, "application/json")
                    .header(header::CONTENT_LENGTH, "14")
                    .body(Body::from(r#"{"name":"Ada"}"#))
                    .expect("response"),
            )
        },
    ));

    let res = svc.serve(Request::new(Body::empty())).await.expect("serve");
    assert_eq!(
        res.headers().get(header::CONTENT_LENGTH),
        Some(&"14".parse().expect("header"))
    );
    let out = res.into_body().collect().await.expect("collect").to_bytes();
    assert_eq!(&out[..], br#"{"name":"Ada"}"#);
}

#[tokio::test]
async fn layer_passthrough_for_non_json() {
    let svc = JsonRewriteLayer::new([name_path()], ReplaceWith("Grace")).into_layer(service_fn(
        async |_: Request| {
            Ok::<_, Infallible>(
                Response::builder()
                    .header(header::CONTENT_TYPE, "text/plain")
                    .body(Body::from(r#"{"name":"Ada"}"#))
                    .expect("response"),
            )
        },
    ));

    let res = svc.serve(Request::new(Body::empty())).await.expect("serve");
    let out = res.into_body().collect().await.expect("collect").to_bytes();
    assert_eq!(&out[..], br#"{"name":"Ada"}"#);
}

#[tokio::test]
async fn layer_skips_content_encoded() {
    let svc = JsonRewriteLayer::new([name_path()], ReplaceWith("Grace")).into_layer(service_fn(
        async |_: Request| {
            Ok::<_, Infallible>(
                Response::builder()
                    .header(header::CONTENT_TYPE, "application/json")
                    .header(header::CONTENT_ENCODING, "gzip")
                    .body(Body::from(r#"{"name":"Ada"}"#))
                    .expect("response"),
            )
        },
    ));

    let res = svc.serve(Request::new(Body::empty())).await.expect("serve");
    let out = res.into_body().collect().await.expect("collect").to_bytes();
    assert_eq!(&out[..], br#"{"name":"Ada"}"#);
}

#[tokio::test]
async fn request_layer_rewrites_json_and_strips_content_length() {
    let svc = JsonRequestRewriteLayer::new([user_name_path()], ReplaceWith("Grace")).into_layer(
        service_fn(async |req: Request<JsonRewriteBody<Body, ReplaceWith>>| {
            assert!(req.headers().get(header::CONTENT_LENGTH).is_none());
            let out = req.into_body().collect().await.expect("collect").to_bytes();
            assert_eq!(&out[..], br#"{"user":{"name":"Grace"}}"#);
            Ok::<_, Infallible>(Response::new(Body::empty()))
        }),
    );

    svc.serve(
        Request::builder()
            .header(header::CONTENT_TYPE, "application/json")
            .header(header::CONTENT_LENGTH, "23")
            .body(Body::from(r#"{"user":{"name":"Ada"}}"#))
            .expect("request"),
    )
    .await
    .expect("serve");
}

#[tokio::test]
async fn request_layer_passthrough_for_non_json() {
    let svc = JsonRequestRewriteLayer::new([name_path()], ReplaceWith("Grace")).into_layer(
        service_fn(async |req: Request<JsonRewriteBody<Body, ReplaceWith>>| {
            assert_eq!(
                req.headers().get(header::CONTENT_LENGTH),
                Some(&"14".parse().expect("header"))
            );
            let out = req.into_body().collect().await.expect("collect").to_bytes();
            assert_eq!(&out[..], br#"{"name":"Ada"}"#);
            Ok::<_, Infallible>(Response::new(Body::empty()))
        }),
    );

    svc.serve(
        Request::builder()
            .header(header::CONTENT_TYPE, "text/plain")
            .header(header::CONTENT_LENGTH, "14")
            .body(Body::from(r#"{"name":"Ada"}"#))
            .expect("request"),
    )
    .await
    .expect("serve");
}

#[tokio::test]
async fn request_layer_skips_content_encoded() {
    let svc = JsonRequestRewriteLayer::new([name_path()], ReplaceWith("Grace")).into_layer(
        service_fn(async |req: Request<JsonRewriteBody<Body, ReplaceWith>>| {
            let out = req.into_body().collect().await.expect("collect").to_bytes();
            assert_eq!(&out[..], br#"{"name":"Ada"}"#);
            Ok::<_, Infallible>(Response::new(Body::empty()))
        }),
    );

    svc.serve(
        Request::builder()
            .header(header::CONTENT_TYPE, "application/json")
            .header(header::CONTENT_ENCODING, "gzip")
            .body(Body::from(r#"{"name":"Ada"}"#))
            .expect("request"),
    )
    .await
    .expect("serve");
}

#[tokio::test]
async fn request_layer_custom_policy_can_skip_rewrite() {
    let svc = JsonRequestRewriteLayer::new([name_path()], ReplaceWith("Grace"))
        .with_rewrite_policy(|_, _| false)
        .into_layer(service_fn(
            async |req: Request<JsonRewriteBody<Body, ReplaceWith>>| {
                let out = req.into_body().collect().await.expect("collect").to_bytes();
                assert_eq!(&out[..], br#"{"name":"Ada"}"#);
                Ok::<_, Infallible>(Response::new(Body::empty()))
            },
        ));

    svc.serve(
        Request::builder()
            .header(header::CONTENT_TYPE, "application/json")
            .body(Body::from(r#"{"name":"Ada"}"#))
            .expect("request"),
    )
    .await
    .expect("serve");
}

#[tokio::test]
async fn request_layer_custom_policy_can_use_extensions() {
    #[derive(Debug)]
    struct RewriteEnabled;

    impl Extension for RewriteEnabled {}

    let svc = JsonRequestRewriteLayer::new([name_path()], ReplaceWith("Grace"))
        .with_rewrite_policy(|_, extensions| extensions.get_ref::<RewriteEnabled>().is_some())
        .into_layer(service_fn(
            async |req: Request<JsonRewriteBody<Body, ReplaceWith>>| {
                let out = req.into_body().collect().await.expect("collect").to_bytes();
                assert_eq!(&out[..], br#"{"name":"Grace"}"#);
                Ok::<_, Infallible>(Response::new(Body::empty()))
            },
        ));

    svc.serve(
        Request::builder()
            .header(header::CONTENT_TYPE, "application/json")
            .extension(RewriteEnabled)
            .body(Body::from(r#"{"name":"Ada"}"#))
            .expect("request"),
    )
    .await
    .expect("serve");
}

#[tokio::test]
async fn request_layer_rewrite_can_set_buffered_input_limit() {
    let svc = JsonRequestRewriteLayer::new([name_path()], ReplaceWith("Grace"))
        .with_max_buffered_bytes(8)
        .into_layer(service_fn(
            async |req: Request<JsonRewriteBody<Body, ReplaceWith>>| {
                req.into_body().collect().await?;
                Ok::<_, BoxError>(Response::new(Body::empty()))
            },
        ));

    svc.serve(
        Request::builder()
            .header(header::CONTENT_TYPE, "application/json")
            .body(Body::from_stream(stream::iter([
                Ok::<_, std::io::Error>(Bytes::from_static(br#"{"name":"#)),
                Ok(Bytes::from_static(br#""unterminated"#)),
            ])))
            .expect("request"),
    )
    .await
    .expect_err("buffered input limit should surface from request body");
}

struct TestBody {
    frames: VecDeque<Frame<Bytes>>,
}

impl TestBody {
    fn new(frames: Vec<Frame<Bytes>>) -> Self {
        Self {
            frames: frames.into(),
        }
    }
}

impl StreamingBody for TestBody {
    type Data = Bytes;
    type Error = Infallible;

    fn poll_frame(
        mut self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
        match self.frames.pop_front() {
            Some(frame) => Poll::Ready(Some(Ok(frame))),
            None => Poll::Ready(None),
        }
    }
}

fn poll_body<B: StreamingBody + Unpin>(body: &mut B) -> Option<Result<Frame<B::Data>, B::Error>> {
    let waker = std::task::Waker::noop();
    let mut cx = Context::from_waker(waker);
    match Pin::new(body).poll_frame(&mut cx) {
        Poll::Ready(result) => result,
        Poll::Pending => None,
    }
}

#[test]
fn body_delivers_trailers_after_rewriter_output() {
    let mut trailers = crate::HeaderMap::new();
    trailers.insert("x-checksum", "abc123".parse().expect("header"));
    let inner = TestBody::new(vec![
        Frame::data(Bytes::from_static(br#"{"name":"Ada"}"#)),
        Frame::trailers(trailers),
    ]);
    let mut body = JsonRewriteBody::new(inner, [name_path()], ReplaceWith("Grace"));

    let first = poll_body(&mut body)
        .expect("first frame")
        .expect("frame ok")
        .into_data()
        .expect("data");
    assert_eq!(&first[..], br#"{"name":"Grace"}"#);

    let received_trailers = poll_body(&mut body)
        .expect("trailers frame")
        .expect("frame ok")
        .into_trailers()
        .expect("trailers");
    assert_eq!(
        received_trailers.get("x-checksum").expect("trailer"),
        "abc123"
    );
    assert!(poll_body(&mut body).is_none());
}

#[derive(Default)]
struct NameRecorder {
    names: Vec<String>,
}

impl JsonValueHandler for NameRecorder {
    fn handle_value(&mut self, _selector: usize, value: &mut JsonValue<'_>) -> HandlerResult {
        if let Some(name) = value.as_str() {
            self.names.push(name.into_owned());
        }
        value.replace("redacted")
    }
}

#[tokio::test]
async fn on_end_recovers_handler_state_at_clean_eof() {
    let calls = Arc::new(AtomicUsize::new(0));
    let recovered: Arc<Mutex<Option<NameRecorder>>> = Arc::new(Mutex::new(None));

    let (calls_cb, sink) = (calls.clone(), recovered.clone());
    let body = JsonRewriteBody::new(
        Body::from(r#"{"users":[{"name":"Ada"},{"name":"Grace"}]}"#),
        [descendant_name_path()],
        NameRecorder::default(),
    )
    .on_end(move |handler: NameRecorder| {
        calls_cb.fetch_add(1, Ordering::SeqCst);
        *sink.lock() = Some(handler);
    });

    let out = body.collect().await.expect("collect").to_bytes();
    assert_eq!(
        &out[..],
        br#"{"users":[{"name":"redacted"},{"name":"redacted"}]}"#
    );
    assert_eq!(calls.load(Ordering::SeqCst), 1);
    let handler = recovered.lock().take().expect("on_end fired");
    assert_eq!(handler.names, vec!["Ada".to_owned(), "Grace".to_owned()]);
}

#[tokio::test]
async fn on_end_does_not_fire_on_error_path() {
    #[derive(Clone)]
    struct Boom;

    impl JsonValueHandler for Boom {
        fn handle_value(&mut self, _selector: usize, _value: &mut JsonValue<'_>) -> HandlerResult {
            Err(rama_json::JsonError::new(
                rama_json::JsonErrorKind::UnexpectedToken("boom"),
            ))
        }
    }

    let fired = Arc::new(AtomicUsize::new(0));
    let flag = fired.clone();
    let body = JsonRewriteBody::new(Body::from(r#"{"name":"Ada"}"#), [name_path()], Boom).on_end(
        move |_handler: Boom| {
            flag.fetch_add(1, Ordering::SeqCst);
        },
    );

    body.collect()
        .await
        .expect_err("handler error should surface as a body error");
    assert_eq!(fired.load(Ordering::SeqCst), 0);
}

#[tokio::test]
async fn on_end_does_not_fire_in_passthrough() {
    let fired = Arc::new(AtomicUsize::new(0));
    let flag = fired.clone();
    let body: JsonRewriteBody<Body, ReplaceWith> = JsonRewriteBody::passthrough(Body::from(
        r#"{"name":"Ada"}"#,
    ))
    .on_end(move |_handler: ReplaceWith| {
        flag.fetch_add(1, Ordering::SeqCst);
    });

    let out = body.collect().await.expect("collect").to_bytes();
    assert_eq!(&out[..], br#"{"name":"Ada"}"#);
    assert_eq!(fired.load(Ordering::SeqCst), 0);
}