Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_steel/tests/api_upstream_down.rs

3.3 KiB, 1 run

created by r1870400018:65939, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! An `api_routes` proxy whose upstream cannot answer is a 502, not a connection closed on nothing.
2//!
3//! `forward_api_proxy` returned the outbound client's error, `handle_get` and `handle_post`
4//! passed it up with `res!`, and the connection was dropped with no response: the visitor of an
5//! `api_routes` entry whose upstream was down, or spoke something that is not HTTP, was told
6//! nothing at all. A `proxy_routes` entry answers 502 in the same case; this holds `api_routes`
7//! to it. Found while rehearsing ontheism.org's move to Steel, where the same empty reply first
8//! showed as the refusal of an HTTP/1.0 upstream (see `api_http10.rs`).
9//!
10//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
11//! Anthropic Claude
12
13mod vhost_rig;
14
15use vhost_rig::{
16 Rig,
17 Scratch,
18 Site,
19};
20
21use oxedyne_fe2o3_core::prelude::*;
22use oxedyne_fe2o3_steel::srv::cfg::ApiRoute;
23
24use tokio::{
25 io::{
26 AsyncReadExt,
27 AsyncWriteExt,
28 },
29 net::TcpListener,
30};
31
32const HOST: &str = "api.test";
33
34fn route(path: &str, upstream_path: &str, port: u16) -> ApiRoute {
35 ApiRoute {
36 path: path.to_string(),
37 upstream_host: Some("127.0.0.1".to_string()),
38 upstream_port: Some(port),
39 upstream_path: Some(upstream_path.to_string()),
40 upstream_tls: false,
41 headers: Vec::new(),
42 handler: None,
43 config: Vec::new(),
44 }
45}
46
47/// A port nothing listens on: bound, noted and released.
48async fn closed_port() -> Outcome<u16> {
49 let listener = res!(TcpListener::bind("127.0.0.1:0").await, Network, Init);
50 Ok(res!(listener.local_addr(), Network, Init).port())
51}
52
53/// An upstream that is down, or that answers with something that is not HTTP at all, is a 502.
54#[test]
55fn an_api_route_whose_upstream_cannot_answer_is_a_502() -> Outcome<()> {
56 let web = res!(Scratch::new("api_502"));
57 res!(web.put("index.html", "<p>home</p>"));
58 let runtime = res!(vhost_rig::runtime());
59 runtime.block_on(async {
60 let dead = res!(closed_port().await);
61 // Speaks, but not HTTP.
62 let babble = res!(TcpListener::bind("127.0.0.1:0").await, Network, Init);
63 let babble_port = res!(babble.local_addr(), Network, Init).port();
64 tokio::spawn(async move {
65 loop {
66 let (mut tcp, _) = match babble.accept().await {
67 Ok(c) => c,
68 Err(_) => return,
69 };
70 let mut buf = [0u8; 1024];
71 let _ = tcp.read(&mut buf).await;
72 let _ = tcp.write_all(b"this is not a status line\r\n\r\n").await;
73 let _ = tcp.shutdown().await;
74 }
75 });
76 let mut site = Site::new(HOST, &web.0);
77 site.api_routes = vec![
78 route("/down", "/x", dead),
79 route("/babble", "/x", babble_port),
80 ];
81 let rig = res!(Rig::new(vec![site]));
82 for path in ["/down", "/babble"] {
83 for method in ["GET", "POST"] {
84 let r = res!(rig.fetch(HOST, method, path, &[], "{}").await);
85 assert_eq!(r.status(), 502, "{} {} must be a 502: {:?}", method, path, r);
86 assert!(r.body.contains("Bad Gateway"), "{} {}: {:?}", method, path, r);
87 }
88 }
89 Ok(())
90 })
91}