-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add aws_json error code tests (#1798)
- Loading branch information
Showing
3 changed files
with
158 additions
and
21 deletions.
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
...re/AWSClientRuntime/Tests/AWSClientRuntimeTests/Protocols/AWSJSON/AWSJSONErrorTests.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,119 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import SmithyTestUtil | ||
import SmithyHTTPAPI | ||
import Smithy | ||
@_spi(SmithyReadWrite) import class SmithyJSON.Reader | ||
@_spi(SmithyReadWrite) import struct AWSClientRuntime.AWSJSONError | ||
@_spi(SmithyReadWrite) import enum ClientRuntime.BaseErrorDecodeError | ||
import XCTest | ||
|
||
class AWSJSONErrorTests: HttpResponseTestBase { | ||
// These error codes are taken from the examples in | ||
// https://smithy.io/2.0/aws/protocols/aws-json-1_0-protocol.html#operation-error-serialization | ||
// (one extra case with leading & trailing whitespace is added) | ||
let errorCodes = [ | ||
"FooError", | ||
" FooError ", | ||
"FooError:http://internal.amazon.com/coral/com.amazon.coral.validate/", | ||
"aws.protocoltests.restjson#FooError", | ||
"aws.protocoltests.restjson#FooError:http://internal.amazon.com/coral/com.amazon.coral.validate/" | ||
] | ||
|
||
// MARK: - error code decoding & sanitization | ||
|
||
func test_errorInitThrowsIfNoCode() async throws { | ||
let httpResponse = try httpResponseWithNoErrorCode() | ||
let reader = try await Reader.from(data: httpResponse.body.readData() ?? Data()) | ||
XCTAssertThrowsError(try AWSJSONError(httpResponse: httpResponse, responseReader: reader, noErrorWrapping: true)) { error in | ||
XCTAssertTrue((error as? BaseErrorDecodeError) == BaseErrorDecodeError.missingRequiredData) | ||
} | ||
} | ||
|
||
func test_sanitizeErrorCodeInHeader() async throws { | ||
for errorCode in errorCodes { | ||
let httpResponse = try httpResponseWithHeaderErrorCode(errorCode: errorCode) | ||
let reader = try await Reader.from(data: httpResponse.body.readData() ?? Data()) | ||
let awsJSONError = try AWSJSONError(httpResponse: httpResponse, responseReader: reader, noErrorWrapping: true) | ||
XCTAssertEqual(awsJSONError.code, "FooError", "Error code '\(errorCode)' was not sanitized correctly, result was '\(awsJSONError.code)'") | ||
} | ||
} | ||
|
||
func test_sanitizeErrorCodeInCodeField() async throws { | ||
for errorCode in errorCodes { | ||
let httpResponse = try httpResponseWithCodeFieldErrorCode(errorCode: errorCode) | ||
let reader = try await Reader.from(data: httpResponse.body.readData() ?? Data()) | ||
let awsJSONError = try AWSJSONError(httpResponse: httpResponse, responseReader: reader, noErrorWrapping: true) | ||
XCTAssertEqual(awsJSONError.code, "FooError", "Error code '\(errorCode)' was not sanitized correctly, result was '\(awsJSONError.code)'") | ||
} | ||
} | ||
|
||
func test_sanitizeErrorCodeInTypeField() async throws { | ||
for errorCode in errorCodes { | ||
let httpResponse = try httpResponseWithTypeFieldErrorCode(errorCode: errorCode) | ||
let reader = try await Reader.from(data: httpResponse.body.readData() ?? Data()) | ||
let awsJSONError = try AWSJSONError(httpResponse: httpResponse, responseReader: reader, noErrorWrapping: true) | ||
XCTAssertEqual(awsJSONError.code, "FooError", "Error code '\(errorCode)' was not sanitized correctly, result was '\(awsJSONError.code)'") | ||
} | ||
} | ||
|
||
// MARK: - Private methods | ||
|
||
private func httpResponseWithNoErrorCode() throws -> HTTPResponse { | ||
guard let response = buildHttpResponse( | ||
code: 400, | ||
headers: [ | ||
"Content-Type": "application/json", | ||
], | ||
content: ByteStream.data(Data("{}".utf8)) | ||
) else { | ||
throw TestError("Something is wrong with the created http response") | ||
} | ||
return response | ||
} | ||
|
||
private func httpResponseWithHeaderErrorCode(errorCode: String) throws -> HTTPResponse { | ||
guard let response = buildHttpResponse( | ||
code: 400, | ||
headers: [ | ||
"Content-Type": "application/json", | ||
"X-Amzn-Errortype": errorCode, | ||
], | ||
content: ByteStream.data(Data("{}".utf8)) | ||
) else { | ||
throw TestError("Something is wrong with the created http response") | ||
} | ||
return response | ||
} | ||
|
||
private func httpResponseWithCodeFieldErrorCode(errorCode: String) throws -> HTTPResponse { | ||
guard let response = buildHttpResponse( | ||
code: 400, | ||
headers: [ | ||
"Content-Type": "application/json", | ||
], | ||
content: ByteStream.data(Data("{\"code\":\"\(errorCode)\"}".utf8)) | ||
) else { | ||
throw TestError("Something is wrong with the created http response") | ||
} | ||
return response | ||
} | ||
|
||
private func httpResponseWithTypeFieldErrorCode(errorCode: String) throws -> HTTPResponse { | ||
guard let response = buildHttpResponse( | ||
code: 400, | ||
headers: [ | ||
"Content-Type": "application/json", | ||
], | ||
content: ByteStream.data(Data("{\"__type\":\"\(errorCode)\"}".utf8)) | ||
) else { | ||
throw TestError("Something is wrong with the created http response") | ||
} | ||
return response | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
Sources/Core/AWSClientRuntime/Tests/AWSClientRuntimeTests/TestError.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,12 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
struct TestError: Error { | ||
let description: String | ||
|
||
init(_ description: String) { self.description = description } | ||
} |