From 05a47bd9dcd2023eed340fc7419aa14cabb44208 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Mon, 28 Apr 2025 21:29:39 +0200 Subject: [PATCH] Fix flaky tests and make sure empty bodies are always None --- .../src/shared/{incoming.rs => body.rs} | 10 +++- crates/lune-std-net/src/shared/mod.rs | 2 +- crates/lune-std-net/src/shared/request.rs | 6 +- crates/lune-std-net/src/shared/response.rs | 6 +- tests/net/serve/handles.luau | 2 +- tests/net/serve/non_blocking.luau | 2 +- tests/net/serve/requests.luau | 2 +- tests/net/serve/websockets.luau | 2 +- tests/roblox/instance/custom/async.luau | 7 +-- tests/serde/json/decode.luau | 60 ++++--------------- tests/serde/json/encode.luau | 10 ---- 11 files changed, 32 insertions(+), 77 deletions(-) rename crates/lune-std-net/src/shared/{incoming.rs => body.rs} (82%) diff --git a/crates/lune-std-net/src/shared/incoming.rs b/crates/lune-std-net/src/shared/body.rs similarity index 82% rename from crates/lune-std-net/src/shared/incoming.rs rename to crates/lune-std-net/src/shared/body.rs index 90120fe..3ab6541 100644 --- a/crates/lune-std-net/src/shared/incoming.rs +++ b/crates/lune-std-net/src/shared/body.rs @@ -1,4 +1,4 @@ -use http_body_util::BodyExt; +use http_body_util::{BodyExt, Full}; use hyper::{ body::{Bytes, Incoming}, header::CONTENT_ENCODING, @@ -33,3 +33,11 @@ pub async fn handle_incoming_body( Ok((body, was_decompressed)) } + +pub fn bytes_to_full(bytes: Bytes) -> Full { + if bytes.is_empty() { + Full::default() + } else { + Full::new(bytes) + } +} diff --git a/crates/lune-std-net/src/shared/mod.rs b/crates/lune-std-net/src/shared/mod.rs index 733fa69..ce262d6 100644 --- a/crates/lune-std-net/src/shared/mod.rs +++ b/crates/lune-std-net/src/shared/mod.rs @@ -1,7 +1,7 @@ +pub mod body; pub mod futures; pub mod headers; pub mod hyper; -pub mod incoming; pub mod lua; pub mod request; pub mod response; diff --git a/crates/lune-std-net/src/shared/request.rs b/crates/lune-std-net/src/shared/request.rs index 8f3859e..ea9cc46 100644 --- a/crates/lune-std-net/src/shared/request.rs +++ b/crates/lune-std-net/src/shared/request.rs @@ -11,8 +11,8 @@ use hyper::{ use mlua::prelude::*; use crate::shared::{ + body::{bytes_to_full, handle_incoming_body}, headers::{hash_map_to_table, header_map_to_table}, - incoming::handle_incoming_body, lua::{lua_table_to_header_map, lua_value_to_bytes, lua_value_to_method}, }; @@ -156,7 +156,7 @@ impl Request { .expect("request was valid") .extend(self.inner.headers().clone()); - let body = Full::new(self.inner.body().clone()); + let body = bytes_to_full(self.inner.body().clone()); builder.body(body).expect("request was valid") } @@ -167,7 +167,7 @@ impl Request { #[allow(dead_code)] pub fn into_full(self) -> HyperRequest> { let (parts, body) = self.inner.into_parts(); - HyperRequest::from_parts(parts, Full::new(body)) + HyperRequest::from_parts(parts, bytes_to_full(body)) } } diff --git a/crates/lune-std-net/src/shared/response.rs b/crates/lune-std-net/src/shared/response.rs index 6b40339..75a5687 100644 --- a/crates/lune-std-net/src/shared/response.rs +++ b/crates/lune-std-net/src/shared/response.rs @@ -9,8 +9,8 @@ use hyper::{ use mlua::prelude::*; use crate::shared::{ + body::{bytes_to_full, handle_incoming_body}, headers::header_map_to_table, - incoming::handle_incoming_body, lua::{lua_table_to_header_map, lua_value_to_bytes}, }; @@ -90,7 +90,7 @@ impl Response { .expect("request was valid") .extend(self.inner.headers().clone()); - let body = Full::new(self.inner.body().clone()); + let body = bytes_to_full(self.inner.body().clone()); builder.body(body).expect("request was valid") } @@ -101,7 +101,7 @@ impl Response { #[allow(dead_code)] pub fn into_full(self) -> HyperResponse> { let (parts, body) = self.inner.into_parts(); - HyperResponse::from_parts(parts, Full::new(body)) + HyperResponse::from_parts(parts, bytes_to_full(body)) } } diff --git a/tests/net/serve/handles.luau b/tests/net/serve/handles.luau index 101dc25..4f823c7 100644 --- a/tests/net/serve/handles.luau +++ b/tests/net/serve/handles.luau @@ -1,7 +1,7 @@ local net = require("@lune/net") local task = require("@lune/task") -local PORT = 8083 +local PORT = 8082 local URL = `http://127.0.0.1:{PORT}` local RESPONSE = "Hello, lune!" diff --git a/tests/net/serve/non_blocking.luau b/tests/net/serve/non_blocking.luau index b05b14e..f855e75 100644 --- a/tests/net/serve/non_blocking.luau +++ b/tests/net/serve/non_blocking.luau @@ -3,7 +3,7 @@ local process = require("@lune/process") local stdio = require("@lune/stdio") local task = require("@lune/task") -local PORT = 8082 +local PORT = 8083 local RESPONSE = "Hello, lune!" -- Serve should not yield the entire main thread forever, only diff --git a/tests/net/serve/requests.luau b/tests/net/serve/requests.luau index 2d9d7e0..a17b0f8 100644 --- a/tests/net/serve/requests.luau +++ b/tests/net/serve/requests.luau @@ -3,7 +3,7 @@ local process = require("@lune/process") local stdio = require("@lune/stdio") local task = require("@lune/task") -local PORT = 8083 +local PORT = 8084 local URL = `http://127.0.0.1:{PORT}` local RESPONSE = "Hello, lune!" diff --git a/tests/net/serve/websockets.luau b/tests/net/serve/websockets.luau index 1b67b33..bf8bf56 100644 --- a/tests/net/serve/websockets.luau +++ b/tests/net/serve/websockets.luau @@ -3,7 +3,7 @@ local process = require("@lune/process") local stdio = require("@lune/stdio") local task = require("@lune/task") -local PORT = 8081 +local PORT = 8085 local WS_URL = `ws://127.0.0.1:{PORT}` local REQUEST = "Hello from client!" local RESPONSE = "Hello, lune!" diff --git a/tests/roblox/instance/custom/async.luau b/tests/roblox/instance/custom/async.luau index 471f143..09a6c57 100644 --- a/tests/roblox/instance/custom/async.luau +++ b/tests/roblox/instance/custom/async.luau @@ -16,14 +16,11 @@ end) -- Reference: https://create.roblox.com/docs/reference/engine/classes/HttpService#GetAsync -local URL_ASTROS = "http://api.open-notify.org/astros.json" - local game = roblox.Instance.new("DataModel") local HttpService = game:GetService("HttpService") :: any -local response = HttpService:GetAsync(URL_ASTROS) +local response = HttpService:GetAsync("https://httpbingo.org/json") local data = HttpService:JSONDecode(response) assert(type(data) == "table", "Returned JSON data should decode to a table") -assert(data.message == "success", "Returned JSON data should have a 'message' with value 'success'") -assert(type(data.people) == "table", "Returned JSON data should have a 'people' table") +assert(type(data.slideshow) == "table", "Returned JSON data should contain 'slideshow'") diff --git a/tests/serde/json/decode.luau b/tests/serde/json/decode.luau index d5ec908..d3f9af6 100644 --- a/tests/serde/json/decode.luau +++ b/tests/serde/json/decode.luau @@ -1,53 +1,13 @@ -local net = require("@lune/net") local serde = require("@lune/serde") +local source = require("./source") -type Response = { - products: { - { - id: number, - title: string, - description: string, - price: number, - discountPercentage: number, - rating: number, - stock: number, - brand: string, - category: string, - thumbnail: string, - images: { string }, - } - }, - total: number, - skip: number, - limit: number, -} +local decoded = serde.decode("json", source.pretty) -local response = net.request("https://dummyjson.com/products") - -assert(response.ok, "Dummy JSON api returned an error") -assert(#response.body > 0, "Dummy JSON api returned empty body") - -local data: Response = serde.decode("json", response.body) - -assert(type(data.limit) == "number", "Products limit was not a number") -assert(type(data.products) == "table", "Products was not a table") -assert(#data.products > 0, "Products table was empty") - -local productCount = 0 -for _, product in data.products do - productCount += 1 - assert(type(product.id) == "number", "Product id was not a number") - assert(type(product.title) == "string", "Product title was not a number") - assert(type(product.description) == "string", "Product description was not a number") - assert(type(product.images) == "table", "Product images was not a table") - assert(#product.images > 0, "Product images table was empty") -end - -assert( - data.limit == productCount, - string.format( - "Products limit and number of products in array mismatch (expected %d, got %d)", - data.limit, - productCount - ) -) +assert(type(decoded) == "table", "Decoded payload was not a table") +assert(decoded.Hello == "World", "Decoded payload Hello was not World") +assert(type(decoded.Inner) == "table", "Decoded payload Inner was not a table") +assert(type(decoded.Inner.Array) == "table", "Decoded payload Inner.Array was not a table") +assert(type(decoded.Inner.Array[1]) == "number", "Decoded payload Inner.Array[1] was not a number") +assert(type(decoded.Inner.Array[2]) == "number", "Decoded payload Inner.Array[2] was not a number") +assert(type(decoded.Inner.Array[3]) == "number", "Decoded payload Inner.Array[3] was not a number") +assert(decoded.Foo == "Bar", "Decoded payload Foo was not Bar") diff --git a/tests/serde/json/encode.luau b/tests/serde/json/encode.luau index 6be3b5a..3615a14 100644 --- a/tests/serde/json/encode.luau +++ b/tests/serde/json/encode.luau @@ -2,16 +2,6 @@ local serde = require("@lune/serde") local source = require("./source") local decoded = serde.decode("json", source.pretty) - -assert(type(decoded) == "table", "Decoded payload was not a table") -assert(decoded.Hello == "World", "Decoded payload Hello was not World") -assert(type(decoded.Inner) == "table", "Decoded payload Inner was not a table") -assert(type(decoded.Inner.Array) == "table", "Decoded payload Inner.Array was not a table") -assert(type(decoded.Inner.Array[1]) == "number", "Decoded payload Inner.Array[1] was not a number") -assert(type(decoded.Inner.Array[2]) == "number", "Decoded payload Inner.Array[2] was not a number") -assert(type(decoded.Inner.Array[3]) == "number", "Decoded payload Inner.Array[3] was not a number") -assert(decoded.Foo == "Bar", "Decoded payload Foo was not Bar") - local encoded = serde.encode("json", decoded, false) assert(encoded == source.encoded, "JSON round-trip did not produce the same result")