monoloop-connector 0.1.1

Abstract Connector contract, shared transport handles, FakeConnector, and ConnectorProxy
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
//! WP-08: generic StreamingHttpConnector against a local scripted server.

use axum::body::Body;
use axum::extract::Request;
use axum::http::{HeaderMap, StatusCode};
use axum::response::Response;
use axum::routing::{get, post};
use axum::Router;
use bytes::Bytes;
use monoloop_connector::{
    validate_endpoint_url, CancellationReason, ConnectionEndKind, Connector, ConnectorErrorKind,
    ConnectorLimits, MapCredentialResolver, OpenConnection, StreamingHttpConfig,
    StreamingHttpConnector, TerminationReason, TransportBufferLimits,
};
use monoloop_contracts::ConnectionId;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;

async fn bind_router(app: Router) -> (std::net::SocketAddr, tokio::task::JoinHandle<()>) {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let join = tokio::spawn(async move {
        let _ = axum::serve(listener, app).await;
    });
    // Brief settle for accept path.
    tokio::time::sleep(Duration::from_millis(10)).await;
    (addr, join)
}

fn connector(
    credentials: Arc<dyn monoloop_connector::CredentialResolver>,
    mut config: StreamingHttpConfig,
) -> StreamingHttpConnector {
    config.require_https = false;
    StreamingHttpConnector::try_new(config, credentials).unwrap()
}

async fn open_and_send(
    c: &StreamingHttpConnector,
    url: String,
    body: &[u8],
    credential_ref: Option<&str>,
    limits: ConnectorLimits,
) -> monoloop_connector::OpenedRawConnection {
    let mut open = OpenConnection::new(ConnectionId::generate(), url);
    open.credential_ref = credential_ref.map(|s| s.to_string());
    open.limits = limits;
    let pending = c.begin_open(open);
    let opened = pending.opened.await.unwrap();
    if !body.is_empty() {
        opened.input.send(Bytes::from(body.to_vec())).await.unwrap();
    }
    opened.input.finish().await.unwrap();
    opened
}

async fn read_all(opened: monoloop_connector::OpenedRawConnection) -> (Vec<u8>, ConnectionEndKind) {
    let mut collected = Vec::new();
    loop {
        match opened.output.receive().await {
            Ok(Some(chunk)) => collected.extend_from_slice(&chunk),
            Ok(None) => break,
            Err(_) => break,
        }
    }
    let end = opened.completion.wait().await;
    (collected, end.kind)
}

#[test]
fn endpoint_validation() {
    assert!(validate_endpoint_url("http://127.0.0.1:9/v1", false).is_ok());
    assert!(validate_endpoint_url("https://example.com/v1", true).is_ok());
    assert!(validate_endpoint_url("http://example.com/v1", true).is_err());
    assert!(validate_endpoint_url("http://user:pass@example.com/", false).is_err());
    assert!(validate_endpoint_url("ftp://example.com/", false).is_err());
    assert!(validate_endpoint_url("", false).is_err());
    assert!(validate_endpoint_url("not a url", false).is_err());
}

#[tokio::test]
async fn fragmented_sse_body_round_trip() {
    let app = Router::new().route(
        "/v1/stream",
        post(|req: Request| async move {
            let body = axum::body::to_bytes(req.into_body(), 1024 * 1024)
                .await
                .unwrap();
            assert_eq!(&body[..], b"{\"hello\":1}");
            // Fragmented SSE-looking body (connector must not parse it).
            let chunks = [
                Bytes::from_static(b"data: {\"a\":"),
                Bytes::from_static(b"1}\n\n"),
                Bytes::from_static(b"data: [DONE]\n\n"),
            ];
            let stream =
                futures_util::stream::iter(chunks.into_iter().map(Ok::<_, std::io::Error>));
            Response::builder()
                .status(StatusCode::OK)
                .body(Body::from_stream(stream))
                .unwrap()
        }),
    );
    let (addr, _join) = bind_router(app).await;
    let url = format!("http://{addr}/v1/stream");
    let c = connector(
        Arc::new(MapCredentialResolver::empty()),
        StreamingHttpConfig::default(),
    );
    let opened = open_and_send(&c, url, b"{\"hello\":1}", None, ConnectorLimits::default()).await;
    let (body, kind) = read_all(opened).await;
    assert_eq!(kind, ConnectionEndKind::RemoteEof);
    assert_eq!(
        String::from_utf8_lossy(&body),
        "data: {\"a\":1}\n\ndata: [DONE]\n\n"
    );
}

#[tokio::test]
async fn non_success_status_bounded_error() {
    let app = Router::new().route(
        "/fail",
        post(|| async {
            (
                StatusCode::UNAUTHORIZED,
                "secret-token-must-not-leak-into-connector-error",
            )
        }),
    );
    let (addr, _join) = bind_router(app).await;
    let c = connector(
        Arc::new(MapCredentialResolver::empty()),
        StreamingHttpConfig::default(),
    );
    let opened = open_and_send(
        &c,
        format!("http://{addr}/fail"),
        b"{}",
        None,
        ConnectorLimits::default(),
    )
    .await;
    let end = opened.completion.wait().await;
    assert_eq!(end.kind, ConnectionEndKind::TransportFailure);
    let msg = end.safe_transport_error.unwrap_or_default();
    assert!(msg.contains("401") || msg.contains("http status"));
    assert!(!msg.contains("secret-token"));
}

#[tokio::test]
async fn credential_resolution_and_header() {
    let saw_auth = Arc::new(AtomicUsize::new(0));
    let saw = Arc::clone(&saw_auth);
    let app = Router::new().route(
        "/auth",
        post(move |headers: HeaderMap| {
            let saw = Arc::clone(&saw);
            async move {
                if headers
                    .get(axum::http::header::AUTHORIZATION)
                    .and_then(|v| v.to_str().ok())
                    == Some("Bearer test-secret")
                {
                    saw.store(1, Ordering::SeqCst);
                }
                StatusCode::OK
            }
        }),
    );
    let (addr, _join) = bind_router(app).await;
    let resolver = Arc::new(MapCredentialResolver::new([(
        "cred-a",
        "Bearer test-secret",
    )]));
    let c = connector(resolver, StreamingHttpConfig::default());
    let opened = open_and_send(
        &c,
        format!("http://{addr}/auth"),
        b"{}",
        Some("cred-a"),
        ConnectorLimits::default(),
    )
    .await;
    let end = opened.completion.wait().await;
    assert_eq!(end.kind, ConnectionEndKind::RemoteEof);
    assert_eq!(saw_auth.load(Ordering::SeqCst), 1);

    // Missing credential ref fails at open.
    let mut open = OpenConnection::new(ConnectionId::generate(), format!("http://{addr}/auth"));
    open.credential_ref = Some("missing".into());
    let pending = c.begin_open(open);
    let err = match pending.opened.await {
        Err(e) => e,
        Ok(_) => panic!("expected credential failure"),
    };
    assert_eq!(err.kind, ConnectorErrorKind::CredentialUnavailable);
    assert!(!format!("{err:?}").contains("test-secret"));
}

#[tokio::test]
async fn secrets_absent_from_debug() {
    let resolver = MapCredentialResolver::new([("k", "super-secret-value")]);
    let c = connector(Arc::new(resolver), StreamingHttpConfig::default());
    let dbg = format!("{c:?}");
    assert!(!dbg.contains("super-secret-value"));
    assert!(dbg.contains("<injected>") || dbg.contains("StreamingHttpConnector"));
}

#[tokio::test]
async fn max_request_bytes_plus_one() {
    let app = Router::new().route("/ok", post(|| async { StatusCode::OK }));
    let (addr, _join) = bind_router(app).await;
    let config = StreamingHttpConfig {
        max_request_bytes: 8,
        ..Default::default()
    };
    let c = connector(Arc::new(MapCredentialResolver::empty()), config);
    let mut open = OpenConnection::new(ConnectionId::generate(), format!("http://{addr}/ok"));
    open.limits.buffers = TransportBufferLimits {
        max_queued_input_bytes: 1024,
        max_queued_output_bytes: 1024,
        max_chunk_bytes: 64,
    };
    let pending = c.begin_open(open);
    let opened = pending.opened.await.unwrap();
    opened.input.send(Bytes::from(vec![b'x'; 9])).await.unwrap();
    opened.input.finish().await.unwrap();
    let end = opened.completion.wait().await;
    assert_eq!(end.kind, ConnectionEndKind::TransportFailure);
    assert!(end
        .safe_transport_error
        .unwrap_or_default()
        .contains("max_request_bytes"));
}

#[tokio::test]
async fn max_response_bytes_plus_one() {
    let app = Router::new().route(
        "/big",
        post(|| async { "0123456789abcdef" }), // 16 bytes
    );
    let (addr, _join) = bind_router(app).await;
    let config = StreamingHttpConfig {
        max_response_bytes: 8,
        ..Default::default()
    };
    let c = connector(Arc::new(MapCredentialResolver::empty()), config);
    let opened = open_and_send(
        &c,
        format!("http://{addr}/big"),
        b"{}",
        None,
        ConnectorLimits::default(),
    )
    .await;
    let end = opened.completion.wait().await;
    assert_eq!(end.kind, ConnectionEndKind::TransportFailure);
    assert!(end
        .safe_transport_error
        .unwrap_or_default()
        .contains("max_response_bytes"));
}

#[tokio::test]
async fn cancel_while_request_in_flight() {
    let app = Router::new().route(
        "/ok",
        post(|| async {
            tokio::time::sleep(Duration::from_secs(5)).await;
            StatusCode::OK
        }),
    );
    let (addr, _join) = bind_router(app).await;
    let c = connector(
        Arc::new(MapCredentialResolver::empty()),
        StreamingHttpConfig::default(),
    );
    let mut open = OpenConnection::new(ConnectionId::generate(), format!("http://{addr}/ok"));
    open.limits.connect_deadline = Duration::from_secs(10);
    let pending = c.begin_open(open);
    let control = pending.control.clone();
    let opened = pending.opened.await.unwrap();
    opened.input.send(Bytes::from_static(b"{}")).await.unwrap();
    opened.input.finish().await.unwrap();
    // Cancel while HTTP request is in flight.
    tokio::time::sleep(Duration::from_millis(20)).await;
    control.cancel(CancellationReason::CallerRequested);
    let end = opened.completion.wait().await;
    assert!(
        matches!(
            end.kind,
            ConnectionEndKind::Cancelled | ConnectionEndKind::Terminated
        ),
        "got {:?}",
        end.kind
    );
}

#[tokio::test]
async fn cancel_before_request_send() {
    let app = Router::new().route("/ok", post(|| async { StatusCode::OK }));
    let (addr, _join) = bind_router(app).await;
    let c = connector(
        Arc::new(MapCredentialResolver::empty()),
        StreamingHttpConfig::default(),
    );
    let pending = c.begin_open(OpenConnection::new(
        ConnectionId::generate(),
        format!("http://{addr}/ok"),
    ));
    let control = pending.control.clone();
    let opened = pending.opened.await.unwrap();
    // Cancel while waiting for body finish.
    control.cancel(CancellationReason::CallerRequested);
    let end = opened.completion.wait().await;
    assert_eq!(end.kind, ConnectionEndKind::Cancelled);
}

#[tokio::test]
async fn terminate_during_open_collect() {
    let app = Router::new().route("/ok", post(|| async { StatusCode::OK }));
    let (addr, _join) = bind_router(app).await;
    let c = connector(
        Arc::new(MapCredentialResolver::empty()),
        StreamingHttpConfig::default(),
    );
    let pending = c.begin_open(OpenConnection::new(
        ConnectionId::generate(),
        format!("http://{addr}/ok"),
    ));
    let control = pending.control.clone();
    let opened = pending.opened.await.unwrap();
    control.terminate(TerminationReason::CallerForced);
    let end = opened.completion.wait().await;
    assert_eq!(end.kind, ConnectionEndKind::Terminated);
}

#[tokio::test]
async fn idle_timeout_on_stalled_stream() {
    let app = Router::new().route(
        "/stall",
        post(|| async {
            let (tx, rx) = tokio::sync::mpsc::channel::<Result<Bytes, std::io::Error>>(2);
            tokio::spawn(async move {
                let _ = tx.send(Ok(Bytes::from_static(b"part"))).await;
                // Never send more — client idle timeout should fire.
                tokio::time::sleep(Duration::from_secs(30)).await;
            });
            // Use Stream without tokio-stream: unfold
            let stream = futures_util::stream::unfold(rx, |mut rx| async move {
                rx.recv().await.map(|item| (item, rx))
            });
            Response::builder()
                .status(StatusCode::OK)
                .body(Body::from_stream(stream))
                .unwrap()
        }),
    );
    let (addr, _join) = bind_router(app).await;
    let config = StreamingHttpConfig {
        idle_timeout: Duration::from_millis(100),
        request_timeout: Duration::from_secs(5),
        ..Default::default()
    };
    let c = connector(Arc::new(MapCredentialResolver::empty()), config);
    let opened = open_and_send(
        &c,
        format!("http://{addr}/stall"),
        b"{}",
        None,
        ConnectorLimits::default(),
    )
    .await;
    let end = opened.completion.wait().await;
    assert_eq!(end.kind, ConnectionEndKind::TransportFailure);
    assert!(end
        .safe_transport_error
        .unwrap_or_default()
        .contains("idle"));
}

#[tokio::test]
async fn connection_pool_reuse_no_semantic_session() {
    let hits = Arc::new(AtomicUsize::new(0));
    let hits2 = Arc::clone(&hits);
    let app = Router::new().route(
        "/n",
        post(move || {
            let hits2 = Arc::clone(&hits2);
            async move {
                hits2.fetch_add(1, Ordering::SeqCst);
                StatusCode::OK
            }
        }),
    );
    let (addr, _join) = bind_router(app).await;
    let c = connector(
        Arc::new(MapCredentialResolver::empty()),
        StreamingHttpConfig::default(),
    );
    for _ in 0..3 {
        let opened = open_and_send(
            &c,
            format!("http://{addr}/n"),
            b"{}",
            None,
            ConnectorLimits::default(),
        )
        .await;
        let end = opened.completion.wait().await;
        assert_eq!(end.kind, ConnectionEndKind::RemoteEof);
    }
    assert_eq!(hits.load(Ordering::SeqCst), 3);
    // Each open is an independent connection identity (no session reuse semantics).
}

#[tokio::test]
async fn malformed_endpoint_fails_open() {
    let c = connector(
        Arc::new(MapCredentialResolver::empty()),
        StreamingHttpConfig::default(),
    );
    let pending = c.begin_open(OpenConnection::new(ConnectionId::generate(), "not-a-url"));
    let err = match pending.opened.await {
        Err(e) => e,
        Ok(_) => panic!("expected config failure"),
    };
    assert_eq!(err.kind, ConnectorErrorKind::ConfigurationInvalid);
}

#[tokio::test]
async fn health_get_unused() {
    // Ensure bind helper works with GET too (sanity).
    let app = Router::new().route("/h", get(|| async { "ok" }));
    let (addr, join) = bind_router(app).await;
    let client = reqwest::Client::new();
    let r = client.get(format!("http://{addr}/h")).send().await.unwrap();
    assert_eq!(r.status(), StatusCode::OK);
    join.abort();
}