Skip to content

Commit

Permalink
feat(*): decode api response json body with array metatable
Browse files Browse the repository at this point in the history
  • Loading branch information
windmgc committed May 13, 2024
1 parent aa34df3 commit da5f93a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
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 @@ describe("request execution", function()
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

0 comments on commit da5f93a

Please sign in to comment.