Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notify delegate when task finishes #46

Merged
merged 4 commits into from
Jun 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions DVR/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class Session: NSURLSession {
private var completedInteractions = [Interaction]()
private var completionBlock: (Void -> Void)?

override public var delegate: NSURLSessionDelegate? {
return backingSession.delegate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. 😎

}

// MARK: - Initializers

Expand Down Expand Up @@ -110,6 +113,14 @@ public class Session: NSURLSession {
if !recording && outstandingTasks.count == 0 {
finishRecording()
}

if let delegate = delegate as? NSURLSessionDataDelegate, task = task as? NSURLSessionDataTask, data = interaction.responseData {
delegate.URLSession?(self, dataTask: task, didReceiveData: data)
}

if let delegate = delegate as? NSURLSessionTaskDelegate {
delegate.URLSession?(self, task: task, didCompleteWithError: nil)
}
}


Expand Down
10 changes: 8 additions & 2 deletions DVR/SessionDataTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class SessionDataTask: NSURLSessionDataTask {
let request: NSURLRequest
let completion: Completion?
private let queue = dispatch_queue_create("com.venmo.DVR.sessionDataTaskQueue", nil)
private var interaction: Interaction?

override var response: NSURLResponse? {
return interaction?.response
}


// MARK: - Initializers
Expand All @@ -35,6 +40,7 @@ class SessionDataTask: NSURLSessionDataTask {

// Find interaction
if let interaction = session.cassette?.interactionForRequest(request) {
self.interaction = interaction
// Forward completion
if let completion = completion {
dispatch_async(queue) {
Expand Down Expand Up @@ -75,8 +81,8 @@ class SessionDataTask: NSURLSessionDataTask {
}

// Create interaction
let interaction = Interaction(request: this.request, response: response, responseData: data)
this.session.finishTask(this, interaction: interaction, playback: false)
this.interaction = Interaction(request: this.request, response: response, responseData: data)
this.session.finishTask(this, interaction: this.interaction!, playback: false)
}
task.resume()
}
Expand Down
54 changes: 54 additions & 0 deletions DVR/Tests/SessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,58 @@ class SessionTests: XCTestCase {

waitForExpectationsWithTimeout(1, handler: nil)
}

func testTaskDelegate() {
class Delegate: NSObject, NSURLSessionTaskDelegate {
let expectation: XCTestExpectation
var response: NSURLResponse?

init(expectation: XCTestExpectation) {
self.expectation = expectation
}

@objc private func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
response = task.response
expectation.fulfill()
}
}

let expectation = expectationWithDescription("didCompleteWithError")
let delegate = Delegate(expectation: expectation)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let backingSession = NSURLSession(configuration: config, delegate: delegate, delegateQueue: nil)
let session = Session(cassetteName: "example", backingSession: backingSession)
session.recordingEnabled = false

let task = session.dataTaskWithRequest(request)
task.resume()

waitForExpectationsWithTimeout(1, handler: nil)
}

func testDataDelegate() {
class Delegate: NSObject, NSURLSessionDataDelegate {
let expectation: XCTestExpectation

init(expectation: XCTestExpectation) {
self.expectation = expectation
}

@objc func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
expectation.fulfill()
}
}

let expectation = expectationWithDescription("didCompleteWithError")
let delegate = Delegate(expectation: expectation)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let backingSession = NSURLSession(configuration: config, delegate: delegate, delegateQueue: nil)
let session = Session(cassetteName: "example", backingSession: backingSession)
session.recordingEnabled = false

let task = session.dataTaskWithRequest(request)
task.resume()

waitForExpectationsWithTimeout(1, handler: nil)
}
}