The same scenarios as adapter.rs, over a real socket: the Hetzner world served on 127.0.0.1, and the adapter's requests sent by sandbox_native::UreqIo as a program of ours would send them. The adapter's sleeps still move the world's virtual clock (a socket does not change what time the world is on), so a wait of minutes takes no time. Every request and response is still held against the pinned OpenAPI document.

6#[macro_use]
7mod lifecycle;
9use cloud_mock::server::{Running, serve};
10use cloud_mock_hetzner::{BASE_PATH, DEFAULT_TOKEN, mock};
11use lifecycle::{Rig, World};
12use sandbox_api::{Io, IoError, Request, Response};
13use sandbox_native::UreqIo;
14use std::sync::Arc;
15use std::time::Duration;

Sends over the socket, sleeps and reads the time on the world's clock.

18#[derive(Clone)]
19struct SocketIo {
20    world: World,
21    http: UreqIo,
22}
24impl Io for SocketIo {
25    async fn send(&self, request: Request) -> Result<Response, IoError> {
26        self.http.send(request).await
27    }
28
29    async fn sleep(&self, duration: Duration) {
30        self.world.advance(duration);
31    }
32
33    fn now_ms(&self) -> u64 {
34        self.world.now_ms()
35    }
36}

The world, served; the runtime that serves it lives as long as the rig.

39struct Socket {
40    world: World,
41    io: SocketIo,
42    url: String,
43    _running: Running,
44    _runtime: Arc<tokio::runtime::Runtime>,
45}
47impl Socket {
48    fn new() -> Socket {
49        let world = mock(DEFAULT_TOKEN);
50        let runtime = tokio::runtime::Builder::new_multi_thread().worker_threads(1).enable_all().build().expect("a runtime to serve on");
51        let running = runtime.block_on(serve(world.clone(), None)).expect("the world served on a socket");
52        let url = format!("{}{BASE_PATH}", running.url());
53        let io = SocketIo { world: world.clone(), http: UreqIo::new(Duration::from_secs(10)) };
54        Socket { world, io, url, _running: running, _runtime: Arc::new(runtime) }
55    }
56}
57
58impl Rig for Socket {
59    type I = SocketIo;
60
61    fn mock(&self) -> &World {
62        &self.world
63    }
64
65    fn io(&self) -> SocketIo {
66        self.io.clone()
67    }
68
69    fn base_url(&self) -> Option<String> {
70        Some(self.url.clone())
71    }
72
73    fn disconnect_text(&self) -> Option<&'static str> {
74        None
75    }
76}
77
78scenarios!(Socket::new);
79
80#[test]
81fn a_plain_request_over_the_socket_gets_the_apis_headers_and_envelope() {
82    let rig = Socket::new();
83    let url = format!("{}/servers", rig.base_url().unwrap());
84    let ask = |token: Option<&str>| {
85        let mut b = http::Request::builder().method("GET").uri(&url);
86        if let Some(t) = token {
87            b = b.header("authorization", format!("Bearer {t}"));
88        }
89        sandbox_api::fake::run(rig.io().send(b.body(Vec::new()).unwrap())).unwrap()
90    };
91    let refused = ask(None);
92    assert_eq!(refused.status().as_u16(), 401);
93    let body: serde_json::Value = serde_json::from_slice(refused.body()).unwrap();
94    assert_eq!(body["error"]["code"], "unauthorized");
95    let answered = ask(Some(DEFAULT_TOKEN));
96    assert_eq!(answered.status().as_u16(), 200);
97    assert!(answered.headers().contains_key("ratelimit-remaining"), "{:?}", answered.headers());
98    rig.mock().assert_conforms();
99}