Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(*): decode api response json body with array metatable #114

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ Release process:
1. test installing the rock from LuaRocks


### Unreleased

- feat: decode AWS api response json body with array metatable
[114](https://github.com/Kong/lua-resty-aws/pull/114)


### 1.4.1 (19-Apr-2024)

- fix: patch expanduser function to be more friendly to OpenResty environment
Expand Down
56 changes: 56 additions & 0 deletions spec/02-requests/03-execute_spec.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
local restore = require "spec.helpers".restore
local cjson = require "cjson"

describe("request execution", function()
local AWS, Credentials

local mock_request_response = {
["s3.amazonaws.com"] = {
["/"] = {
GET = {
status = 200,
headers = {
["x-amz-id-2"] = "test",
["x-amz-request-id"] = "test",
["Date"] = "test",
["Content-Type"] = "application/json",
["Server"] = "AmazonS3",
},
body = [[{"ListAllMyBucketsResult":{"Buckets":[]}}]]
}
}
}
}

setup(function()
restore()
local http = require "resty.luasocket.http"
http.connect = function(...) return true end
http.request = function(self, req)
return { has_body = true,
status = mock_request_response[req.headers.Host][req.path][req.method].status,
headers = mock_request_response[req.headers.Host][req.path][req.method].headers,
read_body = function()
local resp = mock_request_response[req.headers.Host][req.path][req.method].body
return resp
end
}
end
http.set_timeout = function(...) return true end
http.set_keepalive = function(...) return true end
http.close = function(...) return true end
AWS = require "resty.aws"
Credentials = require "resty.aws.credentials.Credentials"
end)

teardown(function()
package.loaded["resty.luasocket.http"] = nil
AWS = nil
package.loaded["resty.aws"] = nil
end)
Expand Down Expand Up @@ -179,4 +214,25 @@
assert.same(request.proxy_opts[k], v)
end
end)

it("decoded json body should have array metatable", function ()
local config = {
region = "us-east-1"
}

config.credentials = Credentials:new({
accessKeyId = "teqst_id",
secretAccessKey = "test_key",
})

local aws = AWS(config)

local s3 = aws:S3()

assert.same(type(s3.listBuckets), "function")
local resp = s3:listBuckets()

assert.is_not_nil(resp.body)
assert.same([[{"ListAllMyBucketsResult":{"Buckets":[]}}]], cjson.encode(resp.body))
end)
end)
5 changes: 4 additions & 1 deletion src/resty/aws/request/execute.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local http = require "resty.luasocket.http"
local json_decode = require("cjson.safe").new().decode

local json_safe = require("cjson.safe").new()
json_safe.decode_array_with_array_mt(true)
local json_decode = json_safe.decode

-- TODO: retries and back-off: https://docs.aws.amazon.com/general/latest/gr/api-retries.html

Expand Down
Loading