|
| 1 | +// |
| 2 | +// URLSessionTaskOperationTests.swift |
| 3 | +// PSOperations |
| 4 | +// |
| 5 | +// Created by Matt McMurry on 9/30/15. |
| 6 | +// Copyright © 2015 Pluralsight. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +@testable import PSOperations |
| 10 | +import XCTest |
| 11 | + |
| 12 | +public extension NSURLSession { |
| 13 | + |
| 14 | + struct SharedInstance { |
| 15 | + static var instance = NSURLSession.sharedSession() |
| 16 | + } |
| 17 | + |
| 18 | + public func setProtocolClasses(classes: [AnyClass]) { |
| 19 | + let sessionconfig = NSURLSession.PSSession.configuration |
| 20 | + sessionconfig.protocolClasses = classes |
| 21 | + SharedInstance.instance = NSURLSession(configuration: sessionconfig) |
| 22 | + } |
| 23 | + |
| 24 | + public static var PSSession: NSURLSession { |
| 25 | + return SharedInstance.instance |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class TestURLProtocol: NSURLProtocol { |
| 30 | + override class func canInitWithRequest(request: NSURLRequest) -> Bool { |
| 31 | + return true |
| 32 | + } |
| 33 | + |
| 34 | + override class func canonicalRequestForRequest(request: NSURLRequest) -> NSURLRequest { |
| 35 | + return request |
| 36 | + } |
| 37 | + |
| 38 | + func GETjson() -> [String: AnyObject] { |
| 39 | + return ["cool": "beans"] |
| 40 | + } |
| 41 | + |
| 42 | + override func startLoading() { |
| 43 | + let resp = NSHTTPURLResponse(URL: request.URL!, statusCode: 200, HTTPVersion: "HTTP/1.1", headerFields: ["Content-Type" : "application/json"]) |
| 44 | + |
| 45 | + client?.URLProtocol(self, didReceiveResponse: resp!, cacheStoragePolicy: .NotAllowed) |
| 46 | + client?.URLProtocol(self, didLoadData: try! NSJSONSerialization.dataWithJSONObject(GETjson(), options: [])) |
| 47 | + client?.URLProtocolDidFinishLoading(self) |
| 48 | + } |
| 49 | + |
| 50 | + override func stopLoading() { |
| 51 | + |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +class URLSessionTaskOperationTests: XCTestCase { |
| 56 | + |
| 57 | + override func setUp() { |
| 58 | + super.setUp() |
| 59 | + NSURLSession.PSSession.setProtocolClasses([TestURLProtocol.self]) |
| 60 | + } |
| 61 | + |
| 62 | + override func tearDown() { |
| 63 | + super.tearDown() |
| 64 | + NSURLSession.PSSession.setProtocolClasses([]) |
| 65 | + } |
| 66 | + |
| 67 | + func testSuccess() { |
| 68 | + |
| 69 | + let exp = expectationWithDescription("") |
| 70 | + |
| 71 | + let taskThing: NSURLSessionTask = NSURLSession.PSSession.dataTaskWithURL(NSURL(string: "http://winning")!) { |
| 72 | + data, response, error in |
| 73 | + XCTAssertNil(error) |
| 74 | + exp.fulfill() |
| 75 | + } |
| 76 | + |
| 77 | + let op = URLSessionTaskOperation(task: taskThing) |
| 78 | + OperationQueue().addOperation(op) |
| 79 | + |
| 80 | + waitForExpectationsWithTimeout(1.0) { |
| 81 | + error in |
| 82 | + |
| 83 | + XCTAssertTrue(op.finished) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + func testCancel() { |
| 88 | + |
| 89 | + let exp = expectationWithDescription("") |
| 90 | + |
| 91 | + let taskThing: NSURLSessionTask = NSURLSession.PSSession.dataTaskWithURL(NSURL(string: "http://winning")!) { |
| 92 | + data, response, error in |
| 93 | + XCTAssertNotNil(error) |
| 94 | + exp.fulfill() |
| 95 | + } |
| 96 | + |
| 97 | + let op = URLSessionTaskOperation(task: taskThing) |
| 98 | + let q = OperationQueue() |
| 99 | + q.suspended = true |
| 100 | + q.addOperation(op) |
| 101 | + op.cancel() |
| 102 | + q.suspended = false |
| 103 | + |
| 104 | + XCTAssertTrue(op.cancelled) |
| 105 | + |
| 106 | + waitForExpectationsWithTimeout(1.0, handler: nil) |
| 107 | + } |
| 108 | +} |
0 commit comments