From 9fb8bdeaa21f60027a8060de04b2baaad9a604bb Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Thu, 20 Nov 2014 16:48:15 -0800 Subject: [PATCH 1/5] Fixing possible exception when force unwrapping optional header properties --- Source/Alamofire.swift | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Source/Alamofire.swift b/Source/Alamofire.swift index 07d0c5b82..ad8143f3a 100644 --- a/Source/Alamofire.swift +++ b/Source/Alamofire.swift @@ -1263,21 +1263,25 @@ extension Request: DebugPrintable { } } - for (field, value) in request.allHTTPHeaderFields! { - switch field { - case "Cookie": - continue - default: - components.append("-H \"\(field): \(value)\"") + if request.allHTTPHeaderFields != nil { + for (field, value) in request.allHTTPHeaderFields! { + switch field { + case "Cookie": + continue + default: + components.append("-H \"\(field): \(value)\"") + } } } - for (field, value) in session.configuration.HTTPAdditionalHeaders! { - switch field { - case "Cookie": - continue - default: - components.append("-H \"\(field): \(value)\"") + if session.configuration.HTTPAdditionalHeaders != nil { + for (field, value) in session.configuration.HTTPAdditionalHeaders! { + switch field { + case "Cookie": + continue + default: + components.append("-H \"\(field): \(value)\"") + } } } From fb5592e01b1af5d657fd17a39a39ad6895b2173e Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Thu, 20 Nov 2014 16:49:05 -0800 Subject: [PATCH 2/5] [Issue #226] Fixing optional cookie entry in cURL output --- Source/Alamofire.swift | 2 +- Tests/RequestTests.swift | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Source/Alamofire.swift b/Source/Alamofire.swift index ad8143f3a..161b7d7ba 100644 --- a/Source/Alamofire.swift +++ b/Source/Alamofire.swift @@ -1257,7 +1257,7 @@ extension Request: DebugPrintable { if let cookieStorage = session.configuration.HTTPCookieStorage { if let cookies = cookieStorage.cookiesForURL(URL) as? [NSHTTPCookie] { if !cookies.isEmpty { - let string = cookies.reduce(""){ $0 + "\($1.name)=\($1.value);" } + let string = cookies.reduce(""){ $0 + "\($1.name)=\($1.value ?? String());" } components.append("-b \"\(string.substringToIndex(string.endIndex.predecessor()))\"") } } diff --git a/Tests/RequestTests.swift b/Tests/RequestTests.swift index 9bf6ecc7a..0d9a7d7f4 100644 --- a/Tests/RequestTests.swift +++ b/Tests/RequestTests.swift @@ -127,4 +127,28 @@ class AlamofireRequestDebugDescriptionTestCase: XCTestCase { XCTAssert(request.debugDescription.rangeOfString("-d \"{\\\"foo\\\":\\\"bar\\\"}\"") != nil) XCTAssert(components.last! == "\"\(URL)\"", "URL component should be equal") } + + func testPOSTRequestWithCookieDebugDescription() { + let URL = "http://httpbin.org/post" + + let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() + + let properties = [ + NSHTTPCookieDomain: "httpbin.org", + NSHTTPCookiePath: "/post", + NSHTTPCookieName: "foo", + NSHTTPCookieValue: "bar", + ] + let cookie = NSHTTPCookie(properties: properties)! + configuration.HTTPCookieStorage?.setCookie(cookie) + + let manager = Alamofire.Manager(configuration: configuration) + let request = manager.request(.POST, URL) + let components = cURLCommandComponents(request) + + XCTAssert(components[0..<3] == ["$", "curl", "-i"], "components should be equal") + XCTAssert(components[3..<5] == ["-X", "POST"], "command should contain explicit -X flag") + XCTAssert(components[5..<7] == ["-b", "\"foo=bar\""], "command should contain -b flag") + XCTAssert(components.last! == "\"\(URL)\"", "URL component should be equal") + } } From 9016949899546c8758f9e17f98c65ae3fda88b3c Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Thu, 20 Nov 2014 16:49:30 -0800 Subject: [PATCH 3/5] Adding missing forced unwrap in test --- Tests/DownloadTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/DownloadTests.swift b/Tests/DownloadTests.swift index 4bde100ec..632294456 100644 --- a/Tests/DownloadTests.swift +++ b/Tests/DownloadTests.swift @@ -68,7 +68,7 @@ class AlamofireDownloadResponseTestCase: XCTestCase { XCTAssertEqual(filteredContents.count, 1, "should have one file in Documents") let file = filteredContents.first as NSURL - XCTAssertEqual(file.lastPathComponent, "\(numberOfLines)", "filename should be \(numberOfLines)") + XCTAssertEqual(file.lastPathComponent!, "\(numberOfLines)", "filename should be \(numberOfLines)") if let data = NSData(contentsOfURL: file) { XCTAssertGreaterThan(data.length, 0, "data length should be non-zero") From 69e626fc23c95257053cc897838778603990ea72 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Thu, 20 Nov 2014 16:49:46 -0800 Subject: [PATCH 4/5] Fixing optional textLabel property on cells --- Example/DetailViewController.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Example/DetailViewController.swift b/Example/DetailViewController.swift index 53a958b09..04a36487c 100644 --- a/Example/DetailViewController.swift +++ b/Example/DetailViewController.swift @@ -104,14 +104,14 @@ class DetailViewController: UITableViewController { let field = self.headers.keys.array.sorted(<)[indexPath.row] let value = self.headers[field] - cell.textLabel.text = field + cell.textLabel?.text = field cell.detailTextLabel!.text = value return cell case .Body: let cell = self.tableView.dequeueReusableCellWithIdentifier("Body") as UITableViewCell - cell.textLabel.text = self.body + cell.textLabel?.text = self.body return cell } From b5d6f581a8a01cb0e985f580dc36d87581f40d9b Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Sun, 23 Nov 2014 13:28:21 -0800 Subject: [PATCH 5/5] [Issue #230] Making response object example initializer failable --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 814a25989..b56b74fb9 100644 --- a/README.md +++ b/README.md @@ -498,7 +498,7 @@ Generics can be used to provide automatic, type-safe response object serializati ```swift @objc public protocol ResponseObjectSerializable { - init(response: NSHTTPURLResponse, representation: AnyObject) + init?(response: NSHTTPURLResponse, representation: AnyObject) } extension Alamofire.Request { @@ -514,7 +514,7 @@ extension Alamofire.Request { } return response(serializer: serializer, completionHandler: { (request, response, object, error) in - completionHandler(request, response, object as? T, error) + completionHandler(request, response, object, error) }) } } @@ -525,7 +525,7 @@ final class User: ResponseObjectSerializable { let username: String let name: String - required init(response: NSHTTPURLResponse, representation: AnyObject) { + required init?(response: NSHTTPURLResponse, representation: AnyObject) { self.username = response.URL!.lastPathComponent self.name = representation.valueForKeyPath("name") as String }