Skip to content

Commit

Permalink
fix(request): build request body by operation payload field
Browse files Browse the repository at this point in the history
  • Loading branch information
windmgc committed Jul 31, 2024
1 parent e5b38f7 commit 8685f0d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
77 changes: 74 additions & 3 deletions spec/02-requests/02-build_request_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ describe("operations protocol", function()


local build_request
local operation
local config
local params
local operation, operation_with_payload_field
local config, config_with_payload
local params, params_with_payload
local snapshot
local binary_data

Expand Down Expand Up @@ -105,6 +105,37 @@ describe("operations protocol", function()
}
}

operation_with_payload_field = {
name = "PutObject",
http = {
method = "PUT",
requestUri = "/{Bucket}/{Key+}"
},
input = {
type = "structure",
required = {
"Bucket",
"Key"
},
members = {
Bucket = {
type = "string",
location = "uri",
locationName = "Bucket"
},
Key = {
type = "string",
location = "uri",
locationName = "Key"
},
Body = {
type = "blob",
},
},
payload = "Body"
},
}

config = {
apiVersion = "2011-06-15",
--endpointPrefix = "sts",
Expand All @@ -119,6 +150,19 @@ describe("operations protocol", function()
xmlNamespace = "https://sts.amazonaws.com/doc/2011-06-15/"
}

config_with_payload = {
apiVersion = "2006-03-01",
signingName = "s3",
globalEndpoint = "s3.amazonaws.com",
--protocol = "query",
serviceAbbreviation = "AWS S3",
serviceFullName = "AWS Object Storage",
serviceId = "S3",
signatureVersion = "v4",
uid = "s3-2006-03-01",
xmlNamespace = "https://s3.amazonaws.com/doc/2006-03-01/"
}

params = {
RoleArn = "hello",
RoleSessionName = "world",
Expand All @@ -134,6 +178,12 @@ describe("operations protocol", function()
BinaryData = binary_data,
}

params_with_payload = {
Bucket = "hello",
Key = "world",
Body = binary_data,
}

end)


Expand Down Expand Up @@ -256,6 +306,27 @@ describe("operations protocol", function()
}, request)
end)

it("json: querystring, uri, header and body params, with payload field", function()

config_with_payload.protocol = "json"

local request = build_request(operation_with_payload_field, config_with_payload, params_with_payload)

assert.same({
headers = {
["Content-Length"] = 4,
["X-Amz-Target"] = "s3.PutObject",
["Host"] = "s3.amazonaws.com",
},
method = 'PUT',
path = '/hello/world',
host = 's3.amazonaws.com',
port = 443,
body = binary_data,
query = {},
}, request)
end)


it("rest-xml: querystring, uri, header and body params", function()

Expand Down
15 changes: 15 additions & 0 deletions src/resty/aws/request/build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ local function build_request(operation, config, params)
request.query[k] = v
end

local payload_member = operation.input and operation.input.payload
if payload_member and body_tbl[payload_member] then
local member_config = operation.input.members[payload_member]
if member_config.type == "structure" then
body_tbl = type(body_tbl[payload_member]) == "table" and body_tbl[payload_member] or body_tbl

elseif body_tbl[payload_member] then
request.body = body_tbl[payload_member]
if member_config.type == "binary" and member_config.streaming == true then
request.headers["Content-Type"] = "binary/octet-stream"
end
body_tbl[payload_member] = nil
end
end

local table_empty = not next(body_tbl)
-- already has a raw body, or no body at all
if request.body then
Expand Down

0 comments on commit 8685f0d

Please # to comment.