diff --git a/crates-cli/yaak-cli/tests/common/http_server.rs b/crates-cli/yaak-cli/tests/common/http_server.rs index 5a004d32..97dcd67f 100644 --- a/crates-cli/yaak-cli/tests/common/http_server.rs +++ b/crates-cli/yaak-cli/tests/common/http_server.rs @@ -1,9 +1,14 @@ use std::io::{Read, Write}; -use std::net::TcpListener; +use std::net::{SocketAddr, TcpListener, TcpStream}; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; use std::thread; +use std::time::Duration; pub struct TestHttpServer { pub url: String, + addr: SocketAddr, + shutdown: Arc, handle: Option>, } @@ -12,29 +17,46 @@ impl TestHttpServer { let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind test HTTP server"); let addr = listener.local_addr().expect("Failed to get local addr"); let url = format!("http://{addr}/test"); + listener.set_nonblocking(true).expect("Failed to set test server listener nonblocking"); + + let shutdown = Arc::new(AtomicBool::new(false)); + let shutdown_signal = Arc::clone(&shutdown); let body_bytes = body.as_bytes().to_vec(); let handle = thread::spawn(move || { - if let Ok((mut stream, _)) = listener.accept() { - let mut request_buf = [0u8; 4096]; - let _ = stream.read(&mut request_buf); + while !shutdown_signal.load(Ordering::Relaxed) { + match listener.accept() { + Ok((mut stream, _)) => { + let _ = stream.set_read_timeout(Some(Duration::from_secs(1))); + let mut request_buf = [0u8; 4096]; + let _ = stream.read(&mut request_buf); - let response = format!( - "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", - body_bytes.len() - ); - let _ = stream.write_all(response.as_bytes()); - let _ = stream.write_all(&body_bytes); - let _ = stream.flush(); + let response = format!( + "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + body_bytes.len() + ); + let _ = stream.write_all(response.as_bytes()); + let _ = stream.write_all(&body_bytes); + let _ = stream.flush(); + break; + } + Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => { + thread::sleep(Duration::from_millis(10)); + } + Err(_) => break, + } } }); - Self { url, handle: Some(handle) } + Self { url, addr, shutdown, handle: Some(handle) } } } impl Drop for TestHttpServer { fn drop(&mut self) { + self.shutdown.store(true, Ordering::Relaxed); + let _ = TcpStream::connect(self.addr); + if let Some(handle) = self.handle.take() { let _ = handle.join(); } diff --git a/crates/yaak-templates/pkg/yaak_templates.d.ts b/crates/yaak-templates/pkg/yaak_templates.d.ts index aed6c395..df962dbc 100644 --- a/crates/yaak-templates/pkg/yaak_templates.d.ts +++ b/crates/yaak-templates/pkg/yaak_templates.d.ts @@ -1,5 +1,5 @@ /* tslint:disable */ /* eslint-disable */ -export function unescape_template(template: string): any; -export function parse_template(template: string): any; export function escape_template(template: string): any; +export function parse_template(template: string): any; +export function unescape_template(template: string): any; diff --git a/crates/yaak-templates/pkg/yaak_templates_bg.js b/crates/yaak-templates/pkg/yaak_templates_bg.js index e7e87861..900d4685 100644 --- a/crates/yaak-templates/pkg/yaak_templates_bg.js +++ b/crates/yaak-templates/pkg/yaak_templates_bg.js @@ -165,10 +165,10 @@ function takeFromExternrefTable0(idx) { * @param {string} template * @returns {any} */ -export function unescape_template(template) { +export function escape_template(template) { const ptr0 = passStringToWasm0(template, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.unescape_template(ptr0, len0); + const ret = wasm.escape_template(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } @@ -193,10 +193,10 @@ export function parse_template(template) { * @param {string} template * @returns {any} */ -export function escape_template(template) { +export function unescape_template(template) { const ptr0 = passStringToWasm0(template, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.escape_template(ptr0, len0); + const ret = wasm.unescape_template(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } diff --git a/crates/yaak-templates/pkg/yaak_templates_bg.wasm b/crates/yaak-templates/pkg/yaak_templates_bg.wasm index 4d8b65b8..716adf95 100644 Binary files a/crates/yaak-templates/pkg/yaak_templates_bg.wasm and b/crates/yaak-templates/pkg/yaak_templates_bg.wasm differ