@@ -37,6 +37,7 @@ class GraphQLSubscribeTasksTests: OperationTestBase {
37
37
38
38
var connectionStateSink : AnyCancellable ?
39
39
var subscriptionDataSink : AnyCancellable ?
40
+ var expectedCompletionFailureError : APIError ?
40
41
41
42
override func setUp( ) async throws {
42
43
try await super. setUp ( )
@@ -109,7 +110,7 @@ class GraphQLSubscribeTasksTests: OperationTestBase {
109
110
await waitForSubscriptionExpectations ( )
110
111
}
111
112
112
- func testConnectionError ( ) async throws {
113
+ func testConnectionErrorWithLimitExceeded ( ) async throws {
113
114
await receivedCompletionSuccess. setShouldTrigger ( false )
114
115
await receivedCompletionFailure. setShouldTrigger ( true )
115
116
await receivedStateValueConnecting. setShouldTrigger ( true )
@@ -123,8 +124,65 @@ class GraphQLSubscribeTasksTests: OperationTestBase {
123
124
await waitForExpectations ( [ onSubscribeInvoked] , timeout: 0.05 )
124
125
125
126
subscriptionEventHandler ( . connection( . connecting) , subscriptionItem)
126
- subscriptionEventHandler ( . failed( " Error " ) , subscriptionItem)
127
+ subscriptionEventHandler ( . failed( ConnectionProviderError . limitExceeded ( nil ) ) , subscriptionItem)
128
+ expectedCompletionFailureError = APIError . operationError ( " " , " " , ConnectionProviderError . limitExceeded ( nil ) )
129
+ await waitForSubscriptionExpectations ( )
130
+ }
131
+
132
+ func testConnectionErrorWithSubscriptionError( ) async throws {
133
+ await receivedCompletionSuccess. setShouldTrigger ( false )
134
+ await receivedCompletionFailure. setShouldTrigger ( true )
135
+ await receivedStateValueConnecting. setShouldTrigger ( true )
136
+ await receivedStateValueConnected. setShouldTrigger ( false )
137
+ await receivedStateValueDisconnected. setShouldTrigger ( false )
138
+
139
+ await receivedDataValueSuccess. setShouldTrigger ( false )
140
+ await receivedDataValueError. setShouldTrigger ( false )
141
+
142
+ try await subscribe ( )
143
+ await waitForExpectations ( [ onSubscribeInvoked] , timeout: 0.05 )
127
144
145
+ subscriptionEventHandler ( . connection( . connecting) , subscriptionItem)
146
+ subscriptionEventHandler ( . failed( ConnectionProviderError . subscription ( " " , nil ) ) , subscriptionItem)
147
+ expectedCompletionFailureError = APIError . operationError ( " " , " " , ConnectionProviderError . subscription ( " " , nil ) )
148
+ await waitForSubscriptionExpectations ( )
149
+ }
150
+
151
+ func testConnectionErrorWithConnectionUnauthorizedError( ) async throws {
152
+ await receivedCompletionSuccess. setShouldTrigger ( false )
153
+ await receivedCompletionFailure. setShouldTrigger ( true )
154
+ await receivedStateValueConnecting. setShouldTrigger ( true )
155
+ await receivedStateValueConnected. setShouldTrigger ( false )
156
+ await receivedStateValueDisconnected. setShouldTrigger ( false )
157
+
158
+ await receivedDataValueSuccess. setShouldTrigger ( false )
159
+ await receivedDataValueError. setShouldTrigger ( false )
160
+
161
+ try await subscribe ( )
162
+ await waitForExpectations ( [ onSubscribeInvoked] , timeout: 0.05 )
163
+
164
+ subscriptionEventHandler ( . connection( . connecting) , subscriptionItem)
165
+ subscriptionEventHandler ( . failed( ConnectionProviderError . unauthorized) , subscriptionItem)
166
+ expectedCompletionFailureError = APIError . operationError ( " " , " " , ConnectionProviderError . unauthorized)
167
+ await waitForSubscriptionExpectations ( )
168
+ }
169
+
170
+ func testConnectionErrorWithConnectionProviderConnectionError( ) async throws {
171
+ await receivedCompletionSuccess. setShouldTrigger ( false )
172
+ await receivedCompletionFailure. setShouldTrigger ( true )
173
+ await receivedStateValueConnecting. setShouldTrigger ( true )
174
+ await receivedStateValueConnected. setShouldTrigger ( false )
175
+ await receivedStateValueDisconnected. setShouldTrigger ( false )
176
+
177
+ await receivedDataValueSuccess. setShouldTrigger ( false )
178
+ await receivedDataValueError. setShouldTrigger ( false )
179
+
180
+ try await subscribe ( )
181
+ await waitForExpectations ( [ onSubscribeInvoked] , timeout: 0.05 )
182
+
183
+ subscriptionEventHandler ( . connection( . connecting) , subscriptionItem)
184
+ subscriptionEventHandler ( . failed( ConnectionProviderError . connection) , subscriptionItem)
185
+ expectedCompletionFailureError = APIError . networkError ( " " , nil , URLError ( . networkConnectionLost) )
128
186
await waitForSubscriptionExpectations ( )
129
187
}
130
188
@@ -270,8 +328,53 @@ class GraphQLSubscribeTasksTests: OperationTestBase {
270
328
271
329
await self . receivedCompletionSuccess. fulfill ( )
272
330
} catch {
331
+ if let apiError = error as? APIError ,
332
+ let expectedError = expectedCompletionFailureError {
333
+ XCTAssertEqual ( apiError, expectedError)
334
+ }
335
+
273
336
await self . receivedCompletionFailure. fulfill ( )
274
337
}
275
338
}
276
339
}
277
340
}
341
+
342
+ extension APIError : Equatable {
343
+ public static func == ( lhs: APIError , rhs: APIError ) -> Bool {
344
+ switch ( lhs, rhs) {
345
+ case ( . unknown, . unknown) ,
346
+ ( . invalidConfiguration, . invalidConfiguration) ,
347
+ ( . httpStatusError, . httpStatusError) ,
348
+ ( . pluginError, . pluginError) :
349
+ return true
350
+ case ( . operationError( _, _, let lhs) , . operationError( _, _, let rhs) ) :
351
+ if let lhs = lhs as? ConnectionProviderError , let rhs = rhs as? ConnectionProviderError {
352
+ switch ( lhs, rhs) {
353
+ case ( . connection, . connection) ,
354
+ ( . jsonParse, . jsonParse) ,
355
+ ( . limitExceeded, . limitExceeded) ,
356
+ ( . subscription, . subscription) ,
357
+ ( . unauthorized, . unauthorized) ,
358
+ ( . unknown, . unknown) :
359
+ return true
360
+ default :
361
+ return false
362
+ }
363
+ } else if lhs == nil && rhs == nil {
364
+ return true
365
+ } else {
366
+ return false
367
+ }
368
+ case ( . networkError( _, _, let lhs) , . networkError( _, _, let rhs) ) :
369
+ if let lhs = lhs as? URLError , let rhs = rhs as? URLError {
370
+ return lhs. code == rhs. code
371
+ } else if lhs == nil && rhs == nil {
372
+ return true
373
+ } else {
374
+ return false
375
+ }
376
+ default :
377
+ return false
378
+ }
379
+ }
380
+ }
0 commit comments