Skip to content

Commit bfe6d6f

Browse files
authored
Don't output body for json DELETE requests (#650)
* Don't output body for DELETE requests * Add test for empty HEAD and DELETE JSON requests
1 parent 048ad08 commit bfe6d6f

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

Sources/SotoCore/HTTP/AWSHTTPRequest.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ extension AWSHTTPRequest {
128128
encoder.userInfo[.awsRequest] = requestEncoderContainer
129129
encoder.dateEncodingStrategy = .secondsSince1970
130130
let buffer = try encoder.encodeAsByteBuffer(input, allocator: configuration.byteBufferAllocator)
131-
if method == .GET || method == .HEAD, buffer == ByteBuffer(string: "{}") {
131+
// GET, HEAD and DELETE methods should have an empty body
132+
if method == .GET || method == .HEAD || method == .DELETE, buffer == ByteBuffer(string: "{}") {
132133
body = .init()
133134
} else {
134135
body = .init(buffer: buffer)
@@ -260,6 +261,8 @@ extension AWSHTTPRequest {
260261
guard self.headers["content-type"].first == nil else {
261262
return
262263
}
264+
// in theory we don't need this check as the check for an empty body should skip adding
265+
// a header and a GET or HEAD request should have an empty body
263266
guard self.method != .GET, self.method != .HEAD else {
264267
return
265268
}

Tests/SotoCoreTests/AWSRequestTests.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,13 +501,29 @@ class AWSRequestTests: XCTestCase {
501501
}
502502

503503
/// JSON POST request require a body even if there is no data to POST
504-
func testEmptyJsonObject() {
504+
func testEmptyPostJsonObject() throws {
505505
struct Input: AWSEncodableShape {}
506506
let input = Input()
507507
let config = createServiceConfig(serviceProtocol: .json(version: "1.0"), endpoint: "https://test.com")
508-
var request: AWSHTTPRequest?
509-
XCTAssertNoThrow(request = try AWSHTTPRequest(operation: "Test", path: "/", method: .POST, input: input, configuration: config))
510-
XCTAssertEqual(request?.body.asString(), "{}")
508+
let request = try AWSHTTPRequest(operation: "Test", path: "/", method: .POST, input: input, configuration: config)
509+
XCTAssertEqual(request.body.asString(), "{}")
510+
XCTAssertEqual(request.headers["content-type"].first, "application/x-amz-json-1.0")
511+
}
512+
513+
/// JSON GET, HEAD, DELETE requests should not output a body if it is empty ie `{}`
514+
func testEmptyGetJsonObject() throws {
515+
struct Input: AWSEncodableShape {}
516+
let input = Input()
517+
let config = createServiceConfig(serviceProtocol: .json(version: "1.0"), endpoint: "https://test.com")
518+
let request = try AWSHTTPRequest(operation: "Test", path: "/", method: .GET, input: input, configuration: config)
519+
XCTAssertEqual(request.body.asString(), "")
520+
XCTAssertNil(request.headers["content-type"].first)
521+
let request2 = try AWSHTTPRequest(operation: "Test", path: "/", method: .HEAD, input: input, configuration: config)
522+
XCTAssertEqual(request2.body.asString(), "")
523+
XCTAssertNil(request2.headers["content-type"].first)
524+
let request3 = try AWSHTTPRequest(operation: "Test", path: "/", method: .DELETE, input: input, configuration: config)
525+
XCTAssertEqual(request3.body.asString(), "")
526+
XCTAssertNil(request3.headers["content-type"].first)
511527
}
512528

513529
/// Test host prefix

0 commit comments

Comments
 (0)