diff --git a/casper-server/src/lua/http/body.rs b/casper-server/src/lua/http/body.rs index 1a7e6fb..f10a0f8 100644 --- a/casper-server/src/lua/http/body.rs +++ b/casper-server/src/lua/http/body.rs @@ -7,7 +7,7 @@ use std::time::{Duration, Instant}; use futures::{Stream, TryStreamExt}; use mlua::{ - AnyUserData, Error as LuaError, ErrorContext as _, ExternalError, FromLua, IntoLua, Lua, + AnyUserData, Error as LuaError, ErrorContext as _, ExternalError, FromLua, Lua, OwnedAnyUserData, Result as LuaResult, String as LuaString, UserData, Value, }; use ntex::http::body::{self, BodySize, BoxedBodyStream, MessageBody, ResponseBody, SizedStream}; @@ -453,7 +453,7 @@ mod tests { use std::io::{Error as IoError, ErrorKind}; use std::time::Duration; - use mlua::{chunk, Lua, Result as LuaResult}; + use mlua::{chunk, Lua, Result as LuaResult, Value}; use ntex::http::body::BoxedBodyStream; use tokio_stream::{self as stream, StreamExt}; @@ -700,12 +700,18 @@ mod tests { let lua = Lua::new(); super::super::super::bytes::register_types(&lua)?; + lua.globals().set( + "json_encode", + lua.create_function(|_, value: Value| Ok(serde_json::to_string(&value).unwrap()))?, + )?; + let body = LuaBody::from(r#"{"hello": "world"}"#); lua.load(chunk! { local json = $body:json() assert(typeof(json) == "JsonObject", "variable is not JsonObject") assert(json.hello == "world", "`json.hello` is not 'world'") assert($body:json() ~= nil, "`json()` method must not consume body") + assert(json_encode(json) == "{\"hello\":\"world\"}", "`body_json` must be encoded correctly") }) .exec_async() .await diff --git a/casper-server/src/lua/http/response.rs b/casper-server/src/lua/http/response.rs index b8c5dff..7e3b973 100644 --- a/casper-server/src/lua/http/response.rs +++ b/casper-server/src/lua/http/response.rs @@ -518,6 +518,10 @@ mod tests { lua.globals() .set("Response", lua.create_proxy::()?)?; + lua.globals().set( + "json_encode", + lua.create_function(|_, value: Value| Ok(serde_json::to_string(&value).unwrap()))?, + )?; // Check JSON parsing lua.load(chunk! { @@ -527,8 +531,9 @@ mod tests { }, body = "{\"hello\": \"world\"}", }) - local json = resp:body_json() - assert(json.hello == "world", "`json.hello` must be 'world'") + local body_json = resp:body_json() + assert(body_json.hello == "world", "`json.hello` must be 'world'") + assert(json_encode(body_json) == "{\"hello\":\"world\"}", "`body_json` must be encoded correctly") }) .exec_async() .await diff --git a/casper-server/src/lua/json.rs b/casper-server/src/lua/json.rs index e64ba49..afb700a 100644 --- a/casper-server/src/lua/json.rs +++ b/casper-server/src/lua/json.rs @@ -84,7 +84,7 @@ impl JsonObject { } /// Converts this `JsonObject` into a Lua `Value`. - fn into_lua(self, lua: &Lua) -> Result { + pub(crate) fn into_lua(self, lua: &Lua) -> Result { match self.current() { serde_json::Value::Null => Ok(Value::NULL), serde_json::Value::Bool(b) => Ok(Value::Boolean(*b)),