From f084af068159f4ae4628d15ffbd3f4f4b8db55af Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Tue, 15 Oct 2024 10:33:45 +0200 Subject: [PATCH] fix(http): allow to append also numbers as url query parameters --- src/lua/zencode_http.lua | 19 +++++++++------- test/zencode/http.bats | 47 +++++++++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/lua/zencode_http.lua b/src/lua/zencode_http.lua index fd06be0ae..9df0d90ef 100644 --- a/src/lua/zencode_http.lua +++ b/src/lua/zencode_http.lua @@ -159,13 +159,15 @@ end) local function _append_to_url(ele, dst, encoding_f) local arg, arg_c = have(ele) local url, url_c = have(dst) - zencode_assert(arg_c.encoding == 'string' and luatype(arg) ~= 'table', - "Cannot append http request that are not strings: "..ele) - zencode_assert(url_c.content == 'url', - "Cannot append http request to invalid url: "..dst) + if url_c.content ~= 'url' then + error("Cannot append http request to invalid url: "..dst, 2) + end + if luatype(arg) == 'table' then + error("Cannot append table to url: "..dst, 2) + end local separator = fif( url:str():find('?'), '&', '?' ) local tv = type(arg) - if tv == 'zenroom.time' or tv == 'zenroom.big' then + if tv == 'zenroom.time' or tv == 'zenroom.big' or tv == 'zenroom.float' then arg = tostring(arg) elseif tv == 'zenroom.octet' then arg = arg:str() @@ -193,13 +195,14 @@ local function _get_parameters_from_table(table_params, encoding_f) if(params_c.zentype ~= 'd') then error("Expected dictionary, found "..params_c.zentype.." for "..table_params, 2) end - if(params_c.encoding ~= 'string') then - error("Parameters in "..table_params.." must be strings", 2) + if( params_c.encoding ~= 'string' and params_c.encoding ~= 'float' and + params_c.encoding ~= 'integer' and params_c.encoding ~= 'time') then + error("Parameters in "..table_params.." must be of type 'string', 'float', 'integer' or 'time'", 2) end local res = "" for k,v in pairs(params) do local tv = type(v) - if tv == 'zenroom.time' or tv == 'zenroom.big' then + if tv == 'zenroom.time' or tv == 'zenroom.big' or tv == 'zenroom.float' then v = tostring(v) elseif tv == 'zenroom.octet' then v = v:str() diff --git a/test/zencode/http.bats b/test/zencode/http.bats index 0dbab14e0..dc9f61945 100644 --- a/test/zencode/http.bats +++ b/test/zencode/http.bats @@ -219,26 +219,28 @@ EOF assert_output '{"empty_get_parameters":"","get_parameters_with_percent_endoing":"client_id=did%3Adyne%3Asandbox.signroom%3APTDvvQn1iWQiVxkfsDnUid8FbieKbHq46Qs8c9CZx67&code=eyJhbGciOiJFUzI1NiJ9.eyJzdWIiOiJlYWMyMjNmOTMwYmRkNzk1NDdlNzI2ZGRjZTg5ZTlmYTA1NWExZTFjIiwiaWF0IjoxNzA5MDQ4MzkzMjk1LCJpc3MiOiJodHRwczovL3ZhbGlkLmlzc3Vlci51cmwiLCJhdWQiOiJkaWQ6ZHluZTpzYW5kYm94LmdlbmVyaWNpc3N1ZXI6NkNwOG1QVXZKbVFhTXhRUFNuTnloYjc0ZjlHYTRXcWZYQ2tCbmVGZ2lrbTUiLCJleHAiOjE3MDkwNTE5OTN9.TahF8CqDDj5yynvtvkhr-Gt6RjzHSvKMosOhFf5sVmWGohKBPMNFhI8WlBlWj7aRauXB0lsvbQk03lf4eZN-2g&redirectUris=https%3A%2F%2Fdidroom.com%2F&grant_type=authorization_code&code_verifier=JYTDgtxcjiNqa3AvJjfubMX6gx98-wCH7iTydBYAeFg","get_parameters_without_percent_endoing":"client_id=did:dyne:sandbox.signroom:PTDvvQn1iWQiVxkfsDnUid8FbieKbHq46Qs8c9CZx67&code=eyJhbGciOiJFUzI1NiJ9.eyJzdWIiOiJlYWMyMjNmOTMwYmRkNzk1NDdlNzI2ZGRjZTg5ZTlmYTA1NWExZTFjIiwiaWF0IjoxNzA5MDQ4MzkzMjk1LCJpc3MiOiJodHRwczovL3ZhbGlkLmlzc3Vlci51cmwiLCJhdWQiOiJkaWQ6ZHluZTpzYW5kYm94LmdlbmVyaWNpc3N1ZXI6NkNwOG1QVXZKbVFhTXhRUFNuTnloYjc0ZjlHYTRXcWZYQ2tCbmVGZ2lrbTUiLCJleHAiOjE3MDkwNTE5OTN9.TahF8CqDDj5yynvtvkhr-Gt6RjzHSvKMosOhFf5sVmWGohKBPMNFhI8WlBlWj7aRauXB0lsvbQk03lf4eZN-2g&redirectUris=https://didroom.com/&grant_type=authorization_code&code_verifier=JYTDgtxcjiNqa3AvJjfubMX6gx98-wCH7iTydBYAeFg"}' } -@test "create a GET parameters from a with big and time" { - cat <