Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit 79bea7f

Browse files
committed
Add tests to URLSessionTaskOperation
1 parent 849db3c commit 79bea7f

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

PSOperations.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
FD39C29E1B8B5F090092008B /* PSOperationsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D9B436D1B6A942C008F7F18 /* PSOperationsTests.swift */; };
4343
FDB1BBBE1B8D67CB00FB9D7A /* NSLock+Operations.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDB1BBBD1B8D67CB00FB9D7A /* NSLock+Operations.swift */; settings = {ASSET_TAGS = (); }; };
4444
FDB1E8F21BBC2B08006BC157 /* OperationConditionResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDB1E8F11BBC2B08006BC157 /* OperationConditionResultTests.swift */; settings = {ASSET_TAGS = (); }; };
45+
FDC6C5291BBCDDC9008FB96E /* URLSessionTaskOperationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDC6C5281BBCDDC9008FB96E /* URLSessionTaskOperationTests.swift */; settings = {ASSET_TAGS = (); }; };
4546
/* End PBXBuildFile section */
4647

4748
/* Begin PBXContainerItemProxy section */
@@ -93,6 +94,7 @@
9394
1D9B436D1B6A942C008F7F18 /* PSOperationsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PSOperationsTests.swift; sourceTree = "<group>"; };
9495
FDB1BBBD1B8D67CB00FB9D7A /* NSLock+Operations.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSLock+Operations.swift"; sourceTree = "<group>"; };
9596
FDB1E8F11BBC2B08006BC157 /* OperationConditionResultTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OperationConditionResultTests.swift; sourceTree = "<group>"; };
97+
FDC6C5281BBCDDC9008FB96E /* URLSessionTaskOperationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLSessionTaskOperationTests.swift; sourceTree = "<group>"; };
9698
/* End PBXFileReference section */
9799

98100
/* Begin PBXFrameworksBuildPhase section */
@@ -157,6 +159,7 @@
157159
1D9B431D1B6A92ED008F7F18 /* PSOperationsTests */ = {
158160
isa = PBXGroup;
159161
children = (
162+
FDC6C5281BBCDDC9008FB96E /* URLSessionTaskOperationTests.swift */,
160163
FDB1E8F11BBC2B08006BC157 /* OperationConditionResultTests.swift */,
161164
1D9B436D1B6A942C008F7F18 /* PSOperationsTests.swift */,
162165
1D9B431E1B6A92ED008F7F18 /* Supporting Files */,
@@ -387,6 +390,7 @@
387390
files = (
388391
FDB1E8F21BBC2B08006BC157 /* OperationConditionResultTests.swift in Sources */,
389392
FD39C29E1B8B5F090092008B /* PSOperationsTests.swift in Sources */,
393+
FDC6C5291BBCDDC9008FB96E /* URLSessionTaskOperationTests.swift in Sources */,
390394
);
391395
runOnlyForDeploymentPostprocessing = 0;
392396
};
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
 (0)