-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for requiresLength trait and Transfer-Encoding: Chu…
…nked (#604)
- Loading branch information
Showing
8 changed files
with
294 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
Tests/ClientRuntimeTests/NetworkingTests/ContentLengthMiddlewareTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0. | ||
|
||
import XCTest | ||
import SmithyTestUtil | ||
@testable import ClientRuntime | ||
|
||
class ContentLengthMiddlewareTests: XCTestCase { | ||
private var builtContext: HttpContext! | ||
private var stack: OperationStack<MockInput, MockOutput, MockMiddlewareError>! | ||
|
||
override func setUpWithError() throws { | ||
try super.setUpWithError() | ||
builtContext = HttpContextBuilder() | ||
.withMethod(value: .get) | ||
.withPath(value: "/") | ||
.withEncoder(value: JSONEncoder()) | ||
.withDecoder(value: JSONDecoder()) | ||
.withOperation(value: "Test Operation") | ||
.build() | ||
stack = OperationStack<MockInput, MockOutput, MockMiddlewareError>(id: "Test Operation") | ||
} | ||
|
||
func testTransferEncodingChunkedSetWhenStreamLengthIsNil() async throws { | ||
addContentLengthMiddlewareWith(requiresLength: false, unsignedPayload: true) | ||
forceEmptyStream() | ||
try await AssertHeadersArePresent(expectedHeaders: ["Transfer-Encoding": "Chunked"]) | ||
} | ||
|
||
func testContentLengthSetWhenStreamLengthAvailableAndRequiresLengthSet() async throws { | ||
addContentLengthMiddlewareWith(requiresLength: true, unsignedPayload: false) | ||
try await AssertHeadersArePresent(expectedHeaders: ["Content-Length": "0"]) | ||
} | ||
|
||
func testContentLengthSetWhenRequiresLengthAndUnsignedPayload() async throws { | ||
addContentLengthMiddlewareWith(requiresLength: true, unsignedPayload: true) | ||
try await AssertHeadersArePresent(expectedHeaders: ["Content-Length": "0"]) | ||
} | ||
|
||
func testRequiresLengthSetWithNilStreamShouldThrowError() async throws { | ||
addContentLengthMiddlewareWith(requiresLength: true, unsignedPayload: false) | ||
forceEmptyStream() | ||
do { | ||
try await AssertHeadersArePresent(expectedHeaders: ["Content-Length": "0"]) | ||
XCTFail("Should throw error") | ||
} catch let error as StreamError { | ||
switch error { | ||
case .notSupported("Missing content-length for SigV4 signing on operation: Test Operation"), .notSupported("Missing content-length for operation: Test Operation"): | ||
// The error matches one of the expected cases, test passes | ||
break | ||
default: | ||
XCTFail("Error is not StreamError.notSupported with expected message") | ||
} | ||
} | ||
} | ||
|
||
private func addContentLengthMiddlewareWith(requiresLength: Bool, unsignedPayload: Bool) { | ||
stack.finalizeStep.intercept( | ||
position: .before, | ||
middleware: ContentLengthMiddleware(requiresLength: requiresLength, unsignedPayload: unsignedPayload) | ||
) | ||
} | ||
|
||
private func forceEmptyStream() { | ||
// Force stream length to be nil | ||
stack.finalizeStep.intercept(position: .before, id: "set nil stream length") { (context, input, next) -> OperationOutput<MockOutput> in | ||
input.body = .stream(BufferedStream()) // Set the stream length to nil | ||
return try await next.handle(context: context, input: input) | ||
} | ||
} | ||
|
||
private func AssertHeadersArePresent(expectedHeaders: [String: String], file: StaticString = #file, line: UInt = #line) async throws -> Void { | ||
let mockHandler = MockHandler { (_, input) in | ||
for (key, value) in expectedHeaders { | ||
XCTAssert(input.headers.value(for: key) == value, file: file, line: line) | ||
} | ||
let httpResponse = HttpResponse(body: HttpBody.none, statusCode: HttpStatusCode.ok) | ||
let mockOutput = try! MockOutput(httpResponse: httpResponse, decoder: nil) | ||
let output = OperationOutput<MockOutput>(httpResponse: httpResponse, output: mockOutput) | ||
return output | ||
} | ||
|
||
_ = try await stack.handleMiddleware(context: builtContext, input: MockInput(), next: mockHandler) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.