🤡 Handle requestSynchronousDataWithURLString in mock

This commit is contained in:
Ben Chatelain 2019-01-05 10:53:03 -07:00
parent 1fb28d6b6b
commit b0d5d2c4b8
6 changed files with 25 additions and 13 deletions

View file

@ -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")

View file

@ -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

View file

@ -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<String, AnyObject> {
return jsonResult

View file

@ -363,6 +363,8 @@
isa = PBXGroup;
children = (
B576FE0B21E116590016B39D /* NetworkManagerTests.swift */,
B576FDFB21E10A610016B39D /* TestURLSessionConfiguration.swift */,
B576FDFD21E10B660016B39D /* TestURLSessionDelegate.swift */,
);
path = Network;
sourceTree = "<group>";
@ -525,8 +527,6 @@
B594B12C21D584E800F3AC59 /* Mocks */,
B576FE0A21E116470016B39D /* Network */,
B594B13121D5876200F3AC59 /* ResultPredicates.swift */,
B576FDFB21E10A610016B39D /* TestURLSessionConfiguration.swift */,
B576FDFD21E10B660016B39D /* TestURLSessionDelegate.swift */,
);
path = MasKitTests;
sourceTree = "<group>";