@@ -33,6 +33,8 @@ class GraphQLSubscribeTests: OperationTestBase {
33
33
var subscriptionItem : SubscriptionItem !
34
34
var subscriptionEventHandler : SubscriptionEventHandler !
35
35
36
+ var expectedCompletionFailureError : APIError ?
37
+
36
38
override func setUpWithError( ) throws {
37
39
try super. setUpWithError ( )
38
40
@@ -122,7 +124,23 @@ class GraphQLSubscribeTests: OperationTestBase {
122
124
/// - The value handler is not invoked with with a data value
123
125
/// - The value handler is invoked with a disconnection message
124
126
/// - The completion handler is invoked with an error termination
125
- func testConnectionError( ) throws {
127
+ func testConnectionErrorWithLimitExceeded( ) throws {
128
+ receivedCompletionFinish. shouldTrigger = false
129
+ receivedCompletionFailure. shouldTrigger = true
130
+ receivedConnected. shouldTrigger = false
131
+ receivedDisconnected. shouldTrigger = false
132
+ receivedSubscriptionEventData. shouldTrigger = false
133
+ receivedSubscriptionEventError. shouldTrigger = false
134
+
135
+ subscribe ( )
136
+ wait ( for: [ onSubscribeInvoked] , timeout: 0.05 )
137
+
138
+ subscriptionEventHandler ( . failed( ConnectionProviderError . limitExceeded ( nil ) ) , subscriptionItem)
139
+ expectedCompletionFailureError = APIError . operationError ( " " , " " , ConnectionProviderError . limitExceeded ( nil ) )
140
+ waitForExpectations ( timeout: 0.05 )
141
+ }
142
+
143
+ func testConnectionErrorWithSubscriptionError( ) throws {
126
144
receivedCompletionFinish. shouldTrigger = false
127
145
receivedCompletionFailure. shouldTrigger = true
128
146
receivedConnected. shouldTrigger = false
@@ -134,8 +152,42 @@ class GraphQLSubscribeTests: OperationTestBase {
134
152
wait ( for: [ onSubscribeInvoked] , timeout: 0.05 )
135
153
136
154
subscriptionEventHandler ( . connection( . connecting) , subscriptionItem)
137
- subscriptionEventHandler ( . failed( " Error " ) , subscriptionItem)
155
+ subscriptionEventHandler ( . failed( ConnectionProviderError . subscription ( " " , nil ) ) , subscriptionItem)
156
+ expectedCompletionFailureError = APIError . operationError ( " " , " " , ConnectionProviderError . subscription ( " " , nil ) )
157
+ waitForExpectations ( timeout: 0.05 )
158
+ }
159
+
160
+ func testConnectionErrorWithConnectionUnauthorizedError( ) throws {
161
+ receivedCompletionFinish. shouldTrigger = false
162
+ receivedCompletionFailure. shouldTrigger = true
163
+ receivedConnected. shouldTrigger = false
164
+ receivedDisconnected. shouldTrigger = false
165
+ receivedSubscriptionEventData. shouldTrigger = false
166
+ receivedSubscriptionEventError. shouldTrigger = false
138
167
168
+ subscribe ( )
169
+ wait ( for: [ onSubscribeInvoked] , timeout: 0.05 )
170
+
171
+ subscriptionEventHandler ( . connection( . connecting) , subscriptionItem)
172
+ subscriptionEventHandler ( . failed( ConnectionProviderError . unauthorized) , subscriptionItem)
173
+ expectedCompletionFailureError = APIError . operationError ( " " , " " , ConnectionProviderError . unauthorized)
174
+ waitForExpectations ( timeout: 0.05 )
175
+ }
176
+
177
+ func testConnectionErrorWithConnectionProviderConnectionError( ) throws {
178
+ receivedCompletionFinish. shouldTrigger = false
179
+ receivedCompletionFailure. shouldTrigger = true
180
+ receivedConnected. shouldTrigger = false
181
+ receivedDisconnected. shouldTrigger = false
182
+ receivedSubscriptionEventData. shouldTrigger = false
183
+ receivedSubscriptionEventError. shouldTrigger = false
184
+
185
+ subscribe ( )
186
+ wait ( for: [ onSubscribeInvoked] , timeout: 0.05 )
187
+
188
+ subscriptionEventHandler ( . connection( . connecting) , subscriptionItem)
189
+ subscriptionEventHandler ( . failed( ConnectionProviderError . connection) , subscriptionItem)
190
+ expectedCompletionFailureError = APIError . networkError ( " " , nil , URLError ( . networkConnectionLost) )
139
191
waitForExpectations ( timeout: 0.05 )
140
192
}
141
193
@@ -286,7 +338,11 @@ class GraphQLSubscribeTests: OperationTestBase {
286
338
}
287
339
} , completionListener: { result in
288
340
switch result {
289
- case . failure:
341
+ case . failure( let error) :
342
+ if let apiError = error as? APIError ,
343
+ let expectedError = self . expectedCompletionFailureError {
344
+ XCTAssertEqual ( apiError, expectedError)
345
+ }
290
346
self . receivedCompletionFailure. fulfill ( )
291
347
case . success:
292
348
self . receivedCompletionFinish. fulfill ( )
@@ -296,3 +352,43 @@ class GraphQLSubscribeTests: OperationTestBase {
296
352
return operation
297
353
}
298
354
}
355
+
356
+ extension APIError : Equatable {
357
+ public static func == ( lhs: APIError , rhs: APIError ) -> Bool {
358
+ switch ( lhs, rhs) {
359
+ case ( . unknown, . unknown) ,
360
+ ( . invalidConfiguration, . invalidConfiguration) ,
361
+ ( . httpStatusError, . httpStatusError) ,
362
+ ( . pluginError, . pluginError) :
363
+ return true
364
+ case ( . operationError( _, _, let lhs) , . operationError( _, _, let rhs) ) :
365
+ if let lhs = lhs as? ConnectionProviderError , let rhs = rhs as? ConnectionProviderError {
366
+ switch ( lhs, rhs) {
367
+ case ( . connection, . connection) ,
368
+ ( . jsonParse, . jsonParse) ,
369
+ ( . limitExceeded, . limitExceeded) ,
370
+ ( . subscription, . subscription) ,
371
+ ( . unauthorized, . unauthorized) ,
372
+ ( . unknown, . unknown) :
373
+ return true
374
+ default :
375
+ return false
376
+ }
377
+ } else if lhs == nil && rhs == nil {
378
+ return true
379
+ } else {
380
+ return false
381
+ }
382
+ case ( . networkError( _, _, let lhs) , . networkError( _, _, let rhs) ) :
383
+ if let lhs = lhs as? URLError , let rhs = rhs as? URLError {
384
+ return lhs. code == rhs. code
385
+ } else if lhs == nil && rhs == nil {
386
+ return true
387
+ } else {
388
+ return false
389
+ }
390
+ default :
391
+ return false
392
+ }
393
+ }
394
+ }
0 commit comments