-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathplatform.rs
More file actions
821 lines (739 loc) · 31 KB
/
Copy pathplatform.rs
File metadata and controls
821 lines (739 loc) · 31 KB
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
use std::future::Future;
use std::net::IpAddr;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use async_trait::async_trait;
use edgezero_core::http::{HeaderMap, HeaderName, HeaderValue, header};
use error_stack::{Report, ResultExt as _};
use trusted_server_core::platform::{
ClientInfo, GeoInfo, PlatformBackend, PlatformBackendSpec, PlatformConfigStore, PlatformError,
PlatformGeo, PlatformHttpClient, PlatformHttpRequest, PlatformPendingRequest, PlatformResponse,
PlatformSecretStore, PlatformSelectResult, RuntimeServices, StoreId, StoreName,
};
// ---------------------------------------------------------------------------
// Env-var naming helpers
// ---------------------------------------------------------------------------
/// Normalize a store name or key for use as an environment-variable segment.
///
/// Uppercases and replaces hyphens, dots, and spaces with underscores.
fn normalize_env_segment(s: &str) -> String {
s.to_uppercase().replace(['-', '.', ' '], "_")
}
fn config_env_var(store_name: &str, key: &str) -> String {
format!(
"TRUSTED_SERVER_CONFIG_{}_{}",
normalize_env_segment(store_name),
normalize_env_segment(key),
)
}
fn secret_env_var(store_name: &str, key: &str) -> String {
format!(
"TRUSTED_SERVER_SECRET_{}_{}",
normalize_env_segment(store_name),
normalize_env_segment(key),
)
}
// ---------------------------------------------------------------------------
// PlatformConfigStore
// ---------------------------------------------------------------------------
/// Environment-variable–backed config store for the Axum dev server.
///
/// Reads from `TRUSTED_SERVER_CONFIG_{STORE}_{KEY}` (uppercased, hyphens→underscores).
/// Write operations are unsupported in local development.
pub struct AxumPlatformConfigStore;
impl PlatformConfigStore for AxumPlatformConfigStore {
fn get(&self, store_name: &StoreName, key: &str) -> Result<String, Report<PlatformError>> {
let var_name = config_env_var(store_name.as_ref(), key);
std::env::var(&var_name).map_err(|_| {
Report::new(PlatformError::ConfigStore).attach(format!(
"env var '{var_name}' not set — export it to supply this config value"
))
})
}
fn put(
&self,
store_id: &StoreId,
key: &str,
_value: &str,
) -> Result<(), Report<PlatformError>> {
log::warn!(
"AxumPlatformConfigStore: write to store '{}' key '{}' ignored \
(config store writes are not supported on the Axum dev server)",
store_id.as_ref(),
key
);
Err(Report::new(PlatformError::ConfigStore)
.attach("config store writes are not supported on the Axum dev server"))
}
fn delete(&self, store_id: &StoreId, key: &str) -> Result<(), Report<PlatformError>> {
log::warn!(
"AxumPlatformConfigStore: delete from store '{}' key '{}' ignored \
(config store deletes are not supported on the Axum dev server)",
store_id.as_ref(),
key
);
Err(Report::new(PlatformError::ConfigStore)
.attach("config store deletes are not supported on the Axum dev server"))
}
}
// ---------------------------------------------------------------------------
// PlatformSecretStore
// ---------------------------------------------------------------------------
/// Environment-variable–backed secret store for the Axum dev server.
///
/// Reads from `TRUSTED_SERVER_SECRET_{STORE}_{KEY}` as raw UTF-8 bytes.
/// Write operations are unsupported in local development.
pub struct AxumPlatformSecretStore;
impl PlatformSecretStore for AxumPlatformSecretStore {
fn get_bytes(
&self,
store_name: &StoreName,
key: &str,
) -> Result<Vec<u8>, Report<PlatformError>> {
let var_name = secret_env_var(store_name.as_ref(), key);
std::env::var(&var_name)
.map(String::into_bytes)
.map_err(|_| {
Report::new(PlatformError::SecretStore).attach(format!(
"env var '{var_name}' not set — export it to supply this secret value"
))
})
}
fn create(
&self,
store_id: &StoreId,
name: &str,
_value: &str,
) -> Result<(), Report<PlatformError>> {
log::warn!(
"AxumPlatformSecretStore: create '{}' in store '{}' ignored \
(secret store writes are not supported on the Axum dev server)",
name,
store_id.as_ref()
);
Err(Report::new(PlatformError::SecretStore)
.attach("secret store writes are not supported on the Axum dev server"))
}
fn delete(&self, store_id: &StoreId, name: &str) -> Result<(), Report<PlatformError>> {
log::warn!(
"AxumPlatformSecretStore: delete '{}' from store '{}' ignored \
(secret store deletes are not supported on the Axum dev server)",
name,
store_id.as_ref()
);
Err(Report::new(PlatformError::SecretStore)
.attach("secret store deletes are not supported on the Axum dev server"))
}
}
// ---------------------------------------------------------------------------
// PlatformBackend
// ---------------------------------------------------------------------------
/// No-op backend for the Axum dev server.
///
/// Returns a deterministic name; `ensure` is a no-op returning the same name.
/// The Axum HTTP client sends directly to URIs and ignores backend names.
pub struct AxumPlatformBackend;
impl PlatformBackend for AxumPlatformBackend {
fn predict_name(&self, spec: &PlatformBackendSpec) -> Result<String, Report<PlatformError>> {
let port = spec
.port
.unwrap_or(if spec.scheme == "https" { 443 } else { 80 });
// Keep two providers that share an origin on distinct names so auction
// response correlation cannot cross providers.
let discriminator = spec
.discriminator
.as_deref()
.map(|d| format!("_p_{}", normalize_env_segment(d)))
.unwrap_or_default();
Ok(format!(
"{}_{}_{}{}",
normalize_env_segment(&spec.scheme),
normalize_env_segment(&spec.host),
port,
discriminator,
))
}
fn ensure(&self, spec: &PlatformBackendSpec) -> Result<String, Report<PlatformError>> {
self.predict_name(spec)
}
}
// ---------------------------------------------------------------------------
// PlatformGeo
// ---------------------------------------------------------------------------
/// No-op geo implementation — geographic lookup is unavailable in local development.
pub struct AxumPlatformGeo;
impl PlatformGeo for AxumPlatformGeo {
fn lookup(&self, _client_ip: Option<IpAddr>) -> Result<Option<GeoInfo>, Report<PlatformError>> {
Ok(None)
}
}
// ---------------------------------------------------------------------------
// PlatformHttpClient
// ---------------------------------------------------------------------------
type SpawnedRequestResult = Result<(u16, Vec<(String, Vec<u8>)>, Vec<u8>), Report<PlatformError>>;
fn sanitized_response_headers(headers: &HeaderMap) -> Vec<(String, Vec<u8>)> {
let connection_tokens = connection_header_tokens(headers);
headers
.iter()
.filter(|(name, _)| !is_hop_by_hop_response_header(name, &connection_tokens))
.map(|(name, value)| (name.to_string(), value.as_bytes().to_vec()))
.collect()
}
fn connection_header_tokens(headers: &HeaderMap) -> Vec<HeaderName> {
headers
.get_all(header::CONNECTION)
.iter()
.filter_map(header_value_to_str)
.flat_map(|value| value.split(','))
.map(str::trim)
.filter(|token| !token.is_empty())
.filter_map(|token| HeaderName::from_bytes(token.as_bytes()).ok())
.collect()
}
fn header_value_to_str(value: &HeaderValue) -> Option<&str> {
value.to_str().ok()
}
fn is_hop_by_hop_response_header(name: &HeaderName, connection_tokens: &[HeaderName]) -> bool {
name == header::CONNECTION
|| name == header::PROXY_AUTHENTICATE
|| name == header::PROXY_AUTHORIZATION
|| name == header::TE
|| name == header::TRAILER
|| name == header::TRANSFER_ENCODING
|| name == header::UPGRADE
|| name.as_str().eq_ignore_ascii_case("keep-alive")
|| connection_tokens.iter().any(|token| token == name)
}
/// Buffered response parts from a spawned outbound request.
///
/// Stored inside [`PlatformPendingRequest`] so that [`AxumPlatformHttpClient::select`]
/// can poll multiple in-flight handles concurrently via
/// [`futures::future::select_all`].
struct AxumPendingHandle {
backend_name: String,
handle: tokio::task::JoinHandle<SpawnedRequestResult>,
}
impl Drop for AxumPendingHandle {
fn drop(&mut self) {
// Abort instead of detaching: when the orchestrator hits the auction
// deadline and drops the remaining pending requests, the abandoned
// bidder tasks would otherwise keep running for up to the 30s
// transport timeout.
self.handle.abort();
}
}
/// Resolves to the backend name together with the task result so that
/// [`futures::future::select_all`] callers never have to reconstruct which
/// backend a completion belongs to by position. `select_all` removes the
/// ready future with `swap_remove` and makes no ordering guarantee for the
/// remaining futures, so positional bookkeeping would mislabel them.
impl Future for AxumPendingHandle {
type Output = (String, Result<SpawnedRequestResult, tokio::task::JoinError>);
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match Pin::new(&mut self.handle).poll(cx) {
Poll::Ready(result) => {
let backend_name = std::mem::take(&mut self.backend_name);
Poll::Ready((backend_name, result))
}
Poll::Pending => Poll::Pending,
}
}
}
/// reqwest-backed HTTP client for the Axum dev server.
///
/// `send_async` buffers any `Body::Stream` in the calling context, then spawns
/// a `tokio` task for each outbound request so that multiple `send_async` calls
/// run concurrently. `select` uses [`futures::future::select_all`] to wait for
/// the first completing handle, preserving fan-out semantics.
pub struct AxumPlatformHttpClient {
client: reqwest::Client,
}
impl AxumPlatformHttpClient {
/// Create a new client with sensible dev-server timeouts.
///
/// # Panics
///
/// Panics if the underlying `reqwest::Client` cannot be built (should not
/// happen with the default TLS configuration on any supported platform).
#[must_use]
pub fn new() -> Self {
Self {
client: reqwest::Client::builder()
.connect_timeout(Duration::from_secs(5))
.timeout(Duration::from_secs(30))
// Disable automatic redirects: core proxy code enforces redirect
// limits and allowed_domains checks itself. Without this, reqwest
// would follow Location headers internally and bypass those checks.
.redirect(reqwest::redirect::Policy::none())
.build()
.expect("should build reqwest client"),
}
}
/// Drain `body` to a `Vec<u8>`.
///
/// For `Body::Stream` this awaits every chunk in the current async context
/// (where `LocalBoxStream` is valid) before the bytes are moved into a
/// `tokio::spawn` task that requires `Send`.
async fn buffer_body(
body: edgezero_core::body::Body,
) -> Result<Vec<u8>, Report<PlatformError>> {
match body {
edgezero_core::body::Body::Once(bytes) => Ok(bytes.to_vec()),
edgezero_core::body::Body::Stream(mut stream) => {
log::debug!("buffering Body::Stream into Vec<u8> for outbound request");
use futures::StreamExt as _;
let mut buf = Vec::new();
while let Some(chunk) = stream.next().await {
let bytes = chunk.map_err(|e| {
Report::new(PlatformError::HttpClient)
.attach(format!("failed to buffer outbound streaming body: {e}"))
})?;
buf.extend_from_slice(&bytes);
}
Ok(buf)
}
}
}
async fn execute(
&self,
request: PlatformHttpRequest,
) -> Result<PlatformResponse, Report<PlatformError>> {
let uri = request.request.uri().to_string();
let method = reqwest::Method::from_bytes(request.request.method().as_str().as_bytes())
.change_context(PlatformError::HttpClient)?;
let mut builder = self.client.request(method, &uri);
for (name, value) in request.request.headers() {
builder = builder.header(name.as_str(), value.as_bytes());
}
let (_, body) = request.request.into_parts();
let body_bytes = Self::buffer_body(body).await?;
if !body_bytes.is_empty() {
builder = builder.body(body_bytes);
}
let resp = builder
.send()
.await
.change_context(PlatformError::HttpClient)
.attach(format!("outbound request to {uri} failed"))?;
let status = resp.status().as_u16();
let mut edge_builder = edgezero_core::http::response_builder().status(status);
for (name, value) in sanitized_response_headers(resp.headers()) {
edge_builder = edge_builder.header(name.as_str(), value.as_slice());
}
let resp_bytes = resp
.bytes()
.await
.change_context(PlatformError::HttpClient)?;
// Upstream responses are buffered whole with no cap — acceptable for
// the dev server, but log the size so a large or hostile upstream is
// visible instead of silently growing the heap.
log::debug!(
"buffered {} upstream response bytes from {uri}",
resp_bytes.len()
);
let edge_resp = edge_builder
.body(edgezero_core::body::Body::from(resp_bytes.to_vec()))
.change_context(PlatformError::HttpClient)?;
Ok(PlatformResponse::new(edge_resp).with_backend_name(request.backend_name))
}
}
impl Default for AxumPlatformHttpClient {
fn default() -> Self {
Self::new()
}
}
#[async_trait(?Send)]
impl PlatformHttpClient for AxumPlatformHttpClient {
async fn send(
&self,
request: PlatformHttpRequest,
) -> Result<PlatformResponse, Report<PlatformError>> {
self.execute(request).await
}
async fn send_async(
&self,
request: PlatformHttpRequest,
) -> Result<PlatformPendingRequest, Report<PlatformError>> {
let backend_name = request.backend_name.clone();
// Extract all Send-compatible parts before spawning.
let uri = request.request.uri().to_string();
let method_bytes = request.request.method().as_str().as_bytes().to_vec();
let headers: Vec<(String, Vec<u8>)> = request
.request
.headers()
.iter()
.map(|(n, v)| (n.to_string(), v.as_bytes().to_vec()))
.collect();
// Buffer any LocalBoxStream body here in the ?Send context before spawn.
let (_, body) = request.request.into_parts();
let body_bytes = Self::buffer_body(body).await?;
let client = self.client.clone();
let handle = tokio::spawn(async move {
let method = reqwest::Method::from_bytes(&method_bytes)
.map_err(|e| Report::new(PlatformError::HttpClient).attach(e.to_string()))?;
let mut builder = client.request(method, &uri);
for (name, value) in &headers {
builder = builder.header(name.as_str(), value.as_slice());
}
if !body_bytes.is_empty() {
builder = builder.body(body_bytes);
}
let resp = builder.send().await.map_err(|e| {
Report::new(PlatformError::HttpClient)
.attach(format!("outbound request to {uri} failed: {e}"))
})?;
let status = resp.status().as_u16();
let resp_headers = sanitized_response_headers(resp.headers());
let body = resp
.bytes()
.await
.map_err(|e| Report::new(PlatformError::HttpClient).attach(e.to_string()))?
.to_vec();
// Same unbounded-buffering note as the synchronous path: log the
// size so large upstream responses are visible in dev.
log::debug!("buffered {} upstream response bytes from {uri}", body.len());
Ok::<_, Report<PlatformError>>((status, resp_headers, body))
});
let pending = AxumPendingHandle {
backend_name: backend_name.clone(),
handle,
};
Ok(PlatformPendingRequest::new(pending).with_backend_name(backend_name))
}
async fn select(
&self,
pending_requests: Vec<PlatformPendingRequest>,
) -> Result<PlatformSelectResult, Report<PlatformError>> {
if pending_requests.is_empty() {
return Err(Report::new(PlatformError::HttpClient)
.attach("select called with an empty pending_requests list"));
}
let handles: Vec<AxumPendingHandle> = pending_requests
.into_iter()
.map(|pr| {
pr.downcast::<AxumPendingHandle>().map_err(|_| {
Report::new(PlatformError::HttpClient)
.attach("unexpected inner type in AxumPlatformHttpClient::select")
})
})
.collect::<Result<Vec<_>, _>>()?;
// Each AxumPendingHandle resolves to (backend_name, result), so the
// remaining handles keep their own backend names — no positional
// reconstruction (select_all does not preserve the order of the
// remaining futures).
let ((backend_name, result), _ready_idx, remaining_handles) =
futures::future::select_all(handles).await;
let remaining: Vec<PlatformPendingRequest> = remaining_handles
.into_iter()
.map(|handle| {
let backend_name = handle.backend_name.clone();
PlatformPendingRequest::new(handle).with_backend_name(backend_name)
})
.collect();
// Map join panics and per-request errors into ready: Err(...) so that the
// auction orchestrator can log the failure and continue with remaining providers
// rather than treating one bad provider as a fatal select() failure.
let ready = result
.map_err(|e| {
Report::new(PlatformError::HttpClient)
.attach(format!("auction request task panicked: {e}"))
})
.and_then(|inner| inner)
.and_then(|(status, headers, body)| {
let mut builder = edgezero_core::http::response_builder().status(status);
for (name, value) in &headers {
builder = builder.header(name.as_str(), value.as_slice());
}
builder
.body(edgezero_core::body::Body::from(body))
.change_context(PlatformError::HttpClient)
})
.map(|edge_resp| {
PlatformResponse::new(edge_resp).with_backend_name(backend_name.clone())
});
// Attribute the failure to its backend so the orchestrator can remove
// the provider and record a BidStatus::Error, matching the Fastly
// adapter. Without this, a failed provider silently vanishes through
// the orchestrator's "backend not identified" branch.
let failed_backend_name = ready.as_ref().err().map(|_| backend_name);
Ok(PlatformSelectResult {
ready,
remaining,
failed_backend_name,
})
}
}
// ---------------------------------------------------------------------------
// build_runtime_services
// ---------------------------------------------------------------------------
/// Construct [`RuntimeServices`] for an incoming Axum request.
///
/// # Degraded features in dev
///
/// KV store is [`trusted_server_core::platform::UnavailableKvStore`] — any route
/// touching synthetic-ID or consent KV will degrade gracefully. A `warn` log is
/// emitted once per process.
pub fn build_runtime_services(ctx: &edgezero_core::context::RequestContext) -> RuntimeServices {
static KV_WARNED: std::sync::OnceLock<()> = std::sync::OnceLock::new();
KV_WARNED.get_or_init(|| {
log::warn!(
"Axum dev server: KV store is unavailable (UnavailableKvStore). \
Routes that depend on synthetic-ID or consent KV will degrade gracefully."
);
});
let client_ip = edgezero_adapter_axum::context::AxumRequestContext::get(ctx.request())
.and_then(|c| c.remote_addr)
.map(|addr| addr.ip());
use trusted_server_core::platform::{
PlatformBackend, PlatformConfigStore, PlatformGeo, PlatformKvStore, PlatformSecretStore,
};
// Stateless shims are promoted to process-wide statics so callers clone
// an existing Arc instead of allocating a new one per request.
static CONFIG_STORE: std::sync::OnceLock<Arc<dyn PlatformConfigStore>> =
std::sync::OnceLock::new();
static SECRET_STORE: std::sync::OnceLock<Arc<dyn PlatformSecretStore>> =
std::sync::OnceLock::new();
static KV_STORE: std::sync::OnceLock<Arc<dyn PlatformKvStore>> = std::sync::OnceLock::new();
static BACKEND: std::sync::OnceLock<Arc<dyn PlatformBackend>> = std::sync::OnceLock::new();
static GEO: std::sync::OnceLock<Arc<dyn PlatformGeo>> = std::sync::OnceLock::new();
RuntimeServices::builder()
.config_store(Arc::clone(CONFIG_STORE.get_or_init(|| {
Arc::new(AxumPlatformConfigStore) as Arc<dyn PlatformConfigStore>
})))
.secret_store(Arc::clone(SECRET_STORE.get_or_init(|| {
Arc::new(AxumPlatformSecretStore) as Arc<dyn PlatformSecretStore>
})))
.kv_store(Arc::clone(KV_STORE.get_or_init(|| {
Arc::new(trusted_server_core::platform::UnavailableKvStore) as Arc<dyn PlatformKvStore>
})))
.backend(Arc::clone(BACKEND.get_or_init(|| {
Arc::new(AxumPlatformBackend) as Arc<dyn PlatformBackend>
})))
// Keep the HTTP client request-scoped in the dev adapter. Sharing a pooled
// client across requests previously regressed the Next.js server-action →
// API-route integration flow by reusing a poisoned connection after a
// truncated POST. Revisit pooling if profiling shows allocation cost.
.http_client(Arc::new(AxumPlatformHttpClient::new()))
.geo(Arc::clone(GEO.get_or_init(|| {
Arc::new(AxumPlatformGeo) as Arc<dyn PlatformGeo>
})))
.client_info(ClientInfo {
client_ip,
tls_protocol: None,
tls_cipher: None,
..ClientInfo::default()
})
.build()
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
use edgezero_core::body::Body as EdgeBody;
use std::time::Duration;
use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
#[test]
fn config_store_reads_from_env_var() {
temp_env::with_var(
"TRUSTED_SERVER_CONFIG_MY_STORE_MY_KEY",
Some("test-value"),
|| {
let store = AxumPlatformConfigStore;
let result = store
.get(&StoreName::from("my-store"), "my-key")
.expect("should read env var");
assert_eq!(result, "test-value", "should return env var value");
},
);
}
#[test]
fn config_store_returns_error_for_missing_env_var() {
let store = AxumPlatformConfigStore;
let result = store.get(
&StoreName::from("nonexistent-store-zzz"),
"nonexistent-key-zzz",
);
assert!(result.is_err(), "should error for missing env var");
}
#[test]
fn secret_store_reads_bytes_from_env_var() {
temp_env::with_var(
"TRUSTED_SERVER_SECRET_MY_SECRETS_MY_SECRET",
Some("hello"),
|| {
let store = AxumPlatformSecretStore;
let result = store
.get_bytes(&StoreName::from("my-secrets"), "my-secret")
.expect("should read env var as bytes");
assert_eq!(result, b"hello", "should return raw bytes");
},
);
}
#[test]
fn backend_predict_name_returns_deterministic_string() {
let backend = AxumPlatformBackend;
let spec = PlatformBackendSpec {
scheme: "https".to_string(),
host: "example.com".to_string(),
port: None,
certificate_check: true,
first_byte_timeout: Duration::from_secs(15),
between_bytes_timeout: Duration::from_secs(15),
host_header_override: None,
discriminator: None,
};
let name1 = backend.predict_name(&spec).expect("should return a name");
let name2 = backend
.predict_name(&spec)
.expect("should return same name");
assert!(!name1.is_empty(), "should return a non-empty name");
assert_eq!(name1, name2, "should be deterministic");
}
#[test]
fn backend_ensure_returns_same_name_as_predict() {
let backend = AxumPlatformBackend;
let spec = PlatformBackendSpec {
scheme: "https".to_string(),
host: "example.com".to_string(),
port: None,
certificate_check: true,
first_byte_timeout: Duration::from_secs(15),
between_bytes_timeout: Duration::from_secs(15),
host_header_override: None,
discriminator: None,
};
assert_eq!(
backend.predict_name(&spec).expect("should return name"),
backend.ensure(&spec).expect("should return name"),
"ensure should equal predict_name"
);
}
#[test]
fn geo_always_returns_none() {
let geo = AxumPlatformGeo;
let no_ip = geo.lookup(None).expect("should not error");
assert!(no_ip.is_none(), "should return None for no IP");
let with_ip = geo
.lookup(Some("127.0.0.1".parse().expect("should parse IP")))
.expect("should not error");
assert!(with_ip.is_none(), "should return None for any IP");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn http_client_strips_hop_by_hop_response_headers() {
let url = serve_raw_response(
b"HTTP/1.1 200 OK\r\n\
Transfer-Encoding: chunked\r\n\
Connection: keep-alive, x-remove-me\r\n\
Keep-Alive: timeout=5\r\n\
X-Remove-Me: listed-by-connection\r\n\
X-Preserve-Me: application-header\r\n\
\r\n\
2\r\n\
ok\r\n\
0\r\n\
\r\n",
)
.await;
let request = edgezero_core::http::request_builder()
.uri(url)
.body(EdgeBody::empty())
.expect("should build outbound request");
let response = AxumPlatformHttpClient::new()
.send(PlatformHttpRequest::new(request, "test_backend"))
.await
.expect("should proxy raw response")
.response;
assert!(
response.headers().get(header::TRANSFER_ENCODING).is_none(),
"should strip transfer-encoding"
);
assert!(
response.headers().get(header::CONNECTION).is_none(),
"should strip connection"
);
assert!(
response.headers().get("keep-alive").is_none(),
"should strip keep-alive"
);
assert!(
response.headers().get("x-remove-me").is_none(),
"should strip headers named by connection"
);
assert_eq!(
response
.headers()
.get("x-preserve-me")
.and_then(|value| value.to_str().ok()),
Some("application-header"),
"should preserve end-to-end headers"
);
assert_eq!(
response
.into_body()
.into_bytes()
.unwrap_or_default()
.as_ref(),
b"ok",
"should preserve decoded response body"
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn select_attributes_failed_backend_name() {
// Bind and immediately drop a listener so the port is closed — the
// request fails with connection refused.
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
.await
.expect("should bind probe listener");
let addr = listener.local_addr().expect("should read local address");
drop(listener);
let request = edgezero_core::http::request_builder()
.uri(format!("http://{addr}/"))
.body(EdgeBody::empty())
.expect("should build outbound request");
let client = AxumPlatformHttpClient::new();
let pending = client
.send_async(PlatformHttpRequest::new(request, "failing_backend"))
.await
.expect("should spawn async request");
let result = client
.select(vec![pending])
.await
.expect("select should surface the failure via ready, not a fatal error");
assert!(
result.ready.is_err(),
"request to a closed port should fail"
);
assert_eq!(
result.failed_backend_name.as_deref(),
Some("failing_backend"),
"failed provider must be attributed to its backend so the orchestrator can record BidStatus::Error"
);
assert!(
result.remaining.is_empty(),
"no remaining requests expected"
);
}
async fn serve_raw_response(response: &'static [u8]) -> String {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
.await
.expect("should bind raw HTTP test server");
let addr = listener.local_addr().expect("should read local address");
tokio::spawn(async move {
let (mut stream, _) = listener.accept().await.expect("should accept request");
let mut request = [0; 1024];
let _ = stream
.read(&mut request)
.await
.expect("should read request");
stream
.write_all(response)
.await
.expect("should write response");
});
format!("http://{addr}/")
}
}