diff --git a/README.md b/README.md index 01cdef4..142fa59 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/spec/02-requests/03-execute_spec.lua b/spec/02-requests/03-execute_spec.lua index ed2180b..48b0c2b 100644 --- a/spec/02-requests/03-execute_spec.lua +++ b/spec/02-requests/03-execute_spec.lua @@ -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) @@ -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) diff --git a/src/resty/aws/request/execute.lua b/src/resty/aws/request/execute.lua index d05f8fa..88ac2d7 100644 --- a/src/resty/aws/request/execute.lua +++ b/src/resty/aws/request/execute.lua @@ -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