|
1 | 1 | local restore = require "spec.helpers".restore
|
| 2 | +local cjson = require "cjson" |
2 | 3 |
|
3 | 4 | describe("request execution", function()
|
4 | 5 | local AWS, Credentials
|
5 | 6 |
|
| 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 | + |
6 | 25 | setup(function()
|
7 | 26 | 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 |
8 | 42 | AWS = require "resty.aws"
|
9 | 43 | Credentials = require "resty.aws.credentials.Credentials"
|
10 | 44 | end)
|
11 | 45 |
|
12 | 46 | teardown(function()
|
| 47 | + package.loaded["resty.luasocket.http"] = nil |
13 | 48 | AWS = nil
|
14 | 49 | package.loaded["resty.aws"] = nil
|
15 | 50 | end)
|
@@ -179,4 +214,25 @@ describe("request execution", function()
|
179 | 214 | assert.same(request.proxy_opts[k], v)
|
180 | 215 | end
|
181 | 216 | 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) |
182 | 238 | end)
|
0 commit comments