diff --git a/DVR/Cassette.swift b/DVR/Cassette.swift index 23f5b55..ec04bc1 100644 --- a/DVR/Cassette.swift +++ b/DVR/Cassette.swift @@ -19,11 +19,31 @@ struct Cassette { // MARK: - Functions func interactionForRequest(request: NSURLRequest) -> Interaction? { + print("REQUEST") + print(request.HTTPMethod) + print(request.URL) + if let body = request.HTTPBody { + print(Interaction.encodeBody(body, headers: request.allHTTPHeaderFields)) + } else { + print("[empty body]") + } + print("") for interaction in interactions { let interactionRequest = interaction.request + print("INTERACTION") + print(interactionRequest.HTTPMethod) + print(interactionRequest.URL) + if let body = interactionRequest.HTTPBody { + print(Interaction.encodeBody(body, headers: interactionRequest.allHTTPHeaderFields)) + } else { + print("[empty body]") + } + print("") + // Note: We don't check headers right now if interactionRequest.HTTPMethod == request.HTTPMethod && interactionRequest.URL == request.URL && interactionRequest.hasHTTPBodyEqualToThatOfRequest(request) { + print("MATCHED!") return interaction } } diff --git a/DVR/Session.swift b/DVR/Session.swift index 5ec8029..0805e1a 100644 --- a/DVR/Session.swift +++ b/DVR/Session.swift @@ -66,7 +66,11 @@ public class Session: NSURLSession { } public override func uploadTaskWithRequest(request: NSURLRequest, fromFile fileURL: NSURL, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Void) -> NSURLSessionUploadTask { + print(fileURL) + print("File exists: \(NSFileManager.defaultManager().fileExistsAtPath(fileURL.path!))") let data = NSData(contentsOfURL: fileURL) + print("DATA-2") + print(data) return addUploadTask(request, fromData: data, completionHandler: completionHandler) } @@ -162,7 +166,12 @@ public class Session: NSURLSession { private func addUploadTask(request: NSURLRequest, fromData data: NSData?, completionHandler: SessionUploadTask.Completion? = nil) -> NSURLSessionUploadTask { var modifiedRequest = backingSession.configuration.HTTPAdditionalHeaders.map(request.requestByAppendingHeaders) ?? request - modifiedRequest = data.map(modifiedRequest.requestWithBody) ?? modifiedRequest + print("DATA-3") + print(data) + let mapped = data.map(modifiedRequest.requestWithBody) + print("MAPPED") + print(mapped) + modifiedRequest = mapped ?? modifiedRequest let task = SessionUploadTask(session: self, request: modifiedRequest, completion: completionHandler) addTask(task.dataTask) return task diff --git a/DVR/Tests/SessionUploadTests.swift b/DVR/Tests/SessionUploadTests.swift index 1c9ddb1..d39110a 100644 --- a/DVR/Tests/SessionUploadTests.swift +++ b/DVR/Tests/SessionUploadTests.swift @@ -25,6 +25,14 @@ class SessionUploadTests: XCTestCase { guard let fileData = NSData(contentsOfURL: testFile) else { XCTFail("Missing body data"); return } let data = encodeMultipartBody(fileData, parameters: [:]) let file = writeDataToFile(data, fileName: "upload-file") + print("DATA") + print(data) + print("FILE") + print(file) + print("File exists: \(NSFileManager.defaultManager().fileExistsAtPath(file.path!))") + let check = NSData(contentsOfURL: file) + print("DATA-1.2") + print(check) session.uploadTaskWithRequest(request, fromFile: file) { data, response, error in if let error = error { @@ -99,7 +107,9 @@ class SessionUploadTests: XCTestCase { let url = documentsURL.URLByAppendingPathComponent(fileName + ".tmp") - data.writeToURL(url, atomically: true) + if !data.writeToURL(url, atomically: true) { + XCTFail("Failed to write to \(url)") + } return url }