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.
Sends over the socket, sleeps and reads the time on the world's clock.
The world, served; the runtime that serves it lives as long as the rig.
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}