Browse Source

[PR #983] Added safeguards to the Request class's debugDescription property.

tokorom 10 years ago
parent
commit
08cf269586
2 changed files with 23 additions and 9 deletions
  1. 11 9
      Source/Request.swift
  2. 12 0
      Tests/RequestTests.swift

+ 11 - 9
Source/Request.swift

@@ -454,22 +454,24 @@ extension Request: CustomDebugStringConvertible {
     func cURLRepresentation() -> String {
         var components = ["$ curl -i"]
 
-        guard let request = self.request else {
+        guard let
+            request = self.request,
+            URL = request.URL,
+            host = URL.host
+        else {
             return "$ curl command could not be created"
         }
 
-        let URL = request.URL
-
         if let HTTPMethod = request.HTTPMethod where HTTPMethod != "GET" {
             components.append("-X \(HTTPMethod)")
         }
 
         if let credentialStorage = self.session.configuration.URLCredentialStorage {
             let protectionSpace = NSURLProtectionSpace(
-                host: URL!.host!,
-                port: URL!.port?.integerValue ?? 0,
-                `protocol`: URL!.scheme,
-                realm: URL!.host!,
+                host: host,
+                port: URL.port?.integerValue ?? 0,
+                `protocol`: URL.scheme,
+                realm: host,
                 authenticationMethod: NSURLAuthenticationMethodHTTPBasic
             )
 
@@ -487,7 +489,7 @@ extension Request: CustomDebugStringConvertible {
         if session.configuration.HTTPShouldSetCookies {
             if let
                 cookieStorage = session.configuration.HTTPCookieStorage,
-                cookies = cookieStorage.cookiesForURL(URL!) where !cookies.isEmpty
+                cookies = cookieStorage.cookiesForURL(URL) where !cookies.isEmpty
             {
                 let string = cookies.reduce("") { $0 + "\($1.name)=\($1.value ?? String());" }
                 components.append("-b \"\(string.substringToIndex(string.endIndex.predecessor()))\"")
@@ -524,7 +526,7 @@ extension Request: CustomDebugStringConvertible {
             components.append("-d \"\(escapedBody)\"")
         }
 
-        components.append("\"\(URL!.absoluteString)\"")
+        components.append("\"\(URL.absoluteString)\"")
 
         return components.joinWithSeparator(" \\\n\t")
     }

+ 12 - 0
Tests/RequestTests.swift

@@ -595,6 +595,18 @@ class RequestDebugDescriptionTestCase: BaseTestCase {
         XCTAssertTrue(cookieComponents.isEmpty, "command should not contain -b flag")
     }
 
+    func testThatRequestWithInvalidURLDebugDescription() {
+        // Given
+        let URLString = "invalid_url"
+
+        // When
+        let request = manager.request(.GET, URLString)
+        let debugDescription = request.debugDescription
+
+        // Then
+        XCTAssertNotNil(debugDescription, "debugDescription should not crash")
+    }
+
     // MARK: Test Helper Methods
 
     private func cURLCommandComponents(request: Request) -> [String] {