Skip to content

Commit d7911da

Browse files
author
Ed Paulosky
authored
fix: Adds HttpBody.isEmpty to help parse S3 404 errors properly (#499)
* Adds `isEmpty` computed var to HttpBody * Adds tests for HttpBody.isEmpty
1 parent 05fbc1b commit d7911da

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Sources/ClientRuntime/Networking/Http/HttpBody.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ public extension HttpBody {
2525
return nil
2626
}
2727
}
28+
29+
/// Returns true if the http body is `.none` or if the underlying data is nil or is empty.
30+
var isEmpty: Bool {
31+
switch self {
32+
case let .data(data):
33+
return data?.isEmpty ?? true
34+
case let .stream(stream):
35+
return stream.toBytes().getData().isEmpty
36+
case .none:
37+
return true
38+
}
39+
}
2840
}
2941

3042

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import XCTest
9+
import AwsCommonRuntimeKit
10+
@testable import ClientRuntime
11+
12+
class HttpBodyTests: XCTestCase {
13+
func testWhenDataIsEmptyThenIsEmptyIsTrue() {
14+
let data = Data()
15+
let body = HttpBody.data(data)
16+
XCTAssertTrue(body.isEmpty)
17+
}
18+
19+
func testWhenDataIsNilThenIsEmptyIsTrue() {
20+
let body = HttpBody.data(nil)
21+
XCTAssertTrue(body.isEmpty)
22+
}
23+
24+
func testWhenDataIsNotEmptyThenIsEmptyIsFalse() {
25+
let data = "foo".data(using: .utf8)!
26+
let body = HttpBody.data(data)
27+
XCTAssertFalse(body.isEmpty)
28+
}
29+
30+
func testWhenStreamIsEmptyThenIsEmptyIsTrue() {
31+
let stream = ByteStream.from(data: Data())
32+
let body = HttpBody.stream(stream)
33+
XCTAssertTrue(body.isEmpty)
34+
}
35+
36+
func testWhenStreamIsNotEmptyThenIsEmptyIsFalse() {
37+
let stream = ByteStream.from(data: "foo".data(using: .utf8)!)
38+
let body = HttpBody.stream(stream)
39+
XCTAssertFalse(body.isEmpty)
40+
}
41+
42+
func testWhenBodyIsNoneThenIsEmptyIsTrue() {
43+
let body = HttpBody.none
44+
XCTAssertTrue(body.isEmpty)
45+
}
46+
}

0 commit comments

Comments
 (0)