Explorar el Código

Refactored URLStringCovertible to URLConvertible and removed protocol property.

Christian Noon hace 9 años
padre
commit
e48a78e68d

+ 15 - 16
Source/AFError.swift

@@ -27,7 +27,7 @@ import Foundation
 /// `AFError` is the error type returned by Alamofire. It encompasses a few different types of errors, each with
 /// their own associated reasons.
 ///
-/// - invalidURLString:            Returned when a URL string is missing or fails to create a valid `URL`.
+/// - invalidURL:                  Returned when a `URLConvertible` type fails to create a valid `URL`.
 /// - parameterEncodingFailed:     Returned when a parameter encoding object throws an error during the encoding process.
 /// - multipartEncodingFailed:     Returned when some step in the multipart encoding process fails.
 /// - responseValidationFailed:    Returned when a `validate()` call fails.
@@ -125,7 +125,7 @@ public enum AFError: Error {
         case propertyListSerializationFailed(error: Error)
     }
 
-    case invalidURLString(urlString: URLStringConvertible)
+    case invalidURL(url: URLConvertible)
     case parameterEncodingFailed(reason: ParameterEncodingFailureReason)
     case multipartEncodingFailed(reason: MultipartEncodingFailureReason)
     case responseValidationFailed(reason: ResponseValidationFailureReason)
@@ -135,10 +135,9 @@ public enum AFError: Error {
 // MARK: - Error Booleans
 
 extension AFError {
-    /// Returns whether the AFError is an invalid URL string error. When `true`, the `urlString` property will
-    /// contain the associated value.
-    public var isInvalidURLStringError: Bool {
-        if case .invalidURLString = self { return true }
+    /// Returns whether the AFError is an invalid URL error.
+    public var isInvalidURLError: Bool {
+        if case .invalidURL = self { return true }
         return false
     }
 
@@ -174,21 +173,21 @@ extension AFError {
 // MARK: - Convenience Properties
 
 extension AFError {
-    /// The `URL` associated with the error.
-    public var url: URL? {
+    /// The `URLConvertible` associated with the error.
+    public var urlConvertible: URLConvertible? {
         switch self {
-        case .multipartEncodingFailed(let reason):
-            return reason.url
+        case .invalidURL(let url):
+            return url
         default:
             return nil
         }
     }
 
-    /// The `URLStringConvertible` associated with the error.
-    public var urlString: URLStringConvertible? {
+    /// The `URL` associated with the error.
+    public var url: URL? {
         switch self {
-        case .invalidURLString(let urlString):
-            return urlString
+        case .multipartEncodingFailed(let reason):
+            return reason.url
         default:
             return nil
         }
@@ -340,8 +339,8 @@ extension AFError.ResponseSerializationFailureReason {
 extension AFError: LocalizedError {
     public var errorDescription: String? {
         switch self {
-        case .invalidURLString(let urlString):
-            return "URL string is not valid: \(urlString)"
+        case .invalidURL(let url):
+            return "URL is not valid: \(url)"
         case .parameterEncodingFailed(let reason):
             return reason.localizedDescription
         case .multipartEncodingFailed(let reason):

+ 69 - 86
Source/Alamofire.swift

@@ -24,60 +24,43 @@
 
 import Foundation
 
-/// Types adopting the `URLStringConvertible` protocol can be used to construct URL strings, which are then used to
-/// construct URL requests.
-public protocol URLStringConvertible {
-    /// A URL string that conforms to RFC 2396.
+/// Types adopting the `URLConvertible` protocol can be used to construct URLs, which are then used to construct 
+/// URL requests.
+public protocol URLConvertible {
+    /// Returns a URL that conforms to RFC 2396 or throws an `Error`.
     ///
-    /// Methods accepting a `URLStringConvertible` type parameter parse it according to RFCs 1738 and 1808.
+    /// - throws: An `Error` if the type cannot be converted to a `URL`.
     ///
-    /// See https://tools.ietf.org/html/rfc2396
-    /// See https://tools.ietf.org/html/rfc1738
-    /// See https://tools.ietf.org/html/rfc1808
-    var urlString: String? { get }
+    /// - returns: A URL or throws an `Error`.
+    func asURL() throws -> URL
+}
 
-    /// Returns a URL string that conforms to RFC 2396 or throws if an `Error` was encoutered.
-    ///
-    /// Methods accepting a `URLStringConvertible` type parameter parse it according to RFCs 1738 and 1808.
+extension String: URLConvertible {
+    /// Returns a URL if `self` represents a valid URL string that conforms to RFC 2396 or throws an `AFError`.
     ///
-    /// - throws: An `Error` if underlying url string is `nil`.
+    /// - throws: An `AFError.invalidURL` if `self` is not a valid URL string.
     ///
-    /// - returns: A URL string.
-    func asURLString() throws -> String
-}
-
-extension URLStringConvertible {
-    public var urlString: String? { return try? asURLString() }
+    /// - returns: A URL or throws an `AFError`.
+    public func asURL() throws -> URL {
+        guard let url = URL(string: self) else { throw AFError.invalidURL(url: self) }
+        return url
+    }
 }
 
-extension String: URLStringConvertible {
-    /// Returns a URL string that conforms to RFC 2396.
-    public func asURLString() throws -> String { return self }
+extension URL: URLConvertible {
+    /// Returns self.
+    public func asURL() throws -> URL { return self }
 }
 
-extension URL: URLStringConvertible {
-    /// Returns a URL string that conforms to RFC 2396.
-    public func asURLString() throws -> String { return absoluteString }
-
-    /// Creates a URL with the specified URL string if possible, otherwise throws an `Error`.
+extension URLComponents: URLConvertible {
+    /// Returns a URL if `url` is not nil, otherise throws an `Error`.
     ///
-    /// - parameter urlString: The URL string convertible to create the URL with.
+    /// - throws: An `AFError.invalidURL` if `url` is `nil`.
     ///
-    /// - throws: An `AFError.invalidURLString` if invalid.
-    ///
-    /// - returns: A URL if created successfully, otherwise throws an `AFError`.
-    public init(urlString: URLStringConvertible) throws {
-        let urlString = try urlString.asURLString()
-        guard let url = URL(string: urlString) else { throw AFError.invalidURLString(urlString: urlString) }
-        self = url
-    }
-}
-
-extension URLComponents: URLStringConvertible {
-    /// Returns a URL string that conforms to RFC 2396.
-    public func asURLString() throws -> String {
-        guard let urlString = url?.urlString else { throw AFError.invalidURLString(urlString: self) }
-        return urlString
+    /// - returns: A URL or throws an `AFError`.
+    public func asURL() throws -> URL {
+        guard let url = url else { throw AFError.invalidURL(url: self) }
+        return url
     }
 }
 
@@ -111,13 +94,13 @@ extension URLRequest: URLRequestConvertible {
 extension URLRequest {
     /// Creates an instance with the specified `method`, `urlString` and `headers`.
     ///
-    /// - parameter urlString: The URL string.
-    /// - parameter method:    The HTTP method.
-    /// - parameter headers:   The HTTP headers. `nil` by default.
+    /// - parameter url:     The URL.
+    /// - parameter method:  The HTTP method.
+    /// - parameter headers: The HTTP headers. `nil` by default.
     ///
     /// - returns: The new `URLRequest` instance.
-    public init(urlString: URLStringConvertible, method: HTTPMethod, headers: HTTPHeaders? = nil) throws {
-        let url = try URL(urlString: urlString)
+    public init(url: URLConvertible, method: HTTPMethod, headers: HTTPHeaders? = nil) throws {
+        let url = try url.asURL()
 
         self.init(url: url)
 
@@ -138,10 +121,10 @@ extension URLRequest {
 
 // MARK: - Data Request
 
-/// Creates a `DataRequest` using the default `SessionManager` to retrieve the contents of a URL based on the
-/// specified `urlString`, `method`, `parameters`, `encoding` and `headers`.
+/// Creates a `DataRequest` using the default `SessionManager` to retrieve the contents of the specified `url`,
+/// `method`, `parameters`, `encoding` and `headers`.
 ///
-/// - parameter urlString:  The URL string.
+/// - parameter url:        The URL.
 /// - parameter method:     The HTTP method. `.get` by default.
 /// - parameter parameters: The parameters. `nil` by default.
 /// - parameter encoding:   The parameter encoding. `URLEncoding.default` by default.
@@ -150,7 +133,7 @@ extension URLRequest {
 /// - returns: The created `DataRequest`.
 @discardableResult
 public func request(
-    _ urlString: URLStringConvertible,
+    _ url: URLConvertible,
     method: HTTPMethod = .get,
     parameters: Parameters? = nil,
     encoding: ParameterEncoding = URLEncoding.default,
@@ -158,7 +141,7 @@ public func request(
     -> DataRequest
 {
     return SessionManager.default.request(
-        urlString,
+        url,
         method: method,
         parameters: parameters,
         encoding: encoding,
@@ -181,13 +164,13 @@ public func request(_ urlRequest: URLRequestConvertible) -> DataRequest {
 
 // MARK: URL Request
 
-/// Creates a `DownloadRequest` using the default `SessionManager` to retrieve the contents of a URL based on the
-/// specified `urlString`, `method`, `parameters`, `encoding`, `headers` and save them to the `destination`.
+/// Creates a `DownloadRequest` using the default `SessionManager` to retrieve the contents of the specified `url`,
+/// `method`, `parameters`, `encoding`, `headers` and save them to the `destination`.
 ///
 /// If `destination` is not specified, the contents will remain in the temporary location determined by the
 /// underlying URL session.
 ///
-/// - parameter urlString:   The URL string.
+/// - parameter url:         The URL.
 /// - parameter method:      The HTTP method. `.get` by default.
 /// - parameter parameters:  The parameters. `nil` by default.
 /// - parameter encoding:    The parameter encoding. `URLEncoding.default` by default.
@@ -197,7 +180,7 @@ public func request(_ urlRequest: URLRequestConvertible) -> DataRequest {
 /// - returns: The created `DownloadRequest`.
 @discardableResult
 public func download(
-    _ urlString: URLStringConvertible,
+    _ url: URLConvertible,
     method: HTTPMethod = .get,
     parameters: Parameters? = nil,
     encoding: ParameterEncoding = URLEncoding.default,
@@ -206,7 +189,7 @@ public func download(
     -> DownloadRequest
 {
     return SessionManager.default.download(
-        urlString,
+        url,
         method: method,
         parameters: parameters,
         encoding: encoding,
@@ -261,24 +244,24 @@ public func download(
 
 // MARK: File
 
-/// Creates an `UploadRequest` using the default `SessionManager` from the specified `method`, `urlString`
-/// and `headers` for uploading the `file`.
+/// Creates an `UploadRequest` using the default `SessionManager` from the specified `url`, `method` and `headers` 
+/// for uploading the `file`.
 ///
-/// - parameter file:      The file to upload.
-/// - parameter urlString: The URL string.
-/// - parameter method:    The HTTP method. `.post` by default.
-/// - parameter headers:   The HTTP headers. `nil` by default.
+/// - parameter file:    The file to upload.
+/// - parameter url:     The URL.
+/// - parameter method:  The HTTP method. `.post` by default.
+/// - parameter headers: The HTTP headers. `nil` by default.
 ///
 /// - returns: The created `UploadRequest`.
 @discardableResult
 public func upload(
     _ fileURL: URL,
-    to urlString: URLStringConvertible,
+    to url: URLConvertible,
     method: HTTPMethod = .post,
     headers: HTTPHeaders? = nil)
     -> UploadRequest
 {
-    return SessionManager.default.upload(fileURL, to: urlString, method: method, headers: headers)
+    return SessionManager.default.upload(fileURL, to: url, method: method, headers: headers)
 }
 
 /// Creates a `UploadRequest` using the default `SessionManager` from the specified `urlRequest` for
@@ -295,24 +278,24 @@ public func upload(_ fileURL: URL, with urlRequest: URLRequestConvertible) -> Up
 
 // MARK: Data
 
-/// Creates an `UploadRequest` using the default `SessionManager` from the specified `method`, `urlString`
-/// and `headers` for uploading the `data`.
+/// Creates an `UploadRequest` using the default `SessionManager` from the specified `url`, `method` and `headers` 
+/// for uploading the `data`.
 ///
-/// - parameter data:      The data to upload.
-/// - parameter urlString: The URL string.
-/// - parameter method:    The HTTP method. `.post` by default.
-/// - parameter headers:   The HTTP headers. `nil` by default.
+/// - parameter data:    The data to upload.
+/// - parameter url:     The URL.
+/// - parameter method:  The HTTP method. `.post` by default.
+/// - parameter headers: The HTTP headers. `nil` by default.
 ///
 /// - returns: The created `UploadRequest`.
 @discardableResult
 public func upload(
     _ data: Data,
-    to urlString: URLStringConvertible,
+    to url: URLConvertible,
     method: HTTPMethod = .post,
     headers: HTTPHeaders? = nil)
     -> UploadRequest
 {
-    return SessionManager.default.upload(data, to: urlString, method: method, headers: headers)
+    return SessionManager.default.upload(data, to: url, method: method, headers: headers)
 }
 
 /// Creates an `UploadRequest` using the default `SessionManager` from the specified `urlRequest` for
@@ -329,24 +312,24 @@ public func upload(_ data: Data, with urlRequest: URLRequestConvertible) -> Uplo
 
 // MARK: InputStream
 
-/// Creates an `UploadRequest` using the default `SessionManager` from the specified `method`, `urlString`
-/// and `headers` for uploading the `stream`.
+/// Creates an `UploadRequest` using the default `SessionManager` from the specified `url`, `method` and `headers` 
+/// for uploading the `stream`.
 ///
-/// - parameter stream:    The stream to upload.
-/// - parameter urlString: The URL string.
-/// - parameter method:    The HTTP method. `.post` by default.
-/// - parameter headers:   The HTTP headers. `nil` by default.
+/// - parameter stream:  The stream to upload.
+/// - parameter url:     The URL.
+/// - parameter method:  The HTTP method. `.post` by default.
+/// - parameter headers: The HTTP headers. `nil` by default.
 ///
 /// - returns: The created `UploadRequest`.
 @discardableResult
 public func upload(
     _ stream: InputStream,
-    to urlString: URLStringConvertible,
+    to url: URLConvertible,
     method: HTTPMethod = .post,
     headers: HTTPHeaders? = nil)
     -> UploadRequest
 {
-    return SessionManager.default.upload(stream, to: urlString, method: method, headers: headers)
+    return SessionManager.default.upload(stream, to: url, method: method, headers: headers)
 }
 
 /// Creates an `UploadRequest` using the default `SessionManager` from the specified `urlRequest` for
@@ -364,7 +347,7 @@ public func upload(_ stream: InputStream, with urlRequest: URLRequestConvertible
 // MARK: MultipartFormData
 
 /// Encodes `multipartFormData` using `encodingMemoryThreshold` with the default `SessionManager` and calls
-/// `encodingCompletion` with new `UploadRequest` using the `method`, `urlString` and `headers`.
+/// `encodingCompletion` with new `UploadRequest` using the `url`, `method` and `headers`.
 ///
 /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cummulative
 /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
@@ -382,14 +365,14 @@ public func upload(_ stream: InputStream, with urlRequest: URLRequestConvertible
 /// - parameter multipartFormData:       The closure used to append body parts to the `MultipartFormData`.
 /// - parameter encodingMemoryThreshold: The encoding memory threshold in bytes.
 ///                                      `multipartFormDataEncodingMemoryThreshold` by default.
-/// - parameter urlString:               The URL string.
+/// - parameter url:                     The URL.
 /// - parameter method:                  The HTTP method. `.post` by default.
 /// - parameter headers:                 The HTTP headers. `nil` by default.
 /// - parameter encodingCompletion:      The closure called when the `MultipartFormData` encoding is complete.
 public func upload(
     multipartFormData: @escaping (MultipartFormData) -> Void,
     usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold,
-    to urlString: URLStringConvertible,
+    to url: URLConvertible,
     method: HTTPMethod = .post,
     headers: HTTPHeaders? = nil,
     encodingCompletion: ((SessionManager.MultipartFormDataEncodingResult) -> Void)?)
@@ -397,7 +380,7 @@ public func upload(
     return SessionManager.default.upload(
         multipartFormData: multipartFormData,
         usingThreshold: encodingMemoryThreshold,
-        to: urlString,
+        to: url,
         method: method,
         headers: headers,
         encodingCompletion: encodingCompletion

+ 35 - 35
Source/SessionManager.swift

@@ -212,10 +212,10 @@ open class SessionManager {
 
     // MARK: - Data Request
 
-    /// Creates a `DataRequest` to retrieve the contents of a URL based on the specified `urlString`, `method`,
-    /// `parameters`, `encoding` and `headers`.
+    /// Creates a `DataRequest` to retrieve the contents of the specified `url`, `method`, `parameters`, `encoding`
+    /// and `headers`.
     ///
-    /// - parameter urlString:  The URL string.
+    /// - parameter url:        The URL.
     /// - parameter method:     The HTTP method. `.get` by default.
     /// - parameter parameters: The parameters. `nil` by default.
     /// - parameter encoding:   The parameter encoding. `URLEncoding.default` by default.
@@ -224,7 +224,7 @@ open class SessionManager {
     /// - returns: The created `DataRequest`.
     @discardableResult
     open func request(
-        _ urlString: URLStringConvertible,
+        _ url: URLConvertible,
         method: HTTPMethod = .get,
         parameters: Parameters? = nil,
         encoding: ParameterEncoding = URLEncoding.default,
@@ -232,7 +232,7 @@ open class SessionManager {
         -> DataRequest
     {
         do {
-            let urlRequest = try URLRequest(urlString: urlString, method: method, headers: headers)
+            let urlRequest = try URLRequest(url: url, method: method, headers: headers)
             let encodedURLRequest = try encoding.encode(urlRequest, with: parameters)
             return request(encodedURLRequest)
         } catch {
@@ -277,15 +277,15 @@ open class SessionManager {
 
     // MARK: URL Request
 
-    /// Creates a `DownloadRequest` to retrieve the contents of a URL based on the specified `urlString`, `method`,
-    /// `parameters`, `encoding`, `headers` and save them to the `destination`.
+    /// Creates a `DownloadRequest` to retrieve the contents the specified `url`, `method`, `parameters`, `encoding`,
+    /// `headers` and save them to the `destination`.
     ///
     /// If `destination` is not specified, the contents will remain in the temporary location determined by the
     /// underlying URL session.
     ///
     /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
     ///
-    /// - parameter urlString:   The URL string.
+    /// - parameter url:         The URL.
     /// - parameter method:      The HTTP method. `.get` by default.
     /// - parameter parameters:  The parameters. `nil` by default.
     /// - parameter encoding:    The parameter encoding. `URLEncoding.default` by default.
@@ -295,7 +295,7 @@ open class SessionManager {
     /// - returns: The created `DownloadRequest`.
     @discardableResult
     open func download(
-        _ urlString: URLStringConvertible,
+        _ url: URLConvertible,
         method: HTTPMethod = .get,
         parameters: Parameters? = nil,
         encoding: ParameterEncoding = URLEncoding.default,
@@ -304,7 +304,7 @@ open class SessionManager {
         -> DownloadRequest
     {
         do {
-            let urlRequest = try URLRequest(urlString: urlString, method: method, headers: headers)
+            let urlRequest = try URLRequest(url: url, method: method, headers: headers)
             let encodedURLRequest = try encoding.encode(urlRequest, with: parameters)
             return download(encodedURLRequest, to: destination)
         } catch {
@@ -396,26 +396,26 @@ open class SessionManager {
 
     // MARK: File
 
-    /// Creates an `UploadRequest` from the specified `method`, `urlString` and `headers` for uploading the `file`.
+    /// Creates an `UploadRequest` from the specified `url`, `method` and `headers` for uploading the `file`.
     ///
     /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
     ///
-    /// - parameter file:      The file to upload.
-    /// - parameter urlString: The URL string.
-    /// - parameter method:    The HTTP method. `.post` by default.
-    /// - parameter headers:   The HTTP headers. `nil` by default.
+    /// - parameter file:    The file to upload.
+    /// - parameter url:     The URL.
+    /// - parameter method:  The HTTP method. `.post` by default.
+    /// - parameter headers: The HTTP headers. `nil` by default.
     ///
     /// - returns: The created `UploadRequest`.
     @discardableResult
     open func upload(
         _ fileURL: URL,
-        to urlString: URLStringConvertible,
+        to url: URLConvertible,
         method: HTTPMethod = .post,
         headers: HTTPHeaders? = nil)
         -> UploadRequest
     {
         do {
-            let urlRequest = try URLRequest(urlString: urlString, method: method, headers: headers)
+            let urlRequest = try URLRequest(url: url, method: method, headers: headers)
             return upload(fileURL, with: urlRequest)
         } catch {
             return upload(failedWith: error)
@@ -442,26 +442,26 @@ open class SessionManager {
 
     // MARK: Data
 
-    /// Creates an `UploadRequest` from the specified `method`, `urlString` and `headers` for uploading the `data`.
+    /// Creates an `UploadRequest` from the specified `url`, `method` and `headers` for uploading the `data`.
     ///
     /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
     ///
-    /// - parameter data:      The data to upload.
-    /// - parameter urlString: The URL string.
-    /// - parameter method:    The HTTP method. `.post` by default.
-    /// - parameter headers:   The HTTP headers. `nil` by default.
+    /// - parameter data:    The data to upload.
+    /// - parameter url:     The URL.
+    /// - parameter method:  The HTTP method. `.post` by default.
+    /// - parameter headers: The HTTP headers. `nil` by default.
     ///
     /// - returns: The created `UploadRequest`.
     @discardableResult
     open func upload(
         _ data: Data,
-        to urlString: URLStringConvertible,
+        to url: URLConvertible,
         method: HTTPMethod = .post,
         headers: HTTPHeaders? = nil)
         -> UploadRequest
     {
         do {
-            let urlRequest = try URLRequest(urlString: urlString, method: method, headers: headers)
+            let urlRequest = try URLRequest(url: url, method: method, headers: headers)
             return upload(data, with: urlRequest)
         } catch {
             return upload(failedWith: error)
@@ -488,26 +488,26 @@ open class SessionManager {
 
     // MARK: InputStream
 
-    /// Creates an `UploadRequest` from the specified `method`, `urlString` and `headers` for uploading the `stream`.
+    /// Creates an `UploadRequest` from the specified `url`, `method` and `headers` for uploading the `stream`.
     ///
     /// If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
     ///
-    /// - parameter stream:    The stream to upload.
-    /// - parameter urlString: The URL string.
-    /// - parameter method:    The HTTP method. `.post` by default.
-    /// - parameter headers:   The HTTP headers. `nil` by default.
+    /// - parameter stream:  The stream to upload.
+    /// - parameter url:     The URL.
+    /// - parameter method:  The HTTP method. `.post` by default.
+    /// - parameter headers: The HTTP headers. `nil` by default.
     ///
     /// - returns: The created `UploadRequest`.
     @discardableResult
     open func upload(
         _ stream: InputStream,
-        to urlString: URLStringConvertible,
+        to url: URLConvertible,
         method: HTTPMethod = .post,
         headers: HTTPHeaders? = nil)
         -> UploadRequest
     {
         do {
-            let urlRequest = try URLRequest(urlString: urlString, method: method, headers: headers)
+            let urlRequest = try URLRequest(url: url, method: method, headers: headers)
             return upload(stream, with: urlRequest)
         } catch {
             return upload(failedWith: error)
@@ -535,7 +535,7 @@ open class SessionManager {
     // MARK: MultipartFormData
 
     /// Encodes `multipartFormData` using `encodingMemoryThreshold` and calls `encodingCompletion` with new
-    /// `UploadRequest` using the `method`, `urlString` and `headers`.
+    /// `UploadRequest` using the `url`, `method` and `headers`.
     ///
     /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cummulative
     /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
@@ -555,20 +555,20 @@ open class SessionManager {
     /// - parameter multipartFormData:       The closure used to append body parts to the `MultipartFormData`.
     /// - parameter encodingMemoryThreshold: The encoding memory threshold in bytes.
     ///                                      `multipartFormDataEncodingMemoryThreshold` by default.
-    /// - parameter urlString:               The URL string.
+    /// - parameter url:                     The URL.
     /// - parameter method:                  The HTTP method. `.post` by default.
     /// - parameter headers:                 The HTTP headers. `nil` by default.
     /// - parameter encodingCompletion:      The closure called when the `MultipartFormData` encoding is complete.
     open func upload(
         multipartFormData: @escaping (MultipartFormData) -> Void,
         usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold,
-        to urlString: URLStringConvertible,
+        to url: URLConvertible,
         method: HTTPMethod = .post,
         headers: HTTPHeaders? = nil,
         encodingCompletion: ((MultipartFormDataEncodingResult) -> Void)?)
     {
         do {
-            let urlRequest = try URLRequest(urlString: urlString, method: method, headers: headers)
+            let urlRequest = try URLRequest(url: url, method: method, headers: headers)
 
             return upload(
                 multipartFormData: multipartFormData,

+ 2 - 2
Tests/DownloadTests.swift

@@ -40,7 +40,7 @@ class DownloadInitializationTestCase: BaseTestCase {
         // Then
         XCTAssertNotNil(request.request)
         XCTAssertEqual(request.request?.httpMethod, "GET")
-        XCTAssertEqual(request.request?.url?.urlString, urlString)
+        XCTAssertEqual(request.request?.url?.absoluteString, urlString)
         XCTAssertNil(request.response)
     }
 
@@ -55,7 +55,7 @@ class DownloadInitializationTestCase: BaseTestCase {
         // Then
         XCTAssertNotNil(request.request)
         XCTAssertEqual(request.request?.httpMethod, "GET")
-        XCTAssertEqual(request.request?.url?.urlString, urlString)
+        XCTAssertEqual(request.request?.url?.absoluteString, urlString)
         XCTAssertEqual(request.request?.value(forHTTPHeaderField: "Authorization"), "123456")
         XCTAssertNil(request.response)
     }

+ 3 - 3
Tests/RequestTests.swift

@@ -37,7 +37,7 @@ class RequestInitializationTestCase: BaseTestCase {
         // Then
         XCTAssertNotNil(request.request)
         XCTAssertEqual(request.request?.httpMethod, "GET")
-        XCTAssertEqual(request.request?.url?.urlString, urlString)
+        XCTAssertEqual(request.request?.url?.absoluteString, urlString)
         XCTAssertNil(request.response)
     }
 
@@ -51,7 +51,7 @@ class RequestInitializationTestCase: BaseTestCase {
         // Then
         XCTAssertNotNil(request.request)
         XCTAssertEqual(request.request?.httpMethod, "GET")
-        XCTAssertNotEqual(request.request?.url?.urlString, urlString)
+        XCTAssertNotEqual(request.request?.url?.absoluteString, urlString)
         XCTAssertEqual(request.request?.url?.query, "foo=bar")
         XCTAssertNil(request.response)
     }
@@ -67,7 +67,7 @@ class RequestInitializationTestCase: BaseTestCase {
         // Then
         XCTAssertNotNil(request.request)
         XCTAssertEqual(request.request?.httpMethod, "GET")
-        XCTAssertNotEqual(request.request?.url?.urlString, urlString)
+        XCTAssertNotEqual(request.request?.url?.absoluteString, urlString)
         XCTAssertEqual(request.request?.url?.query, "foo=bar")
         XCTAssertEqual(request.request?.value(forHTTPHeaderField: "Authorization"), "123456")
         XCTAssertNil(request.response)

+ 8 - 8
Tests/SessionDelegateTests.swift

@@ -139,7 +139,7 @@ class SessionDelegateTestCase: BaseTestCase {
         XCTAssertNotNil(response?.data)
         XCTAssertNil(response?.error)
 
-        XCTAssertEqual(response?.response?.url?.urlString, redirectURLString)
+        XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
         XCTAssertEqual(response?.response?.statusCode, 200)
     }
 
@@ -167,7 +167,7 @@ class SessionDelegateTestCase: BaseTestCase {
         XCTAssertNotNil(response?.data)
         XCTAssertNil(response?.error)
 
-        XCTAssertEqual(response?.response?.url?.urlString, redirectURLString)
+        XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
         XCTAssertEqual(response?.response?.statusCode, 200)
     }
 
@@ -202,7 +202,7 @@ class SessionDelegateTestCase: BaseTestCase {
         XCTAssertNotNil(response?.data)
         XCTAssertNil(response?.error)
 
-        XCTAssertEqual(response?.response?.url?.urlString, redirectURLString)
+        XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
         XCTAssertEqual(response?.response?.statusCode, 200)
     }
 
@@ -237,7 +237,7 @@ class SessionDelegateTestCase: BaseTestCase {
         XCTAssertNotNil(response?.data)
         XCTAssertNil(response?.error)
 
-        XCTAssertEqual(response?.response?.url?.urlString, redirectURLString)
+        XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
         XCTAssertEqual(response?.response?.statusCode, 200)
     }
 
@@ -272,7 +272,7 @@ class SessionDelegateTestCase: BaseTestCase {
         XCTAssertNotNil(response?.data)
         XCTAssertNil(response?.error)
 
-        XCTAssertEqual(response?.response?.url?.urlString, urlString)
+        XCTAssertEqual(response?.response?.url?.absoluteString, urlString)
         XCTAssertEqual(response?.response?.statusCode, 302)
     }
 
@@ -307,7 +307,7 @@ class SessionDelegateTestCase: BaseTestCase {
         XCTAssertNotNil(response?.data)
         XCTAssertNil(response?.error)
 
-        XCTAssertEqual(response?.response?.url?.urlString, urlString)
+        XCTAssertEqual(response?.response?.url?.absoluteString, urlString)
         XCTAssertEqual(response?.response?.statusCode, 302)
     }
 
@@ -351,7 +351,7 @@ class SessionDelegateTestCase: BaseTestCase {
         XCTAssertNotNil(response?.data)
         XCTAssertNil(response?.error)
 
-        XCTAssertEqual(response?.response?.url?.urlString, redirectURLString)
+        XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
         XCTAssertEqual(response?.response?.statusCode, 200)
     }
 
@@ -397,7 +397,7 @@ class SessionDelegateTestCase: BaseTestCase {
         XCTAssertNotNil(response?.data)
         XCTAssertNil(response?.error)
 
-        XCTAssertEqual(response?.response?.url?.urlString, redirectURLString)
+        XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
         XCTAssertEqual(response?.response?.statusCode, 200)
     }
 

+ 16 - 16
Tests/SessionManagerTests.swift

@@ -40,7 +40,7 @@ class SessionManagerTestCase: BaseTestCase {
         }
 
         func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
-            guard !throwsError else { throw AFError.invalidURLString(urlString: "") }
+            guard !throwsError else { throw AFError.invalidURL(url: "") }
 
             var urlRequest = urlRequest
             urlRequest.httpMethod = method.rawValue
@@ -56,7 +56,7 @@ class SessionManagerTestCase: BaseTestCase {
         var throwsErrorOnSecondAdapt = false
 
         func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
-            if throwsErrorOnSecondAdapt && adaptedCount == 1 { throw AFError.invalidURLString(urlString: "") }
+            if throwsErrorOnSecondAdapt && adaptedCount == 1 { throw AFError.invalidURL(url: "") }
 
             var urlRequest = urlRequest
 
@@ -320,8 +320,8 @@ class SessionManagerTestCase: BaseTestCase {
         XCTAssertNotNil(response?.error)
 
         if let error = response?.error as? AFError {
-            XCTAssertTrue(error.isInvalidURLStringError)
-            XCTAssertEqual(error.urlString?.urlString, "https://httpbin.org/get/äëïöü")
+            XCTAssertTrue(error.isInvalidURLError)
+            XCTAssertEqual(error.urlConvertible as? String, "https://httpbin.org/get/äëïöü")
         } else {
             XCTFail("error should not be nil")
         }
@@ -351,8 +351,8 @@ class SessionManagerTestCase: BaseTestCase {
         XCTAssertNotNil(response?.error)
 
         if let error = response?.error as? AFError {
-            XCTAssertTrue(error.isInvalidURLStringError)
-            XCTAssertEqual(error.urlString?.urlString, "https://httpbin.org/get/äëïöü")
+            XCTAssertTrue(error.isInvalidURLError)
+            XCTAssertEqual(error.urlConvertible as? String, "https://httpbin.org/get/äëïöü")
         } else {
             XCTFail("error should not be nil")
         }
@@ -381,8 +381,8 @@ class SessionManagerTestCase: BaseTestCase {
         XCTAssertNotNil(response?.error)
 
         if let error = response?.error as? AFError {
-            XCTAssertTrue(error.isInvalidURLStringError)
-            XCTAssertEqual(error.urlString?.urlString, "https://httpbin.org/get/äëïöü")
+            XCTAssertTrue(error.isInvalidURLError)
+            XCTAssertEqual(error.urlConvertible as? String, "https://httpbin.org/get/äëïöü")
         } else {
             XCTFail("error should not be nil")
         }
@@ -411,8 +411,8 @@ class SessionManagerTestCase: BaseTestCase {
         XCTAssertNotNil(response?.error)
 
         if let error = response?.error as? AFError {
-            XCTAssertTrue(error.isInvalidURLStringError)
-            XCTAssertEqual(error.urlString?.urlString, "https://httpbin.org/get/äëïöü")
+            XCTAssertTrue(error.isInvalidURLError)
+            XCTAssertEqual(error.urlConvertible as? String, "https://httpbin.org/get/äëïöü")
         } else {
             XCTFail("error should not be nil")
         }
@@ -441,8 +441,8 @@ class SessionManagerTestCase: BaseTestCase {
         XCTAssertNotNil(response?.error)
 
         if let error = response?.error as? AFError {
-            XCTAssertTrue(error.isInvalidURLStringError)
-            XCTAssertEqual(error.urlString?.urlString, "https://httpbin.org/get/äëïöü")
+            XCTAssertTrue(error.isInvalidURLError)
+            XCTAssertEqual(error.urlConvertible as? String, "https://httpbin.org/get/äëïöü")
         } else {
             XCTFail("error should not be nil")
         }
@@ -541,8 +541,8 @@ class SessionManagerTestCase: BaseTestCase {
 
         // Then
         if let error = request.delegate.error as? AFError {
-            XCTAssertTrue(error.isInvalidURLStringError)
-            XCTAssertEqual(error.urlString?.urlString, "")
+            XCTAssertTrue(error.isInvalidURLError)
+            XCTAssertEqual(error.urlConvertible as? String, "")
         } else {
             XCTFail("error should not be nil")
         }
@@ -633,8 +633,8 @@ class SessionManagerTestCase: BaseTestCase {
         XCTAssertEqual(response?.result.isSuccess, false)
 
         if let error = response?.result.error as? AFError {
-            XCTAssertTrue(error.isInvalidURLStringError)
-            XCTAssertEqual(error.urlString?.urlString, "")
+            XCTAssertTrue(error.isInvalidURLError)
+            XCTAssertEqual(error.urlConvertible as? String, "")
         } else {
             XCTFail("error should not be nil")
         }

+ 6 - 6
Tests/UploadTests.swift

@@ -38,7 +38,7 @@ class UploadFileInitializationTestCase: BaseTestCase {
         // Then
         XCTAssertNotNil(request.request, "request should not be nil")
         XCTAssertEqual(request.request?.httpMethod ?? "", "POST", "request HTTP method should be POST")
-        XCTAssertEqual(request.request?.url?.urlString, urlString, "request URL string should be equal")
+        XCTAssertEqual(request.request?.url?.absoluteString, urlString, "request URL string should be equal")
         XCTAssertNil(request.response, "response should be nil")
     }
 
@@ -54,7 +54,7 @@ class UploadFileInitializationTestCase: BaseTestCase {
         // Then
         XCTAssertNotNil(request.request, "request should not be nil")
         XCTAssertEqual(request.request?.httpMethod ?? "", "POST", "request HTTP method should be POST")
-        XCTAssertEqual(request.request?.url?.urlString, urlString, "request URL string should be equal")
+        XCTAssertEqual(request.request?.url?.absoluteString, urlString, "request URL string should be equal")
 
         let authorizationHeader = request.request?.value(forHTTPHeaderField: "Authorization") ?? ""
         XCTAssertEqual(authorizationHeader, "123456", "Authorization header is incorrect")
@@ -76,7 +76,7 @@ class UploadDataInitializationTestCase: BaseTestCase {
         // Then
         XCTAssertNotNil(request.request, "request should not be nil")
         XCTAssertEqual(request.request?.httpMethod ?? "", "POST", "request HTTP method should be POST")
-        XCTAssertEqual(request.request?.url?.urlString, urlString, "request URL string should be equal")
+        XCTAssertEqual(request.request?.url?.absoluteString, urlString, "request URL string should be equal")
         XCTAssertNil(request.response, "response should be nil")
     }
 
@@ -91,7 +91,7 @@ class UploadDataInitializationTestCase: BaseTestCase {
         // Then
         XCTAssertNotNil(request.request, "request should not be nil")
         XCTAssertEqual(request.request?.httpMethod ?? "", "POST", "request HTTP method should be POST")
-        XCTAssertEqual(request.request?.url?.urlString, urlString, "request URL string should be equal")
+        XCTAssertEqual(request.request?.url?.absoluteString, urlString, "request URL string should be equal")
 
         let authorizationHeader = request.request?.value(forHTTPHeaderField: "Authorization") ?? ""
         XCTAssertEqual(authorizationHeader, "123456", "Authorization header is incorrect")
@@ -115,7 +115,7 @@ class UploadStreamInitializationTestCase: BaseTestCase {
         // Then
         XCTAssertNotNil(request.request, "request should not be nil")
         XCTAssertEqual(request.request?.httpMethod ?? "", "POST", "request HTTP method should be POST")
-        XCTAssertEqual(request.request?.url?.urlString, urlString, "request URL string should be equal")
+        XCTAssertEqual(request.request?.url?.absoluteString, urlString, "request URL string should be equal")
         XCTAssertNil(request.response, "response should be nil")
     }
 
@@ -132,7 +132,7 @@ class UploadStreamInitializationTestCase: BaseTestCase {
         // Then
         XCTAssertNotNil(request.request, "request should not be nil")
         XCTAssertEqual(request.request?.httpMethod ?? "", "POST", "request HTTP method should be POST")
-        XCTAssertEqual(request.request?.url?.urlString, urlString, "request URL string should be equal")
+        XCTAssertEqual(request.request?.url?.absoluteString, urlString, "request URL string should be equal")
 
         let authorizationHeader = request.request?.value(forHTTPHeaderField: "Authorization") ?? ""
         XCTAssertEqual(authorizationHeader, "123456", "Authorization header is incorrect")