-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Add aws_json error code tests #1798
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,20 +17,7 @@ class RestJSONErrorTests: HttpResponseTestBase { | |
let host = "myapi.host.com" | ||
|
||
func testRestJsonComplexError() async throws { | ||
guard let httpResponse = buildHttpResponse( | ||
code: 400, | ||
headers: [ | ||
"Content-Type": "application/json", | ||
"X-Header": "Header", | ||
"X-Amzn-Errortype": "ComplexError" | ||
], | ||
content: ByteStream.data(""" | ||
{\"TopLevel\": \"Top level\"} | ||
""".data(using: .utf8)) | ||
) else { | ||
XCTFail("Something is wrong with the created http response") | ||
return | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic above is extracted to a helper method, defined below. |
||
let httpResponse = try httpResponse(errorCode: "my.protocoltests.restjson#ComplexError:http://my.fake.com") | ||
|
||
let greetingWithErrorsError = try await GreetingWithErrorsError.httpError(from:)(httpResponse) | ||
|
||
|
@@ -47,16 +34,35 @@ class RestJSONErrorTests: HttpResponseTestBase { | |
} | ||
} | ||
|
||
func testSanitizeErrorName() { | ||
let errorNames = [ | ||
private func httpResponse(errorCode: String) throws -> HTTPResponse { | ||
guard let response = buildHttpResponse( | ||
code: 400, | ||
headers: [ | ||
"Content-Type": "application/json", | ||
"X-Header": "Header", | ||
"X-Amzn-Errortype": errorCode, | ||
], | ||
content: ByteStream.data(Data("{\"TopLevel\": \"Top level\"}".utf8)) | ||
) else { | ||
throw TestError("Something is wrong with the created http response") | ||
} | ||
return response | ||
} | ||
|
||
func testSanitizeErrorName() async throws { | ||
let errorCodes = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These error codes match the ones used for AWS JSON |
||
"FooError", | ||
" FooError ", | ||
"FooError:http://my.fake.com/", | ||
"my.protocoltests.restjson#FooError", | ||
"my.protocoltests.restjson#FooError:http://my.fake.com" | ||
"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/" | ||
] | ||
|
||
for errorName in errorNames { | ||
XCTAssertEqual(sanitizeErrorType(errorName), "FooError") | ||
for errorCode in errorCodes { | ||
let httpResponse = try httpResponse(errorCode: errorCode) | ||
let reader = try await Reader.from(data: httpResponse.body.readData() ?? Data()) | ||
let restJSONError = try RestJSONError(httpResponse: httpResponse, responseReader: reader, noErrorWrapping: true) | ||
XCTAssertEqual(restJSONError.code, "FooError", "Error code '\(errorCode)' was not sanitized correctly, result was '\(restJSONError.code)'") | ||
} | ||
} | ||
} | ||
|
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 } | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests are added below to ensure that error code can be decoded no matter which of the allowed places it is located.