diff --git a/DVR.xcodeproj/project.pbxproj b/DVR.xcodeproj/project.pbxproj index d80fe53..d9c1529 100644 --- a/DVR.xcodeproj/project.pbxproj +++ b/DVR.xcodeproj/project.pbxproj @@ -41,8 +41,6 @@ B19D62651CB1860400E16D11 /* SessionUploadTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = B19D62641CB1860400E16D11 /* SessionUploadTask.swift */; }; B19D62681CB19AA000E16D11 /* testfile.txt in Resources */ = {isa = PBXBuildFile; fileRef = B19D62661CB18EDB00E16D11 /* testfile.txt */; }; B19D62691CB19AA200E16D11 /* testfile.txt in Resources */ = {isa = PBXBuildFile; fileRef = B19D62661CB18EDB00E16D11 /* testfile.txt */; }; - B19D626E1CB1A0DD00E16D11 /* SessionUploadTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B19D626D1CB1A0DD00E16D11 /* SessionUploadTests.swift */; }; - B19D626F1CB1A0DD00E16D11 /* SessionUploadTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B19D626D1CB1A0DD00E16D11 /* SessionUploadTests.swift */; }; B19D62711CB1A27700E16D11 /* upload-data.json in Resources */ = {isa = PBXBuildFile; fileRef = B19D62701CB1A27700E16D11 /* upload-data.json */; }; B19D62721CB1A27700E16D11 /* upload-data.json in Resources */ = {isa = PBXBuildFile; fileRef = B19D62701CB1A27700E16D11 /* upload-data.json */; }; B19D62771CB1A42600E16D11 /* upload-file.json in Resources */ = {isa = PBXBuildFile; fileRef = B19D62761CB1A42600E16D11 /* upload-file.json */; }; @@ -89,7 +87,6 @@ 36BDDB881B6716AB00878665 /* json-example.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "json-example.json"; sourceTree = ""; }; B19D62641CB1860400E16D11 /* SessionUploadTask.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SessionUploadTask.swift; sourceTree = ""; }; B19D62661CB18EDB00E16D11 /* testfile.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = testfile.txt; sourceTree = ""; }; - B19D626D1CB1A0DD00E16D11 /* SessionUploadTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SessionUploadTests.swift; sourceTree = ""; }; B19D62701CB1A27700E16D11 /* upload-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "upload-data.json"; sourceTree = ""; }; B19D62761CB1A42600E16D11 /* upload-file.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "upload-file.json"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -180,7 +177,6 @@ 3647AFCB1B33689000EF10D4 /* SessionTests.swift */, 3647AFCF1B33693F00EF10D4 /* Fixtures */, 3647AFCC1B33689000EF10D4 /* Info.plist */, - B19D626D1CB1A0DD00E16D11 /* SessionUploadTests.swift */, ); name = Tests; path = DVR/Tests; @@ -407,7 +403,6 @@ buildActionMask = 2147483647; files = ( 3647AFCD1B33689000EF10D4 /* SessionTests.swift in Sources */, - B19D626E1CB1A0DD00E16D11 /* SessionUploadTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -432,7 +427,6 @@ buildActionMask = 2147483647; files = ( 3690A0A21B33AA9E00731222 /* SessionTests.swift in Sources */, - B19D626F1CB1A0DD00E16D11 /* SessionUploadTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/DVR/Tests/SessionUploadTests.swift b/DVR/Tests/SessionUploadTests.swift deleted file mode 100644 index 99b841c..0000000 --- a/DVR/Tests/SessionUploadTests.swift +++ /dev/null @@ -1,113 +0,0 @@ -import XCTest -@testable import DVR - -class SessionUploadTests: XCTestCase { - - lazy var request: NSURLRequest = { - let request = NSMutableURLRequest(URL: NSURL(string: "https://httpbin.org/post")!) - request.HTTPMethod = "POST" - - let contentType = "multipart/form-data; boundary=\(self.multipartBoundary)" - request.addValue(contentType, forHTTPHeaderField: "Content-Type") - return request - }() - let multipartBoundary = "---------------------------3klfenalksjflkjoi9auf89eshajsnl3kjnwal".UTF8Data() - 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: [:]) - let file = writeDataToFile(data, fileName: "upload-file") - - session.uploadTaskWithRequest(request, fromFile: file) { data, response, error in - do { - 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 - XCTAssertEqual(200, HTTPResponse.statusCode) - - expectation.fulfill() - }.resume() - - waitForExpectationsWithTimeout(4, handler: nil) - } - - func testUploadData() { - let session = Session(cassetteName: "upload-data") - session.recordingEnabled = false - let expectation = expectationWithDescription("Network") - - let data = encodeMultipartBody(NSData(contentsOfURL: testFile)!, parameters: [:]) - - session.uploadTaskWithRequest(request, fromData: data) { data, response, error in - do { - 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 - XCTAssertEqual(200, HTTPResponse.statusCode) - - expectation.fulfill() - }.resume() - - waitForExpectationsWithTimeout(4, handler: nil) - } - - // MARK: Helpers - - func encodeMultipartBody(data: NSData, parameters: [String: AnyObject]) -> NSData { - let delim = "--\(multipartBoundary)\r\n".UTF8Data() - - let body = NSMutableData() - body += delim - for (key, value) in parameters { - body += "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)\r\n".UTF8Data() - body += delim - } - - body += "Content-Disposition: form-data; name=\"file\"\r\n\r\n".UTF8Data() - body += data - body += "\r\n--\(multipartBoundary)--\r\n".UTF8Data() - - return body - } - - func writeDataToFile(data: NSData, fileName: String) -> NSURL { - let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] - let documentsURL = NSURL(fileURLWithPath: documentsPath, isDirectory: true) - - guard let url = documentsURL.URLByAppendingPathComponent(fileName + ".tmp") else { - XCTFail("Failed to write to file") - fatalError() - } - - data.writeToURL(url, atomically: true) - return url - } - -} - -// MARK: - Helpers - -extension String { - func UTF8Data() -> NSData { - return dataUsingEncoding(NSUTF8StringEncoding)! - } -} - - -public func +=(lhs: NSMutableData, rhs: NSData) { - lhs.appendData(rhs) -}