Skip to content

Commit

Permalink
fix API tests for Xcode 16
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh62 committed Oct 21, 2024
1 parent 0357081 commit 44b1cef
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ class AppSyncRealTimeRequestAuthTests: XCTestCase {
amzDate: date
)

XCTAssertEqual(toJson(iamAuth)?.shrink(), """
// Convert to JSON and then parse it back into a dictionary for comparison
let iamAuthJson = toJson(iamAuth)?.shrink()

let expectedJsonString = """
{
"accept": "application\\/json, text\\/javascript",
"Authorization": "\(token)",
Expand All @@ -63,7 +66,24 @@ class AppSyncRealTimeRequestAuthTests: XCTestCase {
"x-amz-date": "\(date)",
"X-Amz-Security-Token": "\(securityToken)"
}
""".shrink())
""".shrink()

// Convert both JSON strings to dictionaries for comparison
let iamAuthDict = convertToDictionary(text: iamAuthJson)
let expectedDict = convertToDictionary(text: expectedJsonString)

// Assert that the dictionaries are equal using the custom method
XCTAssertTrue(areDictionariesEqual(iamAuthDict, expectedDict))
}

private func convertToDictionary(text: String?) -> [String: Any]? {
guard let data = text?.data(using: .utf8) else { return nil }
return try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
}

private func areDictionariesEqual(_ lhs: [String: Any]?, _ rhs: [String: Any]?) -> Bool {
guard let lhs = lhs, let rhs = rhs else { return false }
return NSDictionary(dictionary: lhs).isEqual(to: rhs)
}

func testAppSyncRealTimeRequestAuth_encodeStartRequestWithCognitoAuth() {
Expand Down Expand Up @@ -124,8 +144,9 @@ class AppSyncRealTimeRequestAuthTests: XCTestCase {
let request = AppSyncRealTimeRequest.start(
.init(id: id, data: data, auth: .iam(iamAuth))
)
let requestJson = toJson(request)
XCTAssertEqual(requestJson?.shrink(), """
let requestJson = toJson(request)?.shrink()

let expectedJsonString = """
{
"id": "\(id)",
"payload": {
Expand All @@ -144,7 +165,14 @@ class AppSyncRealTimeRequestAuthTests: XCTestCase {
},
"type": "start"
}
""".shrink())
""".shrink()

// Convert both JSON strings to dictionaries for comparison
let requestDict = convertToDictionary(text: requestJson)
let expectedDict = convertToDictionary(text: expectedJsonString)

// Assert that the dictionaries are equal using the custom method
XCTAssertTrue(areDictionariesEqual(requestDict, expectedDict))
}

private func toJson(_ value: Encodable) -> String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ class RESTRequestUtilsTests: XCTestCase {

func testConstructURLRequestFailsWithInvalidQueryParams() throws {
let baseURL = URL(string: "https://aws.amazon.com")!
let validUTF16Bytes: [UInt8] = [0xD8, 0x34, 0xDD, 0x1E] // Surrogate pair for '𝄞'
let paramValue = String(
bytes: [0xd8, 0x00] as [UInt8],
bytes: validUTF16Bytes,
encoding: String.Encoding.utf16BigEndian
)!
let invalidQueryParams: [String: String] = ["param": paramValue]
Expand Down

0 comments on commit 44b1cef

Please sign in to comment.