Skip to content

Commit

Permalink
Explicitly check failure points during testing
Browse files Browse the repository at this point in the history
This also catches and fixes the test failure on master involving a file
that isn't being created because the directory didn't exist.
  • Loading branch information
timshadel committed Aug 12, 2016
1 parent b8c3cbe commit 7cc9710
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DVR/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class Session: NSURLSession {
}

public override func uploadTaskWithRequest(request: NSURLRequest, fromFile fileURL: NSURL, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Void) -> NSURLSessionUploadTask {
let data = NSData(contentsOfURL: fileURL)!
let data = NSData(contentsOfURL: fileURL)
return addUploadTask(request, fromData: data, completionHandler: completionHandler)
}

Expand Down
25 changes: 20 additions & 5 deletions DVR/Tests/SessionUploadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,35 @@ class SessionUploadTests: XCTestCase {
return request
}()
let multipartBoundary = "---------------------------3klfenalksjflkjoi9auf89eshajsnl3kjnwal".UTF8Data()
lazy var testFile: NSURL = {
return NSBundle(forClass: self.dynamicType).URLForResource("testfile", withExtension: "txt")!
lazy var testFile: NSURL? = {
return NSBundle(forClass: self.dynamicType).URLForResource("testfile", withExtension: "txt")
}()

func testUploadFile() {
let session = Session(cassetteName: "upload-file")
session.recordingEnabled = false
let expectation = expectationWithDescription("Network")

let data = encodeMultipartBody(NSData(contentsOfURL: testFile)!, parameters: [:])
guard let testFile = testFile else { XCTFail("Missing test file URL"); return }
guard let fileData = NSData(contentsOfURL: testFile) else { XCTFail("Missing body data"); return }
let data = encodeMultipartBody(fileData, parameters: [:])
let file = writeDataToFile(data, fileName: "upload-file")

session.uploadTaskWithRequest(request, fromFile: file) { data, response, error in
if let error = error {
XCTFail("Error uploading file: \(error)")
return
}
guard let data = data else { XCTFail("Missing request data"); return }

do {
let JSON = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String: AnyObject]
let JSON = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: AnyObject]
XCTAssertEqual("test file\n", (JSON?["form"] as? [String: AnyObject])?["file"] as? String)
} catch {
XCTFail("Failed to read JSON.")
}

let HTTPResponse = response as! NSHTTPURLResponse
guard let HTTPResponse = response as? NSHTTPURLResponse else { XCTFail("Bad HTTP response"); return }
XCTAssertEqual(200, HTTPResponse.statusCode)

expectation.fulfill()
Expand All @@ -46,6 +54,7 @@ class SessionUploadTests: XCTestCase {
session.recordingEnabled = false
let expectation = expectationWithDescription("Network")

guard let testFile = testFile else { XCTFail("Missing testfile URL"); return }
let data = encodeMultipartBody(NSData(contentsOfURL: testFile)!, parameters: [:])

session.uploadTaskWithRequest(request, fromData: data) { data, response, error in
Expand Down Expand Up @@ -87,6 +96,12 @@ class SessionUploadTests: XCTestCase {
func writeDataToFile(data: NSData, fileName: String) -> NSURL {
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let documentsURL = NSURL(fileURLWithPath: documentsPath, isDirectory: true)

do {
try NSFileManager.defaultManager().createDirectoryAtURL(documentsURL, withIntermediateDirectories: true, attributes: nil)
} catch {
XCTFail("Failed to create documents directory \(documentsURL). Error \(error)")
}

let url = documentsURL.URLByAppendingPathComponent(fileName + ".tmp")

Expand Down

0 comments on commit 7cc9710

Please sign in to comment.