diff --git a/MasKit/Network/URLSession+Synchronous.swift b/MasKit/Network/URLSession+Synchronous.swift index 2a7ad43..b72bc78 100644 --- a/MasKit/Network/URLSession+Synchronous.swift +++ b/MasKit/Network/URLSession+Synchronous.swift @@ -31,21 +31,21 @@ public extension URLSession { } /// Return data synchronous from specified endpoint - public func requestSynchronousDataWithURLString(_ requestString: String) -> Data? { - guard let url = URL(string:requestString) else {return nil} + @objc public func requestSynchronousDataWithURLString(_ requestString: String) -> Data? { + guard let url = URL(string:requestString) else { return nil } let request = URLRequest(url: url) return requestSynchronousData(request) } /// Return JSON synchronous from URL request public func requestSynchronousJSON(_ request: URLRequest) -> Any? { - guard let data = requestSynchronousData(request) else {return nil} + guard let data = requestSynchronousData(request) else { return nil } return try! JSONSerialization.jsonObject(with: data, options: []) } /// Return JSON synchronous from specified endpoint @objc public func requestSynchronousJSONWithURLString(_ requestString: String) -> Any? { - guard let url = URL(string: requestString) else {return nil} + guard let url = URL(string: requestString) else { return nil } var request = URLRequest(url:url) request.httpMethod = "GET" request.addValue("application/json", forHTTPHeaderField: "Content-Type") diff --git a/MasKitTests/MasStoreSearchSpec.swift b/MasKitTests/MasStoreSearchSpec.swift index 6b9fb9f..7cc902f 100644 --- a/MasKitTests/MasStoreSearchSpec.swift +++ b/MasKitTests/MasStoreSearchSpec.swift @@ -18,11 +18,6 @@ class MasStoreSearchSpec: QuickSpec { let storeSearch = MasStoreSearch(urlSession: urlSession) describe("store search") { it("can find slack") { - // FIXME: Doesn't work offline -// 2019-01-05 08:37:33.764724-0700 xctest[76864:1854467] TIC TCP Conn Failed [1:0x100c90f90]: 1:50 Err(50) -// 2019-01-05 08:37:33.774861-0700 xctest[76864:1854467] Task <8D1421BF-F9A3-4716-BCB0-803438C7E3E8>.<1> HTTP load failed (error code: -1009 [1:50]) -// 2019-01-05 08:37:33.774983-0700 xctest[76864:1854467] Task <8D1421BF-F9A3-4716-BCB0-803438C7E3E8>.<1> finished with error - code: -1009 -// Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." let result = try! storeSearch.lookup(app: appId.description) expect(result).toNot(beNil()) expect(result!.trackId) == appId diff --git a/MasKitTests/Mocks/MockURLSession.swift b/MasKitTests/Mocks/MockURLSession.swift index d07362e..6b1ee86 100644 --- a/MasKitTests/Mocks/MockURLSession.swift +++ b/MasKitTests/Mocks/MockURLSession.swift @@ -52,16 +52,33 @@ class MockURLSession: URLSession { } } - /// Override which returns JSON contents from a file. + /// Override which returns Data from a file. /// /// - Parameter requestString: Ignored URL string /// - Returns: Contents of responseFile - @objc override func requestSynchronousJSONWithURLString(_ requestString: String) -> Any? { + @objc override func requestSynchronousDataWithURLString(_ requestString: String) -> Data? { guard let fileURL = Bundle.jsonResponse(fileName: responseFile) else { fatalError("Unable to load file \(responseFile)") } do { let data = try Data(contentsOf: fileURL, options: .mappedIfSafe) + return data + } catch { + print("Error opening file: \(error)") + } + + return nil + } + + /// Override which returns JSON contents from a file. + /// + /// - Parameter requestString: Ignored URL string + /// - Returns: Parsed contents of responseFile + @objc override func requestSynchronousJSONWithURLString(_ requestString: String) -> Any? { + guard let data = requestSynchronousDataWithURLString(requestString) + else { return nil } + + do { let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) if let jsonResult = jsonResult as? Dictionary { return jsonResult diff --git a/MasKitTests/TestURLSessionConfiguration.swift b/MasKitTests/Network/TestURLSessionConfiguration.swift similarity index 100% rename from MasKitTests/TestURLSessionConfiguration.swift rename to MasKitTests/Network/TestURLSessionConfiguration.swift diff --git a/MasKitTests/TestURLSessionDelegate.swift b/MasKitTests/Network/TestURLSessionDelegate.swift similarity index 100% rename from MasKitTests/TestURLSessionDelegate.swift rename to MasKitTests/Network/TestURLSessionDelegate.swift diff --git a/mas-cli.xcodeproj/project.pbxproj b/mas-cli.xcodeproj/project.pbxproj index a51fc7c..337a042 100644 --- a/mas-cli.xcodeproj/project.pbxproj +++ b/mas-cli.xcodeproj/project.pbxproj @@ -363,6 +363,8 @@ isa = PBXGroup; children = ( B576FE0B21E116590016B39D /* NetworkManagerTests.swift */, + B576FDFB21E10A610016B39D /* TestURLSessionConfiguration.swift */, + B576FDFD21E10B660016B39D /* TestURLSessionDelegate.swift */, ); path = Network; sourceTree = ""; @@ -525,8 +527,6 @@ B594B12C21D584E800F3AC59 /* Mocks */, B576FE0A21E116470016B39D /* Network */, B594B13121D5876200F3AC59 /* ResultPredicates.swift */, - B576FDFB21E10A610016B39D /* TestURLSessionConfiguration.swift */, - B576FDFD21E10B660016B39D /* TestURLSessionDelegate.swift */, ); path = MasKitTests; sourceTree = "";