Skip to content

Commit 77c62da

Browse files
authored
feat(*): decode api response json body with array metatable (#114)
* feat(*): decode api response json body with array metatable * docs(*): add changelog
1 parent aa34df3 commit 77c62da

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ Release process:
177177
1. test installing the rock from LuaRocks
178178

179179

180+
### Unreleased
181+
182+
- feat: decode AWS api response json body with array metatable
183+
[114](https://github.com/Kong/lua-resty-aws/pull/114)
184+
185+
180186
### 1.4.1 (19-Apr-2024)
181187

182188
- fix: patch expanduser function to be more friendly to OpenResty environment

spec/02-requests/03-execute_spec.lua

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,50 @@
11
local restore = require "spec.helpers".restore
2+
local cjson = require "cjson"
23

34
describe("request execution", function()
45
local AWS, Credentials
56

7+
local mock_request_response = {
8+
["s3.amazonaws.com"] = {
9+
["/"] = {
10+
GET = {
11+
status = 200,
12+
headers = {
13+
["x-amz-id-2"] = "test",
14+
["x-amz-request-id"] = "test",
15+
["Date"] = "test",
16+
["Content-Type"] = "application/json",
17+
["Server"] = "AmazonS3",
18+
},
19+
body = [[{"ListAllMyBucketsResult":{"Buckets":[]}}]]
20+
}
21+
}
22+
}
23+
}
24+
625
setup(function()
726
restore()
27+
local http = require "resty.luasocket.http"
28+
http.connect = function(...) return true end
29+
http.request = function(self, req)
30+
return { has_body = true,
31+
status = mock_request_response[req.headers.Host][req.path][req.method].status,
32+
headers = mock_request_response[req.headers.Host][req.path][req.method].headers,
33+
read_body = function()
34+
local resp = mock_request_response[req.headers.Host][req.path][req.method].body
35+
return resp
36+
end
37+
}
38+
end
39+
http.set_timeout = function(...) return true end
40+
http.set_keepalive = function(...) return true end
41+
http.close = function(...) return true end
842
AWS = require "resty.aws"
943
Credentials = require "resty.aws.credentials.Credentials"
1044
end)
1145

1246
teardown(function()
47+
package.loaded["resty.luasocket.http"] = nil
1348
AWS = nil
1449
package.loaded["resty.aws"] = nil
1550
end)
@@ -179,4 +214,25 @@ describe("request execution", function()
179214
assert.same(request.proxy_opts[k], v)
180215
end
181216
end)
217+
218+
it("decoded json body should have array metatable", function ()
219+
local config = {
220+
region = "us-east-1"
221+
}
222+
223+
config.credentials = Credentials:new({
224+
accessKeyId = "teqst_id",
225+
secretAccessKey = "test_key",
226+
})
227+
228+
local aws = AWS(config)
229+
230+
local s3 = aws:S3()
231+
232+
assert.same(type(s3.listBuckets), "function")
233+
local resp = s3:listBuckets()
234+
235+
assert.is_not_nil(resp.body)
236+
assert.same([[{"ListAllMyBucketsResult":{"Buckets":[]}}]], cjson.encode(resp.body))
237+
end)
182238
end)

src/resty/aws/request/execute.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
local http = require "resty.luasocket.http"
2-
local json_decode = require("cjson.safe").new().decode
2+
3+
local json_safe = require("cjson.safe").new()
4+
json_safe.decode_array_with_array_mt(true)
5+
local json_decode = json_safe.decode
36

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

0 commit comments

Comments
 (0)