11import Apollo
22import ApolloAPI
3- import XCTest
43@testable import GraphQLAPIKit
4+ import XCTest
55
66// MARK: - MockURLProtocol
77
@@ -22,11 +22,11 @@ final class MockURLProtocol: URLProtocol {
2222 mockError = nil
2323 }
2424
25- override class func canInit( with request: URLRequest ) -> Bool {
25+ override static func canInit( with request: URLRequest ) -> Bool {
2626 true
2727 }
2828
29- override class func canonicalRequest( for request: URLRequest ) -> URLRequest {
29+ override static func canonicalRequest( for request: URLRequest ) -> URLRequest {
3030 request
3131 }
3232
@@ -44,12 +44,16 @@ final class MockURLProtocol: URLProtocol {
4444 statusCode: 200
4545 )
4646
47+ guard let url = request. url else {
48+ return
49+ }
50+
4751 let httpResponse = HTTPURLResponse (
48- url: request . url! ,
52+ url: url,
4953 statusCode: response. statusCode,
5054 httpVersion: " HTTP/1.1 " ,
5155 headerFields: [ " Content-Type " : " application/json " ]
52- ) !
56+ ) ! // swiftlint:disable:this force_unwrapping
5357
5458 client? . urlProtocol ( self , didReceive: httpResponse, cacheStoragePolicy: . notAllowed)
5559 client? . urlProtocol ( self , didLoad: response. data)
@@ -60,9 +64,9 @@ final class MockURLProtocol: URLProtocol {
6064
6165 /// A valid GraphQL response with minimal data
6266 private var validGraphQLResponse : Data {
63- """
67+ Data ( """
6468 { " data " : { " __typename " : " Query " }}
65- """ . data ( using : . utf8) !
69+ """ . utf8)
6670 }
6771}
6872
@@ -72,7 +76,9 @@ enum MockSchema: SchemaMetadata {
7276 static let configuration : any SchemaConfiguration . Type = MockSchemaConfiguration . self
7377
7478 static func objectType( forTypename typename: String ) -> Object ? {
75- if typename == " Query " { return MockQuery . Data. self. __parentType as? Object }
79+ if typename == " Query " {
80+ return MockQuery . Data. self. __parentType as? Object
81+ }
7682 return nil
7783 }
7884}
@@ -94,6 +100,7 @@ final class MockQuery: GraphQLQuery {
94100
95101 init ( ) { }
96102
103+ // swiftlint:disable nesting identifier_name
97104 struct MockQueryData : RootSelectionSet {
98105 typealias Schema = MockSchema
99106
@@ -106,6 +113,7 @@ final class MockQuery: GraphQLQuery {
106113 self . __data = _dataDict
107114 }
108115 }
116+ // swiftlint:enable nesting identifier_name
109117}
110118
111119// MARK: - Mock Request Headers
@@ -172,6 +180,7 @@ final class GraphQLAPIAdapterIntegrationTests: XCTestCase {
172180 ]
173181
174182 let adapter = GraphQLAPIAdapter (
183+ // swiftlint:disable:next force_unwrapping
175184 url: URL ( string: " https://api.example.com/graphql " ) !,
176185 urlSessionConfiguration: mockSessionConfiguration ( ) ,
177186 defaultHeaders: defaultHeaders,
@@ -207,10 +216,10 @@ final class GraphQLAPIAdapterIntegrationTests: XCTestCase {
207216 ] )
208217
209218 let adapter = GraphQLAPIAdapter (
219+ // swiftlint:disable:next force_unwrapping
210220 url: URL ( string: " https://api.example.com/graphql " ) !,
211221 urlSessionConfiguration: mockSessionConfiguration ( ) ,
212- defaultHeaders: [ : ] ,
213- networkObservers: observer
222+ networkObservers: [ observer]
214223 )
215224
216225 _ = adapter. fetch ( query: MockQuery ( ) , context: contextHeaders, queue: . main) { _ in
@@ -246,6 +255,7 @@ final class GraphQLAPIAdapterIntegrationTests: XCTestCase {
246255 ] )
247256
248257 let adapter = GraphQLAPIAdapter (
258+ // swiftlint:disable:next force_unwrapping
249259 url: URL ( string: " https://api.example.com/graphql " ) !,
250260 urlSessionConfiguration: mockSessionConfiguration ( ) ,
251261 defaultHeaders: defaultHeaders,
@@ -282,10 +292,13 @@ final class GraphQLAPIAdapterIntegrationTests: XCTestCase {
282292 let defaultHeaders = [ " X-Shared-Header " : " shared-value " ]
283293
284294 let adapter = GraphQLAPIAdapter (
295+ // swiftlint:disable:next force_unwrapping
285296 url: URL ( string: " https://api.example.com/graphql " ) !,
286297 urlSessionConfiguration: mockSessionConfiguration ( ) ,
287298 defaultHeaders: defaultHeaders,
288- networkObservers: observer1, observer2, observer3
299+ networkObservers: observer1,
300+ observer2,
301+ observer3
289302 )
290303
291304 _ = adapter. fetch ( query: MockQuery ( ) , context: nil , queue: . main) { _ in
@@ -319,10 +332,10 @@ final class GraphQLAPIAdapterIntegrationTests: XCTestCase {
319332 let observer = IntegrationMockObserver ( )
320333
321334 let adapter = GraphQLAPIAdapter (
335+ // swiftlint:disable:next force_unwrapping
322336 url: URL ( string: " https://api.example.com/graphql " ) !,
323337 urlSessionConfiguration: mockSessionConfiguration ( ) ,
324- defaultHeaders: [ : ] ,
325- networkObservers: observer
338+ networkObservers: [ observer]
326339 )
327340
328341 _ = adapter. fetch ( query: MockQuery ( ) , context: nil , queue: . main) { _ in
@@ -340,5 +353,4 @@ final class GraphQLAPIAdapterIntegrationTests: XCTestCase {
340353 XCTAssertEqual ( capturedRequest. value ( forHTTPHeaderField: " X-APOLLO-OPERATION-NAME " ) , " MockQuery " )
341354 XCTAssertNotNil ( capturedRequest. value ( forHTTPHeaderField: " Content-Type " ) )
342355 }
343-
344356}
0 commit comments