orion-server 1.0.0

Turn business logic into live REST/Kafka services. Declare workflows as JSON and Orion runs them, with rate limiting, circuit breakers, versioning, and observability built in
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
use std::time::Duration;

use dataflow_rs::engine::error::DataflowError;
use serde_json::Value;

use crate::connector::{AuthConfig, HttpConnectorConfig};

/// Build a URL from a base URL and optional path segment.
pub fn build_url(base: &str, path: Option<&str>) -> String {
    match path {
        Some(p) if !p.is_empty() => {
            let base = base.trim_end_matches('/');
            let path = p.trim_start_matches('/');
            format!("{base}/{path}")
        }
        _ => base.to_string(),
    }
}

/// Apply authentication to a request builder.
pub fn apply_auth(req: reqwest::RequestBuilder, auth: &AuthConfig) -> reqwest::RequestBuilder {
    match auth {
        AuthConfig::Bearer { token } => req.header("authorization", format!("Bearer {token}")),
        AuthConfig::Basic { username, password } => req.basic_auth(username, Some(password)),
        AuthConfig::ApiKey { header, key } => req.header(header, key),
    }
}

/// Redirect-hop cap for the manual follower in [`execute_request`]. The shared
/// client is built with `redirect::Policy::none()` (see main.rs), so every hop
/// returns here and passes SSRF validation before being followed.
const MAX_REDIRECTS: usize = 5;

/// Execute an HTTP request with connector config applied.
///
/// Builds the request with connector headers, auth, optional task-level headers,
/// optional body, and timeout. Returns the parsed JSON response. Redirects are
/// followed manually (up to `MAX_REDIRECTS`) with SSRF re-validation per hop.
#[tracing::instrument(skip(client, task_headers, http_config, body))]
pub async fn execute_request(
    client: &reqwest::Client,
    method: &reqwest::Method,
    url: &str,
    task_headers: Option<&std::collections::HashMap<String, String>>,
    http_config: &HttpConnectorConfig,
    body: Option<&Value>,
    timeout: Duration,
) -> dataflow_rs::Result<Value> {
    let original = url::Url::parse(url)
        .map_err(|e| DataflowError::Validation(format!("Invalid URL '{url}': {e}")))?;
    let mut current = original.clone();
    let mut method = method.clone();
    let mut body = body;

    for _ in 0..=MAX_REDIRECTS {
        // SSRF protection: block requests to private/internal IPs.
        // `allow_private_urls` exempts only the connector's own endpoint — a
        // redirect is server-controlled data, so any hop leaving the original
        // host:port is validated even for private-allowed connectors.
        let own_endpoint = same_endpoint(&current, &original);
        if !(http_config.allow_private_urls && own_endpoint)
            && let Err(msg) = crate::validation::validate_url_not_private(current.as_str()).await
        {
            return Err(DataflowError::function_execution(
                format!("SSRF protection: {msg}"),
                None,
            ));
        }

        let mut req = client
            .request(method.clone(), current.clone())
            .timeout(timeout);

        // Inject W3C trace context headers (traceparent/tracestate) for distributed tracing
        {
            let mut trace_headers = std::collections::HashMap::new();
            crate::server::trace_context::inject_trace_context(&mut trace_headers);
            for (k, v) in &trace_headers {
                req = req.header(k, v);
            }
        }

        // Connector headers, auth, and task headers can carry credentials —
        // they go only to the connector's own endpoint, never on a cross-host hop.
        if own_endpoint {
            // Apply connector default headers (lowest priority)
            for (k, v) in &http_config.headers {
                req = req.header(k, v);
            }

            // Apply auth headers (override connector defaults)
            if let Some(ref auth) = http_config.auth {
                req = apply_auth(req, auth);
            }
        }

        // Apply default content-type and body
        if let Some(b) = body {
            req = req.header("content-type", "application/json").json(b);
        }

        // Apply task-level headers LAST (highest priority — workflow developer's explicit choice wins)
        if own_endpoint && let Some(headers) = task_headers {
            for (k, v) in headers {
                req = req.header(k, v);
            }
        }

        let response = req.send().await.map_err(|e| {
            if e.is_timeout() {
                DataflowError::Timeout(format!("HTTP request to {current} timed out"))
            } else {
                DataflowError::Io(format!("HTTP request to {current} failed: {e}"))
            }
        })?;

        if let Some(next) = redirect_target(&response, &current)? {
            // Mirror reqwest's default policy: 301/302/303 turn a non-GET/HEAD
            // request into a bodyless GET; 307/308 re-send method and body.
            if matches!(response.status().as_u16(), 301..=303)
                && method != reqwest::Method::GET
                && method != reqwest::Method::HEAD
            {
                method = reqwest::Method::GET;
                body = None;
            }
            current = next;
            continue;
        }

        return read_json_response(response, &current, http_config.max_response_size).await;
    }

    Err(DataflowError::function_execution(
        format!("Stopped after {MAX_REDIRECTS} redirects requesting {url}"),
        None,
    ))
}

/// Same scheme-default-aware host:port — the boundary within which connector
/// credentials and the `allow_private_urls` exemption apply.
fn same_endpoint(a: &url::Url, b: &url::Url) -> bool {
    a.host_str().is_some()
        && a.host_str() == b.host_str()
        && a.port_or_known_default() == b.port_or_known_default()
}

/// The target of a redirect response, resolved against the current URL.
/// `None` when the response is not a followable redirect.
fn redirect_target(
    response: &reqwest::Response,
    current: &url::Url,
) -> dataflow_rs::Result<Option<url::Url>> {
    if !matches!(response.status().as_u16(), 301 | 302 | 303 | 307 | 308) {
        return Ok(None);
    }
    let Some(location) = response.headers().get(reqwest::header::LOCATION) else {
        return Ok(None);
    };
    let location = location.to_str().map_err(|_| {
        DataflowError::function_execution(
            format!("Redirect from {current} has a non-ASCII Location header"),
            None,
        )
    })?;
    let next = current.join(location).map_err(|e| {
        DataflowError::function_execution(
            format!("Redirect from {current} has invalid Location '{location}': {e}"),
            None,
        )
    })?;
    if !matches!(next.scheme(), "http" | "https") {
        return Err(DataflowError::function_execution(
            format!(
                "Redirect from {current} targets unsupported scheme '{}'",
                next.scheme()
            ),
            None,
        ));
    }
    Ok(Some(next))
}

/// Read a (non-redirect) response body as JSON, enforcing `max_size`.
///
/// The limit is enforced *while streaming*: a chunked response with no
/// `Content-Length` must not get to sit fully in memory before the size
/// check — that is the exact OOM the limit exists to prevent.
async fn read_json_response(
    mut response: reqwest::Response,
    url: &url::Url,
    max_size: usize,
) -> dataflow_rs::Result<Value> {
    let status = response.status();

    // Check Content-Length hint before reading body
    if let Some(content_length) = response.content_length()
        && content_length as usize > max_size
    {
        return Err(DataflowError::function_execution(
            format!(
                "Response from {url} declared Content-Length {content_length} exceeds limit of {max_size} bytes"
            ),
            None,
        ));
    }

    if !status.is_success() {
        // Read at most `max_size` bytes of the error body for the message;
        // anything beyond that is dropped, never buffered. Read errors end
        // the body early (the status alone still makes a useful error).
        let mut body_bytes = Vec::new();
        while let Some(chunk) = response.chunk().await.ok().flatten() {
            let room = max_size.saturating_sub(body_bytes.len());
            let take = chunk.len().min(room);
            body_bytes.extend_from_slice(&chunk[..take]);
            if take < chunk.len() {
                break;
            }
        }
        let body_text = String::from_utf8_lossy(&body_bytes);
        return Err(DataflowError::http(
            status.as_u16(),
            format!("HTTP {status} from {url}: {body_text}"),
        ));
    }

    let mut body_bytes = Vec::new();
    while let Some(chunk) = response.chunk().await.map_err(|e| {
        DataflowError::function_execution(
            format!("Failed to read response body from {url}: {e}"),
            None,
        )
    })? {
        if body_bytes.len() + chunk.len() > max_size {
            return Err(DataflowError::function_execution(
                format!("Response body from {url} exceeds limit of {max_size} bytes"),
                None,
            ));
        }
        body_bytes.extend_from_slice(&chunk);
    }

    let response_body: Value = serde_json::from_slice(&body_bytes).map_err(|e| {
        DataflowError::function_execution(
            format!("Failed to parse response from {url} as JSON: {e}"),
            None,
        )
    })?;
    Ok(response_body)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_build_url() {
        assert_eq!(
            build_url("https://api.example.com", Some("/users")),
            "https://api.example.com/users"
        );
        assert_eq!(
            build_url("https://api.example.com/", Some("/users")),
            "https://api.example.com/users"
        );
        assert_eq!(
            build_url("https://api.example.com", None),
            "https://api.example.com"
        );
    }

    #[test]
    fn test_build_url_no_path() {
        assert_eq!(
            build_url("https://api.example.com", None),
            "https://api.example.com"
        );
    }

    #[test]
    fn test_build_url_empty_path() {
        assert_eq!(
            build_url("https://api.example.com", Some("")),
            "https://api.example.com"
        );
    }

    #[test]
    fn test_build_url_trims_slashes() {
        assert_eq!(
            build_url("https://api.example.com///", Some("///path")),
            "https://api.example.com/path"
        );
    }

    #[test]
    fn test_apply_auth_bearer() {
        let client = reqwest::Client::new();
        let auth = AuthConfig::Bearer {
            token: "tok123".to_string(),
        };
        let req = apply_auth(client.get("http://localhost"), &auth);
        let built = req.build().expect("test");
        assert_eq!(
            built
                .headers()
                .get("authorization")
                .expect("test")
                .to_str()
                .expect("test"),
            "Bearer tok123"
        );
    }

    #[test]
    fn test_apply_auth_api_key() {
        let client = reqwest::Client::new();
        let auth = AuthConfig::ApiKey {
            header: "x-api-key".to_string(),
            key: "secret123".to_string(),
        };
        let req = apply_auth(client.get("http://localhost"), &auth);
        let built = req.build().expect("test");
        assert_eq!(
            built
                .headers()
                .get("x-api-key")
                .expect("test")
                .to_str()
                .expect("test"),
            "secret123"
        );
    }

    #[tokio::test]
    async fn test_execute_request_success() {
        // Start a mock server using axum
        let mock_app = axum::Router::new().route(
            "/test",
            axum::routing::get(|| async { axum::Json(serde_json::json!({"result": "success"})) }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("test");
        let addr = listener.local_addr().expect("test");
        tokio::spawn(async move {
            axum::serve(listener, mock_app).await.expect("test");
        });

        let client = reqwest::Client::new();
        let http_config = HttpConnectorConfig {
            retry_non_idempotent: false,
            url: format!("http://{}", addr),
            method: String::new(),
            headers: std::collections::HashMap::new(),
            auth: None,
            retry: crate::connector::RetryConfig::default(),
            max_response_size: 10 * 1024 * 1024,
            allow_private_urls: true, // Tests use localhost
            operations: Default::default(),
        };

        let result = execute_request(
            &client,
            &reqwest::Method::GET,
            &format!("http://{}/test", addr),
            None,
            &http_config,
            None,
            std::time::Duration::from_secs(5),
        )
        .await;

        assert!(result.is_ok());
        let val = result.expect("test");
        assert_eq!(val["result"], "success");
    }

    #[tokio::test]
    async fn test_execute_request_with_headers_auth_and_body() {
        let mock_app = axum::Router::new().route(
            "/post-test",
            axum::routing::post(|| async { axum::Json(serde_json::json!({"received": true})) }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("test");
        let addr = listener.local_addr().expect("test");
        tokio::spawn(async move {
            axum::serve(listener, mock_app).await.expect("test");
        });

        let client = reqwest::Client::new();
        let mut headers = std::collections::HashMap::new();
        headers.insert("x-custom".to_string(), "custom-value".to_string());

        let http_config = HttpConnectorConfig {
            retry_non_idempotent: false,
            url: format!("http://{}", addr),
            method: String::new(),
            headers: std::collections::HashMap::from([(
                "x-connector-header".to_string(),
                "conn-val".to_string(),
            )]),
            auth: Some(AuthConfig::Bearer {
                token: "test-token".to_string(),
            }),
            retry: crate::connector::RetryConfig::default(),
            max_response_size: 10 * 1024 * 1024,
            allow_private_urls: true, // Tests use localhost
            operations: Default::default(),
        };

        let body = serde_json::json!({"data": "payload"});

        let result = execute_request(
            &client,
            &reqwest::Method::POST,
            &format!("http://{}/post-test", addr),
            Some(&headers),
            &http_config,
            Some(&body),
            std::time::Duration::from_secs(5),
        )
        .await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_execute_request_non_success_status() {
        let mock_app = axum::Router::new().route(
            "/error",
            axum::routing::get(|| async { (axum::http::StatusCode::BAD_REQUEST, "Bad Request") }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("test");
        let addr = listener.local_addr().expect("test");
        tokio::spawn(async move {
            axum::serve(listener, mock_app).await.expect("test");
        });

        let client = reqwest::Client::new();
        let http_config = HttpConnectorConfig {
            retry_non_idempotent: false,
            url: format!("http://{}", addr),
            method: String::new(),
            headers: std::collections::HashMap::new(),
            auth: None,
            retry: crate::connector::RetryConfig::default(),
            max_response_size: 10 * 1024 * 1024,
            allow_private_urls: true, // Tests use localhost
            operations: Default::default(),
        };

        let result = execute_request(
            &client,
            &reqwest::Method::GET,
            &format!("http://{}/error", addr),
            None,
            &http_config,
            None,
            std::time::Duration::from_secs(5),
        )
        .await;

        assert!(result.is_err());
        let err = result.expect_err("test");
        assert!(err.to_string().contains("400"));
    }

    #[tokio::test]
    async fn test_execute_request_non_json_response() {
        let mock_app = axum::Router::new().route(
            "/text",
            axum::routing::get(|| async { "plain text response" }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("test");
        let addr = listener.local_addr().expect("test");
        tokio::spawn(async move {
            axum::serve(listener, mock_app).await.expect("test");
        });

        let client = reqwest::Client::new();
        let http_config = HttpConnectorConfig {
            retry_non_idempotent: false,
            url: format!("http://{}", addr),
            method: String::new(),
            headers: std::collections::HashMap::new(),
            auth: None,
            retry: crate::connector::RetryConfig::default(),
            max_response_size: 10 * 1024 * 1024,
            allow_private_urls: true, // Tests use localhost
            operations: Default::default(),
        };

        let result = execute_request(
            &client,
            &reqwest::Method::GET,
            &format!("http://{}/text", addr),
            None,
            &http_config,
            None,
            std::time::Duration::from_secs(5),
        )
        .await;

        // Should fail to parse as JSON
        assert!(result.is_err());
        assert!(result.expect_err("test").to_string().contains("parse"));
    }

    #[tokio::test]
    async fn test_execute_request_response_too_large() {
        let mock_app = axum::Router::new().route(
            "/large",
            axum::routing::get(|| async {
                axum::Json(serde_json::json!({"data": "x".repeat(200)}))
            }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("test");
        let addr = listener.local_addr().expect("test");
        tokio::spawn(async move {
            axum::serve(listener, mock_app).await.expect("test");
        });

        let client = reqwest::Client::new();
        let http_config = HttpConnectorConfig {
            retry_non_idempotent: false,
            url: format!("http://{}", addr),
            method: String::new(),
            headers: std::collections::HashMap::new(),
            auth: None,
            retry: crate::connector::RetryConfig::default(),
            max_response_size: 10,    // Very small limit
            allow_private_urls: true, // Tests use localhost
            operations: Default::default(),
        };

        let result = execute_request(
            &client,
            &reqwest::Method::GET,
            &format!("http://{}/large", addr),
            None,
            &http_config,
            None,
            std::time::Duration::from_secs(5),
        )
        .await;

        assert!(result.is_err());
        assert!(result.expect_err("test").to_string().contains("exceed"));
    }

    #[tokio::test]
    async fn test_execute_request_timeout() {
        let mock_app = axum::Router::new().route(
            "/slow",
            axum::routing::get(|| async {
                tokio::time::sleep(std::time::Duration::from_secs(5)).await;
                axum::Json(serde_json::json!({"slow": true}))
            }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("test");
        let addr = listener.local_addr().expect("test");
        tokio::spawn(async move {
            axum::serve(listener, mock_app).await.expect("test");
        });

        let client = reqwest::Client::new();
        let http_config = HttpConnectorConfig {
            retry_non_idempotent: false,
            url: format!("http://{}", addr),
            method: String::new(),
            headers: std::collections::HashMap::new(),
            auth: None,
            retry: crate::connector::RetryConfig::default(),
            max_response_size: 10 * 1024 * 1024,
            allow_private_urls: true, // Tests use localhost
            operations: Default::default(),
        };

        let result = execute_request(
            &client,
            &reqwest::Method::GET,
            &format!("http://{}/slow", addr),
            None,
            &http_config,
            None,
            std::time::Duration::from_millis(100), // Very short timeout
        )
        .await;

        assert!(result.is_err());
        assert!(result.expect_err("test").to_string().contains("timed out"));
    }

    #[tokio::test]
    async fn test_execute_request_connection_refused() {
        let client = reqwest::Client::new();
        let http_config = HttpConnectorConfig {
            retry_non_idempotent: false,
            url: "http://127.0.0.1:1".to_string(),
            method: String::new(),
            headers: std::collections::HashMap::new(),
            auth: None,
            retry: crate::connector::RetryConfig::default(),
            max_response_size: 10 * 1024 * 1024,
            allow_private_urls: true, // Tests use localhost
            operations: Default::default(),
        };

        let result = execute_request(
            &client,
            &reqwest::Method::GET,
            "http://127.0.0.1:1/test",
            None,
            &http_config,
            None,
            std::time::Duration::from_secs(1),
        )
        .await;

        assert!(result.is_err());
        assert!(result.expect_err("test").to_string().contains("failed"));
    }

    /// Mirrors the production client (main.rs): the manual follower in
    /// `execute_request` only sees 3xx responses when reqwest doesn't follow.
    fn redirectless_client() -> reqwest::Client {
        reqwest::Client::builder()
            .redirect(reqwest::redirect::Policy::none())
            .build()
            .expect("test")
    }

    fn localhost_config(addr: std::net::SocketAddr) -> HttpConnectorConfig {
        HttpConnectorConfig {
            retry_non_idempotent: false,
            url: format!("http://{}", addr),
            method: String::new(),
            headers: std::collections::HashMap::new(),
            auth: None,
            retry: crate::connector::RetryConfig::default(),
            max_response_size: 10 * 1024 * 1024,
            allow_private_urls: true, // Tests use localhost
            operations: Default::default(),
        }
    }

    async fn spawn_mock(app: axum::Router) -> std::net::SocketAddr {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("test");
        let addr = listener.local_addr().expect("test");
        tokio::spawn(async move {
            axum::serve(listener, app).await.expect("test");
        });
        addr
    }

    #[tokio::test]
    async fn test_redirect_to_private_target_refused() {
        // allow_private_urls covers only the connector's own endpoint; a
        // redirect pointing anywhere else must still pass SSRF validation.
        let mock_app = axum::Router::new().route(
            "/redirect",
            axum::routing::get(|| async {
                (
                    axum::http::StatusCode::FOUND,
                    [(
                        axum::http::header::LOCATION,
                        "http://169.254.169.254/latest/meta-data",
                    )],
                )
            }),
        );
        let addr = spawn_mock(mock_app).await;

        let result = execute_request(
            &redirectless_client(),
            &reqwest::Method::GET,
            &format!("http://{}/redirect", addr),
            None,
            &localhost_config(addr),
            None,
            std::time::Duration::from_secs(5),
        )
        .await;

        let err = result.expect_err("test").to_string();
        assert!(err.contains("SSRF protection"), "unexpected error: {err}");
    }

    #[tokio::test]
    async fn test_redirect_followed_within_own_endpoint() {
        let mock_app = axum::Router::new()
            .route(
                "/a",
                axum::routing::get(|| async {
                    (
                        axum::http::StatusCode::FOUND,
                        [(axum::http::header::LOCATION, "/b")],
                    )
                }),
            )
            .route(
                "/b",
                axum::routing::get(|| async { axum::Json(serde_json::json!({"hop": "b"})) }),
            );
        let addr = spawn_mock(mock_app).await;

        let result = execute_request(
            &redirectless_client(),
            &reqwest::Method::GET,
            &format!("http://{}/a", addr),
            None,
            &localhost_config(addr),
            None,
            std::time::Duration::from_secs(5),
        )
        .await;

        assert_eq!(result.expect("test")["hop"], "b");
    }

    #[tokio::test]
    async fn test_redirect_loop_is_capped() {
        let mock_app = axum::Router::new().route(
            "/loop",
            axum::routing::get(|| async {
                (
                    axum::http::StatusCode::FOUND,
                    [(axum::http::header::LOCATION, "/loop")],
                )
            }),
        );
        let addr = spawn_mock(mock_app).await;

        let result = execute_request(
            &redirectless_client(),
            &reqwest::Method::GET,
            &format!("http://{}/loop", addr),
            None,
            &localhost_config(addr),
            None,
            std::time::Duration::from_secs(5),
        )
        .await;

        let err = result.expect_err("test").to_string();
        assert!(err.contains("redirects"), "unexpected error: {err}");
    }

    #[tokio::test]
    async fn test_redirect_303_downgrades_post_to_get() {
        let mock_app = axum::Router::new()
            .route(
                "/submit",
                axum::routing::post(|| async {
                    (
                        axum::http::StatusCode::SEE_OTHER,
                        [(axum::http::header::LOCATION, "/done")],
                    )
                }),
            )
            .route(
                "/done",
                // GET-only: the hop only succeeds if the method was downgraded
                axum::routing::get(|| async { axum::Json(serde_json::json!({"done": true})) }),
            );
        let addr = spawn_mock(mock_app).await;

        let body = serde_json::json!({"data": "payload"});
        let result = execute_request(
            &redirectless_client(),
            &reqwest::Method::POST,
            &format!("http://{}/submit", addr),
            None,
            &localhost_config(addr),
            Some(&body),
            std::time::Duration::from_secs(5),
        )
        .await;

        assert_eq!(result.expect("test")["done"], true);
    }

    #[test]
    fn test_apply_auth_basic() {
        let client = reqwest::Client::new();
        let auth = AuthConfig::Basic {
            username: "user".to_string(),
            password: "pass".to_string(),
        };
        let req = apply_auth(client.get("http://localhost"), &auth);
        let built = req.build().expect("test");
        let auth_header = built
            .headers()
            .get("authorization")
            .expect("test")
            .to_str()
            .expect("test");
        assert!(auth_header.starts_with("Basic "));
    }
}