mpp 0.15.0

Rust SDK for the Machine Payments Protocol (MPP)
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
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
use super::*;
use crate::protocol::core::Base64UrlJson;

fn test_challenge() -> PaymentChallenge {
    PaymentChallenge::new(
        "test-id",
        "test-realm",
        "tempo",
        "charge",
        Base64UrlJson::from_value(&serde_json::json!({"amount": "1000"})).unwrap(),
    )
}

struct MockChallenger {
    accept: bool,
}

impl ChargeChallenger for MockChallenger {
    fn challenge(
        &self,
        amount: &str,
        _options: ChallengeOptions,
    ) -> Result<PaymentChallenge, String> {
        Ok(PaymentChallenge::new(
            "mock-id",
            "mock-realm",
            "tempo",
            "charge",
            Base64UrlJson::from_value(&serde_json::json!({"amount": amount})).unwrap(),
        ))
    }

    fn verify_payment(
        &self,
        _credential_str: &str,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Receipt, String>> + Send>> {
        let accept = self.accept;
        Box::pin(async move {
            if accept {
                Ok(Receipt {
                    status: crate::protocol::core::ReceiptStatus::Success,
                    method: crate::protocol::core::MethodName::new("tempo"),
                    timestamp: "2025-01-01T00:00:00Z".into(),
                    reference: "0xabc".into(),
                    external_id: None,
                    subscription_id: None,
                    extensions: serde_json::Map::new(),
                })
            } else {
                Err("payment rejected".into())
            }
        })
    }

    fn verify_payment_for_amount_and_scope(
        &self,
        credential_str: &str,
        amount: &str,
        _mppx_scope: Option<serde_json::Value>,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Receipt, String>> + Send>> {
        self.verify_payment_for_amount(credential_str, amount)
    }
}

struct BodyAwareChallenger {
    seen_challenge_body: Arc<std::sync::Mutex<Option<Vec<u8>>>>,
    seen_verify_body: Arc<std::sync::Mutex<Option<Vec<u8>>>>,
}

impl ChargeChallenger for BodyAwareChallenger {
    fn challenge(
        &self,
        amount: &str,
        _options: ChallengeOptions,
    ) -> Result<PaymentChallenge, String> {
        Ok(PaymentChallenge::new(
            "mock-id",
            "mock-realm",
            "tempo",
            "charge",
            Base64UrlJson::from_value(&serde_json::json!({"amount": amount})).unwrap(),
        ))
    }

    fn challenge_with_body(
        &self,
        amount: &str,
        options: ChallengeOptions,
        body: &[u8],
    ) -> Result<PaymentChallenge, String> {
        *self.seen_challenge_body.lock().unwrap() = Some(body.to_vec());
        let mut challenge = self.challenge(amount, options)?;
        challenge.digest = Some(crate::body_digest::compute(body));
        Ok(challenge)
    }

    fn verify_payment(
        &self,
        _credential_str: &str,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Receipt, String>> + Send>> {
        Box::pin(std::future::ready(Err(
            "legacy verifier should not be called".into(),
        )))
    }

    fn verify_payment_for_amount_with_body(
        &self,
        _credential_str: &str,
        _amount: &str,
        body: &[u8],
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Receipt, String>> + Send>> {
        *self.seen_verify_body.lock().unwrap() = Some(body.to_vec());
        Box::pin(std::future::ready(Ok(Receipt {
            status: crate::protocol::core::ReceiptStatus::Success,
            method: crate::protocol::core::MethodName::new("tempo"),
            timestamp: "2025-01-01T00:00:00Z".into(),
            reference: "0xbody-aware".into(),
            external_id: None,
            subscription_id: None,
            extensions: serde_json::Map::new(),
        })))
    }

    fn verify_payment_for_amount_scope_and_body(
        &self,
        credential_str: &str,
        amount: &str,
        _mppx_scope: Option<serde_json::Value>,
        body: &[u8],
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Receipt, String>> + Send>> {
        self.verify_payment_for_amount_with_body(credential_str, amount, body)
    }
}

#[derive(Debug)]
struct OneCent;
impl ChargeConfig for OneCent {
    fn amount() -> &'static str {
        "0.01"
    }
}

#[test]
fn test_payment_required_into_response() {
    let resp = PaymentRequired(test_challenge()).into_response();
    assert_eq!(resp.status(), StatusCode::PAYMENT_REQUIRED);
    assert!(resp.headers().contains_key(WWW_AUTHENTICATE_HEADER));
    assert_eq!(
        resp.headers().get(header::CACHE_CONTROL).unwrap(),
        "no-store"
    );
}

#[test]
fn test_unformattable_challenge_returns_500_without_challenge() {
    let mut challenge = test_challenge();
    challenge.realm = "a\nb".to_string();
    let resp = PaymentRequired(challenge).into_response();
    assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
    assert!(!resp.headers().contains_key(WWW_AUTHENTICATE_HEADER));
    assert_eq!(
        resp.headers().get(header::CONTENT_TYPE).unwrap(),
        "application/problem+json"
    );
}

#[test]
fn test_payment_required_has_json_content_type() {
    let resp = PaymentRequired(test_challenge()).into_response();
    assert_eq!(
        resp.headers().get(header::CONTENT_TYPE).unwrap(),
        "application/json"
    );
}

#[test]
fn test_rejection_challenge_returns_402_with_header() {
    let rejection = MppChargeRejection::Challenge(PaymentRequired(test_challenge()));
    let resp = rejection.into_response();
    assert_eq!(resp.status(), StatusCode::PAYMENT_REQUIRED);
    assert!(resp.headers().contains_key(WWW_AUTHENTICATE_HEADER));
}

#[test]
fn test_rejection_verification_failed_returns_402_with_header() {
    let rejection = MppChargeRejection::VerificationFailed(PaymentRequired(test_challenge()));
    let resp = rejection.into_response();
    assert_eq!(resp.status(), StatusCode::PAYMENT_REQUIRED);
    assert!(resp.headers().contains_key(WWW_AUTHENTICATE_HEADER));
}

#[test]
fn test_rejection_internal_error() {
    let rejection = MppChargeRejection::InternalError("oops".into());
    let resp = rejection.into_response();
    assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
    assert!(!resp.headers().contains_key(WWW_AUTHENTICATE_HEADER));
    assert_eq!(
        resp.headers().get(header::CONTENT_TYPE).unwrap(),
        "application/problem+json"
    );
}

#[test]
fn test_custom_amount() {
    struct FiveDollars;
    impl ChargeConfig for FiveDollars {
        fn amount() -> &'static str {
            "5.00"
        }
    }
    assert_eq!(FiveDollars::amount(), "5.00");
}

#[test]
fn test_config_defaults() {
    assert_eq!(OneCent::description(), None);
}

#[test]
fn test_config_overrides() {
    struct Premium;
    impl ChargeConfig for Premium {
        fn amount() -> &'static str {
            "10.00"
        }
        fn description() -> Option<&'static str> {
            Some("Premium access")
        }
    }
    assert_eq!(Premium::amount(), "10.00");
    assert_eq!(Premium::description(), Some("Premium access"));
}

#[test]
fn test_with_receipt_attaches_header() {
    use crate::protocol::core::{MethodName, ReceiptStatus};

    let receipt = Receipt {
        status: ReceiptStatus::Success,
        method: MethodName::new("tempo"),
        timestamp: "2025-01-01T00:00:00Z".into(),
        reference: "0xabc".into(),
        external_id: None,
        subscription_id: None,
        extensions: serde_json::Map::new(),
    };

    let resp = WithReceipt {
        receipt,
        body: "ok",
    }
    .into_response();

    assert_eq!(resp.status(), StatusCode::OK);
    assert!(resp.headers().contains_key(PAYMENT_RECEIPT_HEADER));
    // Per spec, receipt responses must be Cache-Control: private.
    assert_eq!(
        resp.headers().get(header::CACHE_CONTROL).unwrap(),
        "private"
    );
}

#[test]
fn test_with_receipt_does_not_attach_header_to_error_response() {
    use crate::protocol::core::{MethodName, ReceiptStatus};

    for status in [
        StatusCode::FOUND,
        StatusCode::FORBIDDEN,
        StatusCode::INTERNAL_SERVER_ERROR,
    ] {
        let receipt = Receipt {
            status: ReceiptStatus::Success,
            method: MethodName::new("tempo"),
            timestamp: "2025-01-01T00:00:00Z".into(),
            reference: "0xabc".into(),
            external_id: None,
            subscription_id: None,
            extensions: serde_json::Map::new(),
        };
        let resp = WithReceipt {
            receipt,
            body: (status, "error"),
        }
        .into_response();

        assert_eq!(resp.status(), status);
        assert!(!resp.headers().contains_key(PAYMENT_RECEIPT_HEADER));
        assert!(!resp.headers().contains_key(header::CACHE_CONTROL));
    }
}

#[test]
fn test_with_receipt_preserves_existing_cache_control() {
    use crate::protocol::core::{MethodName, ReceiptStatus};

    let receipt = Receipt {
        status: ReceiptStatus::Success,
        method: MethodName::new("tempo"),
        timestamp: "2025-01-01T00:00:00Z".into(),
        reference: "0xabc".into(),
        external_id: None,
        subscription_id: None,
        extensions: serde_json::Map::new(),
    };
    let mut body = "ok".into_response();
    body.headers_mut()
        .append(header::CACHE_CONTROL, HeaderValue::from_static("public"));
    body.headers_mut().append(
        header::CACHE_CONTROL,
        HeaderValue::from_static("max-age=60"),
    );

    let resp = WithReceipt { receipt, body }.into_response();

    assert!(resp.headers().contains_key(PAYMENT_RECEIPT_HEADER));
    assert_eq!(
        resp.headers().get(header::CACHE_CONTROL).unwrap(),
        "public, max-age=60, private"
    );
}

#[test]
fn test_mock_challenger_generates_challenge() {
    let challenger = MockChallenger { accept: true };
    let challenge = challenger
        .challenge("0.50", ChallengeOptions::default())
        .unwrap();
    assert_eq!(challenge.id, "mock-id");
}

#[tokio::test]
async fn test_mock_challenger_verify_accept() {
    let challenger = MockChallenger { accept: true };
    let result = challenger.verify_payment("Payment eyJ...").await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap().reference, "0xabc");
}

#[tokio::test]
async fn test_mock_challenger_verify_reject() {
    let challenger = MockChallenger { accept: false };
    let result = challenger.verify_payment("Payment eyJ...").await;
    assert!(result.is_err());
}

async fn run_extractor<C: ChargeConfig>(
    challenger: impl ChargeChallenger,
    auth_header: Option<&str>,
) -> Result<MppCharge<C>, MppChargeRejection> {
    run_extractor_with_uri::<C>(challenger, auth_header, "/test").await
}

async fn run_extractor_with_uri<C: ChargeConfig>(
    challenger: impl ChargeChallenger,
    auth_header: Option<&str>,
    uri: &str,
) -> Result<MppCharge<C>, MppChargeRejection> {
    let state: Arc<dyn ChargeChallenger> = Arc::new(challenger);
    let mut builder = http_types::Request::builder().uri(uri);
    if let Some(auth) = auth_header {
        builder = builder.header(header::AUTHORIZATION, auth);
    }
    let req = builder.body(()).unwrap();
    let (mut parts, _body) = req.into_parts();
    MppCharge::<C>::from_request_parts(&mut parts, &state).await
}

#[tokio::test]
async fn test_extractor_binds_axum_matched_route_scope() {
    use axum::routing::get;
    use tower::ServiceExt;

    #[derive(Clone)]
    struct ScopeCaptureChallenger {
        seen_scope: Arc<std::sync::Mutex<Option<serde_json::Value>>>,
    }

    impl ChargeChallenger for ScopeCaptureChallenger {
        fn challenge(
            &self,
            amount: &str,
            options: ChallengeOptions,
        ) -> Result<PaymentChallenge, String> {
            *self.seen_scope.lock().unwrap() = options.mppx_scope;
            Ok(PaymentChallenge::new(
                "mock-id",
                "mock-realm",
                "tempo",
                "charge",
                Base64UrlJson::from_value(&serde_json::json!({"amount": amount})).unwrap(),
            ))
        }

        fn verify_payment(
            &self,
            _credential_str: &str,
        ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Receipt, String>> + Send>>
        {
            Box::pin(std::future::ready(Err("unused".into())))
        }
    }

    async fn paid(_charge: MppCharge<OneCent>) -> &'static str {
        "paid"
    }

    let seen_scope = Arc::new(std::sync::Mutex::new(None));
    let state: Arc<dyn ChargeChallenger> = Arc::new(ScopeCaptureChallenger {
        seen_scope: seen_scope.clone(),
    });
    let app = axum::Router::new()
        .route("/paid/{id}", get(paid))
        .with_state(state);

    let response = app
        .oneshot(
            http_types::Request::builder()
                .uri("/paid/one?view=full")
                .body(axum::body::Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::PAYMENT_REQUIRED);
    assert_eq!(
        seen_scope.lock().unwrap().as_ref(),
        Some(&serde_json::json!({
            "route": "/paid/{id}",
            "resource": "/paid/one",
            "query": "view=full",
        }))
    );
}

async fn run_body_extractor<C: ChargeConfig>(
    challenger: impl ChargeChallenger,
    auth_header: Option<&str>,
    body: &'static str,
) -> Result<MppChargeWithBody<C>, MppChargeRejection> {
    let state: Arc<dyn ChargeChallenger> = Arc::new(challenger);
    let mut builder = Request::builder().uri("/test");
    if let Some(auth) = auth_header {
        builder = builder.header(header::AUTHORIZATION, auth);
    }
    let req = builder.body(axum_core::body::Body::from(body)).unwrap();
    MppChargeWithBody::<C>::from_request(req, &state).await
}

#[tokio::test]
async fn test_extractor_no_auth_returns_challenge() {
    let result = run_extractor::<OneCent>(MockChallenger { accept: true }, None).await;
    let err = result.unwrap_err();
    let resp = err.into_response();
    assert_eq!(resp.status(), StatusCode::PAYMENT_REQUIRED);
    assert!(resp.headers().contains_key(WWW_AUTHENTICATE_HEADER));
}

#[tokio::test]
async fn test_extractor_valid_payment_returns_receipt() {
    let result = run_extractor::<OneCent>(
        MockChallenger { accept: true },
        Some("Payment eyJmYWtlIjp0cnVlfQ"),
    )
    .await;
    let charge = result.unwrap();
    assert_eq!(charge.receipt.reference, "0xabc");
}

#[tokio::test]
async fn test_extractor_fails_closed_when_challenger_does_not_verify_scope() {
    struct LegacyChallenger;

    impl ChargeChallenger for LegacyChallenger {
        fn challenge(
            &self,
            amount: &str,
            _options: ChallengeOptions,
        ) -> Result<PaymentChallenge, String> {
            Ok(PaymentChallenge::new(
                "mock-id",
                "mock-realm",
                "tempo",
                "charge",
                Base64UrlJson::from_value(&serde_json::json!({"amount": amount})).unwrap(),
            ))
        }

        fn verify_payment(
            &self,
            _credential_str: &str,
        ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Receipt, String>> + Send>>
        {
            Box::pin(std::future::ready(Ok(Receipt {
                status: crate::protocol::core::ReceiptStatus::Success,
                method: crate::protocol::core::MethodName::new("tempo"),
                timestamp: "2025-01-01T00:00:00Z".into(),
                reference: "0xlegacy".into(),
                external_id: None,
                subscription_id: None,
                extensions: serde_json::Map::new(),
            })))
        }
    }

    let result =
        run_extractor::<OneCent>(LegacyChallenger, Some("Payment eyJmYWtlIjp0cnVlfQ")).await;

    let err = result.unwrap_err();
    assert!(matches!(err, MppChargeRejection::Problem(_)));
}

#[tokio::test]
async fn test_extractor_invalid_payment_returns_challenge_for_retry() {
    let result = run_extractor::<OneCent>(
        MockChallenger { accept: false },
        Some("Payment eyJmYWtlIjp0cnVlfQ"),
    )
    .await;
    let err = result.unwrap_err();
    assert!(matches!(err, MppChargeRejection::Problem(_)));
    let resp = err.into_response();
    assert_eq!(resp.status(), StatusCode::PAYMENT_REQUIRED);
    assert!(resp.headers().contains_key(WWW_AUTHENTICATE_HEADER));
    assert_eq!(
        resp.headers().get(header::CONTENT_TYPE).unwrap(),
        "application/problem+json"
    );
}

#[tokio::test]
async fn test_extractor_wrong_scheme_returns_challenge() {
    let result =
        run_extractor::<OneCent>(MockChallenger { accept: true }, Some("Bearer some-token")).await;
    let err = result.unwrap_err();
    assert!(matches!(err, MppChargeRejection::Challenge(_)));
}

#[tokio::test]
async fn test_extractor_custom_amount() {
    #[derive(Debug)]
    struct TenCents;
    impl ChargeConfig for TenCents {
        fn amount() -> &'static str {
            "0.10"
        }
    }

    let result = run_extractor::<TenCents>(MockChallenger { accept: true }, None).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_extractor_challenge_failure_returns_internal_error() {
    struct FailingChallenger;
    impl ChargeChallenger for FailingChallenger {
        fn challenge(
            &self,
            _amount: &str,
            _options: ChallengeOptions,
        ) -> Result<PaymentChallenge, String> {
            Err("config error".into())
        }
        fn verify_payment(
            &self,
            _credential_str: &str,
        ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Receipt, String>> + Send>>
        {
            Box::pin(std::future::ready(Err("unused".into())))
        }
    }

    let state: Arc<dyn ChargeChallenger> = Arc::new(FailingChallenger);
    let req = http_types::Request::builder()
        .uri("/test")
        .body(())
        .unwrap();
    let (mut parts, _body) = req.into_parts();
    let result = MppCharge::<OneCent>::from_request_parts(&mut parts, &state).await;
    let err = result.unwrap_err();
    assert!(matches!(err, MppChargeRejection::InternalError(_)));
    let resp = err.into_response();
    assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
}

#[tokio::test]
async fn test_extractor_malformed_payment_credential_returns_verification_failed() {
    let result = run_extractor::<OneCent>(
        MockChallenger { accept: false },
        Some("Payment !!not-base64!!"),
    )
    .await;
    let err = result.unwrap_err();
    assert!(matches!(err, MppChargeRejection::Problem(_)));
    let resp = err.into_response();
    assert_eq!(resp.status(), StatusCode::PAYMENT_REQUIRED);
}

#[tokio::test]
async fn test_body_extractor_challenge_binds_request_body() {
    let seen_challenge_body = Arc::new(std::sync::Mutex::new(None));
    let seen_verify_body = Arc::new(std::sync::Mutex::new(None));
    let result = run_body_extractor::<OneCent>(
        BodyAwareChallenger {
            seen_challenge_body: seen_challenge_body.clone(),
            seen_verify_body,
        },
        None,
        r#"{"query":"paid"}"#,
    )
    .await;

    let err = result.unwrap_err();
    let challenge = match err {
        MppChargeRejection::Challenge(PaymentRequired(challenge)) => challenge,
        other => panic!("expected challenge rejection, got {other:?}"),
    };
    assert_eq!(
        challenge.digest.as_deref(),
        Some(crate::body_digest::compute(br#"{"query":"paid"}"#).as_str())
    );
    assert_eq!(
        seen_challenge_body.lock().unwrap().as_deref(),
        Some(br#"{"query":"paid"}"#.as_slice())
    );
}

#[tokio::test]
async fn test_body_extractor_verifies_and_preserves_request_body() {
    let seen_challenge_body = Arc::new(std::sync::Mutex::new(None));
    let seen_verify_body = Arc::new(std::sync::Mutex::new(None));
    let result = run_body_extractor::<OneCent>(
        BodyAwareChallenger {
            seen_challenge_body,
            seen_verify_body: seen_verify_body.clone(),
        },
        Some("Payment eyJmYWtlIjp0cnVlfQ"),
        r#"{"query":"paid"}"#,
    )
    .await;

    let charge = result.unwrap();
    assert_eq!(charge.receipt.reference, "0xbody-aware");
    assert_eq!(charge.body.as_ref(), br#"{"query":"paid"}"#);
    assert_eq!(
        seen_verify_body.lock().unwrap().as_deref(),
        Some(br#"{"query":"paid"}"#.as_slice())
    );
}

async fn post_to_body_extractor(body: axum_core::body::Body) -> StatusCode {
    use axum::routing::post;
    use tower::ServiceExt;

    async fn paid(_charge: MppChargeWithBody<OneCent>) {}

    let state: Arc<dyn ChargeChallenger> = Arc::new(MockChallenger { accept: true });
    let app = axum::Router::new()
        .route("/paid", post(paid))
        .layer(axum_core::extract::DefaultBodyLimit::max(1024))
        .with_state(state);

    let req = Request::builder()
        .method("POST")
        .uri("/paid")
        .body(body)
        .unwrap();
    app.oneshot(req).await.unwrap().status()
}

#[tokio::test]
async fn test_body_extractor_respects_default_body_limit() {
    use futures_util::StreamExt;

    let chunks = futures_util::stream::iter(0..).map(|i| {
        assert!(i < 4, "body read past the limit");
        Ok::<_, std::convert::Infallible>(Bytes::from_static(&[0; 512]))
    });
    let status = post_to_body_extractor(axum_core::body::Body::from_stream(chunks)).await;
    assert_eq!(status, StatusCode::PAYLOAD_TOO_LARGE);
}

#[tokio::test]
async fn test_body_extractor_body_read_error_returns_400() {
    let chunks = futures_util::stream::iter([Err::<Bytes, _>(std::io::Error::other("reset"))]);
    let status = post_to_body_extractor(axum_core::body::Body::from_stream(chunks)).await;
    assert_eq!(status, StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn test_extractor_uses_route_aware_verification_path() {
    struct RouteAwareChallenger {
        seen_amount: Arc<std::sync::Mutex<Option<String>>>,
    }

    impl ChargeChallenger for RouteAwareChallenger {
        fn challenge(
            &self,
            amount: &str,
            _options: ChallengeOptions,
        ) -> Result<PaymentChallenge, String> {
            Ok(PaymentChallenge::new(
                "mock-id",
                "mock-realm",
                "tempo",
                "charge",
                Base64UrlJson::from_value(&serde_json::json!({"amount": amount})).unwrap(),
            ))
        }

        fn verify_payment(
            &self,
            _credential_str: &str,
        ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Receipt, String>> + Send>>
        {
            Box::pin(std::future::ready(Err(
                "legacy verifier should not be called".into(),
            )))
        }

        fn verify_payment_for_amount(
            &self,
            _credential_str: &str,
            amount: &str,
        ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Receipt, String>> + Send>>
        {
            *self.seen_amount.lock().unwrap() = Some(amount.to_string());
            Box::pin(std::future::ready(Ok(Receipt {
                status: crate::protocol::core::ReceiptStatus::Success,
                method: crate::protocol::core::MethodName::new("tempo"),
                timestamp: "2025-01-01T00:00:00Z".into(),
                reference: "0xroute-aware".into(),
                external_id: None,
                subscription_id: None,
                extensions: serde_json::Map::new(),
            })))
        }

        fn verify_payment_for_amount_and_scope(
            &self,
            credential_str: &str,
            amount: &str,
            _mppx_scope: Option<serde_json::Value>,
        ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Receipt, String>> + Send>>
        {
            self.verify_payment_for_amount(credential_str, amount)
        }
    }

    let seen_amount = Arc::new(std::sync::Mutex::new(None));
    let result = run_extractor::<OneCent>(
        RouteAwareChallenger {
            seen_amount: seen_amount.clone(),
        },
        Some("Payment eyJmYWtlIjp0cnVlfQ"),
    )
    .await;

    let charge = result.unwrap();
    assert_eq!(charge.receipt.reference, "0xroute-aware");
    assert_eq!(seen_amount.lock().unwrap().as_deref(), Some("0.01"));
}

// Framework route-replay conformance: exercise the real extractor
// (`MppCharge<C>`) and per-route amount selection against the production
// binding logic, matching the two-price axum-extractor example.
#[cfg(feature = "tempo")]
mod route_replay {
    use super::*;
    use crate::protocol::core::headers::format_authorization;
    use crate::protocol::core::{PaymentCredential, PaymentPayload};
    use crate::protocol::intents::ChargeRequest;
    use crate::protocol::traits::VerificationError;
    use crate::server::{ChargeMethod, Mpp};
    use std::future::Future;

    #[derive(Debug)]
    struct OneDollar;
    impl ChargeConfig for OneDollar {
        fn amount() -> &'static str {
            "1.00"
        }
    }

    // Chain-free charge method so verification never needs RPC.
    #[derive(Clone)]
    struct SuccessMethod;

    #[allow(clippy::manual_async_fn)]
    impl ChargeMethod for SuccessMethod {
        fn method(&self) -> &str {
            "tempo"
        }
        fn verify(
            &self,
            _credential: &PaymentCredential,
            _request: &ChargeRequest,
        ) -> impl Future<Output = Result<Receipt, VerificationError>> + Send {
            async { Ok(Receipt::success("tempo", "0xtxhash")) }
        }
    }

    // Faithful challenger delegating to the production binding logic.
    #[derive(Clone)]
    struct RealBindingChallenger {
        mpp: Mpp<SuccessMethod>,
    }

    impl RealBindingChallenger {
        fn new() -> Self {
            Self {
                mpp: Mpp::new_with_config(
                    SuccessMethod,
                    "MPP Payment",
                    "test-secret-key-at-least-32-bytes",
                    "0x20c0000000000000000000000000000000000000",
                    "0x742d35Cc6634C0532925a3b844Bc9e7595f1B0F2",
                ),
            }
        }
    }

    impl ChargeChallenger for RealBindingChallenger {
        fn challenge(
            &self,
            amount: &str,
            options: ChallengeOptions,
        ) -> Result<PaymentChallenge, String> {
            self.mpp
                .charge_with_options(
                    amount,
                    crate::server::ChargeOptions {
                        description: options.description,
                        mppx_scope: options.mppx_scope.as_ref(),
                        ..Default::default()
                    },
                )
                .map(|mut offers| offers.remove(0))
                .map_err(|e| e.to_string())
        }

        fn verify_payment(
            &self,
            credential_str: &str,
        ) -> std::pin::Pin<Box<dyn Future<Output = Result<Receipt, String>> + Send>> {
            let credential = match parse_authorization(credential_str) {
                Ok(c) => c,
                Err(e) => return Box::pin(std::future::ready(Err(e.to_string()))),
            };
            let mpp = self.mpp.clone();
            Box::pin(async move {
                mpp.broadcast_credential(&credential)
                    .await
                    .map_err(|e| e.to_string())
            })
        }

        fn verify_payment_for_amount(
            &self,
            credential_str: &str,
            amount: &str,
        ) -> std::pin::Pin<Box<dyn Future<Output = Result<Receipt, String>> + Send>> {
            let credential = match parse_authorization(credential_str) {
                Ok(c) => c,
                Err(e) => return Box::pin(std::future::ready(Err(e.to_string()))),
            };
            let expected = match self
                .mpp
                .charge(amount)
                .map(|mut offers| offers.remove(0))
                .and_then(|c| c.request.decode())
            {
                Ok(req) => req,
                Err(e) => return Box::pin(std::future::ready(Err(e.to_string()))),
            };
            let mpp = self.mpp.clone();
            Box::pin(async move {
                mpp.verify_credential_with_expected_request(&credential, &expected)
                    .await
                    .map_err(|e| e.to_string())
            })
        }

        fn verify_payment_for_amount_and_scope(
            &self,
            credential_str: &str,
            amount: &str,
            mppx_scope: Option<serde_json::Value>,
        ) -> std::pin::Pin<Box<dyn Future<Output = Result<Receipt, String>> + Send>> {
            let credential = match parse_authorization(credential_str) {
                Ok(c) => c,
                Err(e) => return Box::pin(std::future::ready(Err(e.to_string()))),
            };
            let expected = match self
                .mpp
                .charge_with_options(
                    amount,
                    crate::server::ChargeOptions {
                        mppx_scope: mppx_scope.as_ref(),
                        ..Default::default()
                    },
                )
                .map(|mut offers| offers.remove(0))
                .and_then(|c| c.request.decode())
            {
                Ok(req) => req,
                Err(e) => return Box::pin(std::future::ready(Err(e.to_string()))),
            };
            let mpp = self.mpp.clone();
            Box::pin(async move {
                mpp.verify_credential_with_expected_request(&credential, &expected)
                    .await
                    .map_err(|e| e.to_string())
            })
        }
    }

    // Mint an `Authorization: Payment …` string for the given route amount.
    fn scope_for_uri(uri: &str) -> serde_json::Value {
        let req = http_types::Request::builder().uri(uri).body(()).unwrap();
        let (parts, _body) = req.into_parts();
        mppx_scope_from_parts(&parts).unwrap()
    }

    fn mint_credential(challenger: &RealBindingChallenger, amount: &str) -> String {
        mint_scoped_credential(challenger, amount, "/test")
    }

    fn mint_scoped_credential(
        challenger: &RealBindingChallenger,
        amount: &str,
        uri: &str,
    ) -> String {
        let challenge = challenger
            .challenge(
                amount,
                ChallengeOptions {
                    mppx_scope: Some(scope_for_uri(uri)),
                    ..Default::default()
                },
            )
            .unwrap();
        let credential =
            PaymentCredential::new(challenge.to_echo(), PaymentPayload::hash("0xdeadbeef"));
        format_authorization(&credential).unwrap()
    }

    #[tokio::test]
    async fn test_extractor_accepts_credential_on_matching_route() {
        let challenger = RealBindingChallenger::new();
        let auth = mint_credential(&challenger, OneCent::amount());

        let charge = run_extractor::<OneCent>(challenger, Some(&auth))
            .await
            .expect("credential minted for the route must verify");
        assert_eq!(charge.receipt.reference, "0xtxhash");
    }

    #[tokio::test]
    async fn test_extractor_rejects_cross_route_credential_replay() {
        let challenger = RealBindingChallenger::new();
        // Mint for the cheap route, replay on the expensive route.
        let auth = mint_credential(&challenger, OneCent::amount());

        let err = run_extractor::<OneDollar>(challenger, Some(&auth))
            .await
            .expect_err("cross-route replay must be rejected");
        assert!(matches!(err, MppChargeRejection::Problem(_)));
        assert_eq!(err.into_response().status(), StatusCode::PAYMENT_REQUIRED);
    }

    #[tokio::test]
    async fn test_extractor_selects_per_route_amount() {
        let challenger = RealBindingChallenger::new();
        // A credential minted at $1.00 is accepted by the premium route...
        let auth = mint_credential(&challenger, OneDollar::amount());
        assert!(run_extractor::<OneDollar>(challenger.clone(), Some(&auth))
            .await
            .is_ok());
        // ...but rejected by the cheap route, proving the extractor passes
        // each route's own `ChargeConfig::amount()` to verification.
        let err = run_extractor::<OneCent>(challenger, Some(&auth))
            .await
            .expect_err("premium credential must not satisfy the cheap route");
        assert!(matches!(err, MppChargeRejection::Problem(_)));
    }

    #[tokio::test]
    async fn test_extractor_rejects_cross_resource_credential_replay() {
        let challenger = RealBindingChallenger::new();
        let auth = mint_scoped_credential(&challenger, OneCent::amount(), "/paid/one?view=full");

        let err = run_extractor_with_uri::<OneCent>(challenger, Some(&auth), "/paid/two?view=full")
            .await
            .expect_err("same-price cross-resource replay must be rejected");
        assert!(matches!(err, MppChargeRejection::Problem(_)));
        assert_eq!(err.into_response().status(), StatusCode::PAYMENT_REQUIRED);
    }
}

#[cfg(feature = "tempo")]
mod currency_offers {
    use super::*;
    use crate::protocol::core::headers::{format_authorization, parse_www_authenticate_all};
    use crate::protocol::core::{PaymentCredential, PaymentPayload};
    use crate::protocol::intents::ChargeRequest;
    use crate::protocol::methods::tempo::{CHAIN_ID, OUSD, PATH_USD, USDC};
    use crate::protocol::traits::VerificationError;
    use crate::server::{tempo, ChargeMethod, Mpp, TempoConfig};
    use std::future::Future;

    const RECIPIENT: &str = "0x742d35Cc6634C0532925a3b844Bc9e7595f1B0F2";

    #[derive(Clone)]
    struct SuccessMethod;

    #[allow(clippy::manual_async_fn)]
    impl ChargeMethod for SuccessMethod {
        fn method(&self) -> &str {
            "tempo"
        }
        fn verify(
            &self,
            _credential: &PaymentCredential,
            _request: &ChargeRequest,
        ) -> impl Future<Output = Result<Receipt, VerificationError>> + Send {
            async { Ok(Receipt::success("tempo", "0xtxhash")) }
        }
    }

    /// RPC-free challenger mirroring the Tempo `ChargeChallenger` binding.
    #[derive(Clone)]
    struct OffersChallenger {
        mpp: Mpp<SuccessMethod>,
    }

    impl OffersChallenger {
        fn new(currencies: &[&str]) -> Self {
            Self {
                mpp: Mpp::new_with_config(
                    SuccessMethod,
                    "MPP Payment",
                    "test-secret-key-at-least-32-bytes",
                    "",
                    RECIPIENT,
                )
                .with_currencies(currencies.iter().map(|c| c.to_string()).collect()),
            }
        }

        fn options(options: &ChallengeOptions) -> crate::server::ChargeOptions<'_> {
            crate::server::ChargeOptions {
                description: options.description,
                mppx_scope: options.mppx_scope.as_ref(),
                ..Default::default()
            }
        }
    }

    impl ChargeChallenger for OffersChallenger {
        fn challenge(
            &self,
            amount: &str,
            options: ChallengeOptions,
        ) -> Result<PaymentChallenge, String> {
            self.mpp
                .charge_with_options(amount, Self::options(&options))
                .map(|mut offers| offers.remove(0))
                .map_err(|e| e.to_string())
        }

        fn verify_payment(
            &self,
            credential_str: &str,
        ) -> std::pin::Pin<Box<dyn Future<Output = Result<Receipt, String>> + Send>> {
            self.verify_payment_for_amount_and_scope(credential_str, "0.01", None)
        }

        fn verify_payment_for_amount_and_scope(
            &self,
            credential_str: &str,
            amount: &str,
            mppx_scope: Option<serde_json::Value>,
        ) -> std::pin::Pin<Box<dyn Future<Output = Result<Receipt, String>> + Send>> {
            let credential = match parse_authorization(credential_str) {
                Ok(c) => c,
                Err(e) => return Box::pin(std::future::ready(Err(e.to_string()))),
            };
            // Same as the Tempo binding: expected request from the preferred offer.
            let options = ChallengeOptions {
                mppx_scope,
                ..Default::default()
            };
            let expected = match self
                .mpp
                .charge_with_options(amount, Self::options(&options))
                .map(|mut offers| offers.remove(0))
                .and_then(|c| c.request.decode::<ChargeRequest>())
            {
                Ok(req) => req,
                Err(e) => return Box::pin(std::future::ready(Err(e.to_string()))),
            };
            let mpp = self.mpp.clone();
            Box::pin(async move {
                mpp.verify_credential_with_expected_request(&credential, &expected)
                    .await
                    .map_err(|e| e.to_string())
            })
        }

        fn challenges(
            &self,
            amount: &str,
            options: ChallengeOptions,
        ) -> Result<Vec<PaymentChallenge>, String> {
            self.mpp
                .charge_with_options(amount, Self::options(&options))
                .map_err(|e| e.to_string())
        }
    }

    fn currencies_of(challenges: &[PaymentChallenge]) -> Vec<String> {
        challenges
            .iter()
            .map(|c| c.request.decode::<ChargeRequest>().unwrap().currency)
            .collect()
    }

    fn response_offers(rejection: MppChargeRejection) -> Vec<PaymentChallenge> {
        let resp = rejection.into_response();
        assert_eq!(resp.status(), StatusCode::PAYMENT_REQUIRED);
        let values: Vec<&str> = resp
            .headers()
            .get_all(WWW_AUTHENTICATE_HEADER)
            .iter()
            .map(|v| v.to_str().unwrap())
            .collect();
        let challenges: Vec<PaymentChallenge> = parse_www_authenticate_all(values.clone())
            .into_iter()
            .map(|r| r.unwrap())
            .collect();
        // One header per offer.
        assert_eq!(values.len(), challenges.len());
        challenges
    }

    fn scoped_auth(challenger: &OffersChallenger, currency: &str) -> String {
        let req = http_types::Request::builder()
            .uri("/test")
            .body(())
            .unwrap();
        let (parts, _body) = req.into_parts();
        let challenges = challenger
            .challenges(
                OneCent::amount(),
                ChallengeOptions {
                    mppx_scope: mppx_scope_from_parts(&parts),
                    ..Default::default()
                },
            )
            .unwrap();
        let challenge = challenges
            .iter()
            .find(|c| c.request.decode::<ChargeRequest>().unwrap().currency == currency)
            .unwrap();
        let credential =
            PaymentCredential::new(challenge.to_echo(), PaymentPayload::hash("0xdeadbeef"));
        format_authorization(&credential).unwrap()
    }

    #[tokio::test]
    async fn test_extractor_returns_every_offer_in_order() {
        let err = run_extractor::<OneCent>(OffersChallenger::new(&[OUSD, USDC]), None)
            .await
            .unwrap_err();
        assert!(matches!(err, MppChargeRejection::Offers(_)));
        let offers = response_offers(err);
        assert_eq!(currencies_of(&offers), [OUSD, USDC]);
        assert!(offers
            .iter()
            .all(|c| c.verify("test-secret-key-at-least-32-bytes")));
    }

    #[tokio::test]
    async fn test_extractor_single_offer_keeps_challenge_variant() {
        let err = run_extractor::<OneCent>(OffersChallenger::new(&[PATH_USD]), None)
            .await
            .unwrap_err();
        let MppChargeRejection::Challenge(PaymentRequired(challenge)) = err else {
            panic!("single offer should use the Challenge variant: {err:?}");
        };
        assert_eq!(currencies_of(&[challenge]), [PATH_USD]);
    }

    #[tokio::test]
    async fn test_extractor_accepts_credential_for_each_offer() {
        let challenger = OffersChallenger::new(&[OUSD, USDC]);
        for currency in [OUSD, USDC] {
            let auth = scoped_auth(&challenger, currency);
            let charge = run_extractor::<OneCent>(challenger.clone(), Some(&auth))
                .await
                .unwrap_or_else(|err| panic!("{currency} credential rejected: {err:?}"));
            assert_eq!(charge.receipt.reference, "0xtxhash");
        }
    }

    #[tokio::test]
    async fn test_extractor_rejects_non_offered_currency_with_all_offers() {
        // Credential minted by a server that offered pathUSD, replayed against
        // a server (same secret/realm) that only accepts OUSD and USDC.e.
        let auth = scoped_auth(&OffersChallenger::new(&[PATH_USD]), PATH_USD);
        let err = run_extractor::<OneCent>(OffersChallenger::new(&[OUSD, USDC]), Some(&auth))
            .await
            .unwrap_err();
        assert!(matches!(err, MppChargeRejection::Problem(_)));
        assert_eq!(currencies_of(&response_offers(err)), [OUSD, USDC]);
    }

    #[tokio::test]
    async fn test_tempo_challenger_reports_typed_problems() {
        let mpp = Mpp::create(
            tempo(TempoConfig {
                recipient: RECIPIENT,
            })
            .rpc_url("http://127.0.0.1:1")
            .chain_id(CHAIN_ID)
            .secret_key("test-secret-key-at-least-32-bytes"),
        )
        .unwrap();
        let unpaid = run_extractor::<OneCent>(mpp.clone(), None)
            .await
            .unwrap_err();
        let offer = response_offers(unpaid).remove(0);
        let authorization = |challenge: &PaymentChallenge| {
            let credential =
                PaymentCredential::new(challenge.to_echo(), PaymentPayload::hash("0xdeadbeef"));
            format_authorization(&credential).unwrap()
        };
        let rejected = |rejection| match rejection {
            MppChargeRejection::Problem(rejected) => rejected,
            other => panic!("unexpected rejection: {other:?}"),
        };

        let mut forged = offer.clone();
        forged.id = "forged".to_string();
        let err = run_extractor::<OneCent>(mpp.clone(), Some(&authorization(&forged)))
            .await
            .unwrap_err();
        let problem = rejected(err);
        assert!(problem.problem.problem_type.ends_with("/invalid-challenge"));
        assert_eq!(currencies_of(&problem.offers), [OUSD, USDC]);

        // The chain ID lookup hits an unreachable RPC.
        let err = run_extractor::<OneCent>(mpp, Some(&authorization(&offer)))
            .await
            .unwrap_err();
        let problem = rejected(err);
        assert_eq!(problem.problem.status, 500);
        assert!(problem.offers.is_empty());
    }

    #[tokio::test]
    async fn test_tempo_challenger_offers_mainnet_defaults() {
        let mpp = Mpp::create(
            tempo(TempoConfig {
                recipient: RECIPIENT,
            })
            .chain_id(CHAIN_ID)
            .secret_key("test-secret-key-at-least-32-bytes"),
        )
        .unwrap();
        let challenger: Arc<dyn ChargeChallenger> = Arc::new(mpp);

        let offers = challenger
            .challenges("0.01", ChallengeOptions::default())
            .unwrap();
        assert_eq!(currencies_of(&offers), [OUSD, USDC]);
        let single = challenger
            .challenge("0.01", ChallengeOptions::default())
            .unwrap();
        assert_eq!(currencies_of(&[single]), [OUSD]);

        let body_offers = challenger
            .challenges_with_body("0.01", ChallengeOptions::default(), b"{}")
            .unwrap();
        assert_eq!(currencies_of(&body_offers), [OUSD, USDC]);
        let digest = crate::body_digest::compute(b"{}");
        assert!(body_offers
            .iter()
            .all(|c| c.digest.as_deref() == Some(digest.as_str())));

        let err = run_extractor::<OneCent>(ArcChallenger(challenger.clone()), None)
            .await
            .unwrap_err();
        assert_eq!(currencies_of(&response_offers(err)), [OUSD, USDC]);

        let err = run_body_extractor::<OneCent>(ArcChallenger(challenger), None, "{}")
            .await
            .unwrap_err();
        assert!(matches!(err, MppChargeRejection::Offers(_)));
        let offers = response_offers(err);
        assert_eq!(currencies_of(&offers), [OUSD, USDC]);
        assert!(offers
            .iter()
            .all(|c| c.digest.as_deref() == Some(digest.as_str())));
    }

    #[test]
    fn test_payment_offers_response_headers() {
        let challenger = OffersChallenger::new(&[OUSD, USDC]);
        let offers = challenger
            .challenges("0.01", ChallengeOptions::default())
            .unwrap();
        let resp = PaymentOffers(offers).into_response();
        assert_eq!(resp.status(), StatusCode::PAYMENT_REQUIRED);
        assert_eq!(
            resp.headers()
                .get_all(WWW_AUTHENTICATE_HEADER)
                .iter()
                .count(),
            2
        );
        assert_eq!(
            resp.headers().get(header::CACHE_CONTROL).unwrap(),
            "no-store"
        );
        assert_eq!(
            resp.headers().get(header::CONTENT_TYPE).unwrap(),
            "application/json"
        );
    }

    /// Forwards to a shared challenger so tests can reuse one `Arc`.
    struct ArcChallenger(Arc<dyn ChargeChallenger>);

    impl ChargeChallenger for ArcChallenger {
        fn challenge(
            &self,
            amount: &str,
            options: ChallengeOptions,
        ) -> Result<PaymentChallenge, String> {
            self.0.challenge(amount, options)
        }

        fn verify_payment(
            &self,
            credential_str: &str,
        ) -> std::pin::Pin<Box<dyn Future<Output = Result<Receipt, String>> + Send>> {
            self.0.verify_payment(credential_str)
        }

        fn challenges(
            &self,
            amount: &str,
            options: ChallengeOptions,
        ) -> Result<Vec<PaymentChallenge>, String> {
            self.0.challenges(amount, options)
        }

        fn challenges_with_body(
            &self,
            amount: &str,
            options: ChallengeOptions,
            body: &[u8],
        ) -> Result<Vec<PaymentChallenge>, String> {
            self.0.challenges_with_body(amount, options, body)
        }
    }
}