Skip to content

Commit

Permalink
fix(http): encode time and bigs in url get parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
matteo-cristino authored and jaromil committed Sep 10, 2024
1 parent 5005fea commit dd80633
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/lua/zencode_http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,23 @@ 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(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)
local separator = fif( url:str():find('?'), '&', '?' )
ACK[dst] = O.from_string(url:str() .. separator ..
"Cannot append http request to invalid url: "..dst)
local separator = fif( url:str():find('?'), '&', '?' )
local tv = type(arg)
if tv == 'zenroom.time' or tv == 'zenroom.big' then
arg = tostring(arg)
elseif tv == 'zenroom.octet' then
arg = arg:str()
else
error("Unexpected value type '"..tv.."' for "..ele, 2)
end
ACK[dst] = O.from_string(url:str() .. separator ..
encoding_f(ele)
.. '=' ..
encoding_f(arg:str())
encoding_f(arg)
)
end

Expand All @@ -175,7 +183,15 @@ local function _get_parameters_from_table(table_params, encoding_f)
end
local res = ""
for k,v in pairs(params) do
res = res..encoding_f(k).."="..encoding_f(v:str()).."&"
local tv = type(v)
if tv == 'zenroom.time' or tv == 'zenroom.big' then
v = tostring(v)
elseif tv == 'zenroom.octet' then
v = v:str()
else
error("Unexpected value type '"..tv.."' in "..table_params, 2)
end
res = res..encoding_f(k).."="..encoding_f(v).."&"
end
if #res > 1 then res = res:sub(1, -2) end
ACK.http_get_parameters = O.from_string(res)
Expand Down
34 changes: 34 additions & 0 deletions test/zencode/http.bats
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,37 @@ EOF
save_output 'append_values.json'
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 <<EOF | save_asset append_different_values.data
{
"param1": "value1",
"param2": "1000",
"param3": 2000
}
EOF
cat <<EOF | zexe append_different_values.zen append_different_values.data
Scenario 'http': create a GET request concatenating values on a HTTP url
# the parameters are loaded as string dictionary
Given I have a 'string' named 'param1'
Given I have a 'integer' named 'param2'
Given I have a 'time' named 'param3'
When I create the 'string dictionary' named 'parameters'
and I copy 'param1' in 'parameters'
and I copy 'param2' in 'parameters'
and I copy 'param3' in 'parameters'
# create the get parameters string
When I create the http get parameters from 'parameters'
and I rename 'http_get_parameters' to 'get_parameters_without_percent_endoing'
When I create the http get parameters from 'parameters' using percent encoding
and I rename 'http_get_parameters' to 'get_parameters_with_percent_endoing'
Then print the 'get_parameters_without_percent_endoing'
and print the 'get_parameters_with_percent_endoing'
EOF
save_output 'append_different_values.json'
assert_output '{"get_parameters_with_percent_endoing":"param2=1000&param3=2000&param1=value1","get_parameters_without_percent_endoing":"param2=1000&param3=2000&param1=value1"}'
}

0 comments on commit dd80633

Please sign in to comment.