-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from ishkawa/feature/fix-custom-nsurlsessiond…
…elegate Fix subclassing NSURLSessionAdapter
- Loading branch information
Showing
4 changed files
with
68 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
Tests/APIKit/SessionAdapterType/NSURLSessionAdapterSubclassTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import Foundation | ||
import XCTest | ||
import OHHTTPStubs | ||
import APIKit | ||
|
||
class NSURLSessionAdapterSubclassTests: XCTestCase { | ||
class SessionAdapter: NSURLSessionAdapter { | ||
var functionCallFlags = [String: Bool]() | ||
|
||
override func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError connectionError: NSError?) { | ||
functionCallFlags[(#function)] = true | ||
super.URLSession(session, task: task, didCompleteWithError: connectionError) | ||
} | ||
|
||
override func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) { | ||
functionCallFlags[(#function)] = true | ||
super.URLSession(session, dataTask: dataTask, didReceiveData: data) | ||
} | ||
} | ||
|
||
var adapter: SessionAdapter! | ||
var session: Session! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
||
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() | ||
adapter = SessionAdapter(configuration: configuration) | ||
session = Session(adapter: adapter) | ||
} | ||
|
||
override func tearDown() { | ||
OHHTTPStubs.removeAllStubs() | ||
super.tearDown() | ||
} | ||
|
||
func testDelegateMethodCall() { | ||
let data = try! NSJSONSerialization.dataWithJSONObject([:], options: []) | ||
|
||
OHHTTPStubs.stubRequestsPassingTest({ request in | ||
return true | ||
}, withStubResponse: { request in | ||
return OHHTTPStubsResponse(data: data, statusCode: 200, headers: nil) | ||
}) | ||
|
||
let expectation = expectationWithDescription("wait for response") | ||
let request = TestRequest() | ||
|
||
session.sendRequest(request) { result in | ||
if case .Failure = result { | ||
XCTFail() | ||
} | ||
|
||
expectation.fulfill() | ||
} | ||
|
||
waitForExpectationsWithTimeout(10.0, handler: nil) | ||
|
||
XCTAssertEqual(adapter.functionCallFlags["URLSession(_:task:didCompleteWithError:)"], true) | ||
XCTAssertEqual(adapter.functionCallFlags["URLSession(_:dataTask:didReceiveData:)"], true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters