Skip to content

Commit

Permalink
add unit test cases for appsync response error parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
5d committed May 16, 2024
1 parent d3299a4 commit 633a8f0
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ public class AWSGraphQLSubscriptionTaskRunner<R: Decodable>: InternalTaskRunner,
}
}


internal static func decodeAppSyncRealTimeResponseError(_ data: JSONValue?) -> [Error] {
let knownAppSyncRealTimeRequestErorrs =
decodeAppSyncRealTimeRequestError(data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import XCTest
@testable import AWSPluginsTestCommon

// swiftlint:disable:next type_name
class AWSGraphQLSubscriptionTaskRunnerCancelTests: XCTestCase {
class AWSGraphQLSubscriptionTaskRunnerTests: XCTestCase {
var apiPlugin: AWSAPIPlugin!
var authService: MockAWSAuthService!
var pluginConfig: AWSAPICategoryPluginConfiguration!
Expand Down Expand Up @@ -183,4 +183,102 @@ class AWSGraphQLSubscriptionTaskRunnerCancelTests: XCTestCase {
subscriptionEvents.cancel()
await fulfillment(of: [receivedFailure, receivedCompletion], timeout: 5)
}

func testDecodeAppSyncRealTimeResponseError_withEmptyJsonValue_failedToDecode() {
let errors = AWSGraphQLSubscriptionTaskRunner<String>.decodeAppSyncRealTimeResponseError(nil)
XCTAssertEqual(errors.count, 1)
if case .some(.operationError(let description, _, _)) = errors.first as? APIError {
XCTAssertTrue(description.contains("Failed to decode AppSync error response"))
} else {
XCTFail("Should be failed with APIError")
}
}

func testDecodeAppSYncRealTimeResponseError_withKnownAppSyncRealTimeRequestError_returnKnownErrors() {
let errorJson: JSONValue = [
"errors": [[
"message": "test1",
"errorType": "MaxSubscriptionsReachedError"
]]
]

let errors = AWSGraphQLSubscriptionTaskRunner<String>.decodeAppSyncRealTimeResponseError(errorJson)
XCTAssertEqual(errors.count, 1)
guard case .maxSubscriptionsReached = errors.first as? AppSyncRealTimeRequest.Error else {
XCTFail("Should be AppSyncRealTimeRequestError")
return
}
}

func testDecodeAppSYncRealTimeResponseError_withUnknownError_returnParsedGraphQLError() {
let errorJson: JSONValue = [
"errors": [[
"message": "test1",
"errorType": "Unknown"
]]
]

let errors = AWSGraphQLSubscriptionTaskRunner<String>.decodeAppSyncRealTimeResponseError(errorJson)
XCTAssertEqual(errors.count, 1)
XCTAssertTrue(errors.first is GraphQLError)
}

func testDecodeAppSyncRealTimeRequestError_withoutErrorsField_returnEmptyErrors() {
let errorJson: JSONValue = [
"noErrors": [[
"message": "test1",
"errorType": "Unknown"
]]
]

let errors = AWSGraphQLSubscriptionTaskRunner<String>.decodeAppSyncRealTimeRequestError(errorJson)
XCTAssertEqual(errors.count, 0)
}

func testDecodeAppSyncRealTimeRequestError_withWellFormatErrors_parseErrors() {
let errorJson: JSONValue = [
"errors": [[
"message": "test1",
"errorType": "MaxSubscriptionsReachedError"
], [
"message": "test2",
"errorType": "LimitExceededError"
], [
"message": "test3",
"errorType": "Unauthorized"
]]
]

let errors = AWSGraphQLSubscriptionTaskRunner<String>.decodeAppSyncRealTimeRequestError(errorJson)
XCTAssertEqual(errors.count, 3)
}

func testDecodeAppSyncRealTimeRequestError_withSomeWellFormatErrors_parseErrors() {
let errorJson: JSONValue = [
"errors": [[
"message": "test1",
"errorType": "MaxSubscriptionsReachedError"
], [
"message": "test2",
"errorType": "LimitExceededError"
], [
"random": "123"
]]
]

let errors = AWSGraphQLSubscriptionTaskRunner<String>.decodeAppSyncRealTimeRequestError(errorJson)
XCTAssertEqual(errors.count, 2)
}

func testDecodeAppSyncRealTimeRequestError_withSingletonErrors_parseErrors() {
let errorJson: JSONValue = [
"errors": [
"message": "test1",
"errorType": "MaxSubscriptionsReachedError"
]
]

let errors = AWSGraphQLSubscriptionTaskRunner<String>.decodeAppSyncRealTimeRequestError(errorJson)
XCTAssertEqual(errors.count, 1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class LocalWebSocketServer {
stack.applicationProtocols.insert(ws, at: 0)
let port = NWEndpoint.Port(rawValue: portNumber)!
guard let listener = try? NWListener(using: params, on: port) else {
throw "unable to start the listener at: localhost:\(port)"
throw LocalWebSocketServerError.error("unable to start the listener at: localhost:\(port)")
}

listener.newConnectionHandler = { [weak self] conn in
Expand Down Expand Up @@ -93,7 +93,7 @@ class LocalWebSocketServer {

func sendTransientFailureToConnections() {
self.connections.forEach {
var metadata = NWProtocolWebSocket.Metadata(opcode: .close)
let metadata = NWProtocolWebSocket.Metadata(opcode: .close)
metadata.closeCode = .protocolCode(NWProtocolWebSocket.CloseCode.Defined.internalServerError)
$0.send(
content: nil,
Expand All @@ -103,3 +103,7 @@ class LocalWebSocketServer {
}
}
}

enum LocalWebSocketServerError: Error {
case error(String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import XCTest
@testable import AWSPluginsCore
@testable @_spi(RetryWithJitter) import AmplifyNetwork

class RetryWithJitterTests: XCTestCase {
struct TestError: Error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import XCTest
import Combine
@testable import AWSPluginsCore
@testable @_spi(WebSocket) @_spi(NetworkReachability) import AmplifyNetwork

fileprivate let timeout: TimeInterval = 5

Expand Down
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ let internalNetworkingTargets: [Target] = [
.testTarget(
name: "AmplifyNetworkUnitTests",
dependencies: [
"AmplifyTestCommon",
"AmplifyNetwork"
],
path: "AmplifyPlugins/Internal/Tests/NetworkTests"
Expand Down

0 comments on commit 633a8f0

Please sign in to comment.