Skip to content

Commit

Permalink
Rename httpHeaders properties to headers (Alamofire#2802)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnoon authored Apr 13, 2019
1 parent 2346c56 commit 9d1cec8
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 27 deletions.
6 changes: 3 additions & 3 deletions Source/HTTPHeaders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -413,22 +413,22 @@ extension Collection where Element == String {

extension URLRequest {
/// Returns `allHTTPHeaderFields` as `HTTPHeaders`.
public var httpHeaders: HTTPHeaders {
public var headers: HTTPHeaders {
get { return allHTTPHeaderFields.map(HTTPHeaders.init) ?? HTTPHeaders() }
set { allHTTPHeaderFields = newValue.dictionary }
}
}

extension HTTPURLResponse {
/// Returns `allHeaderFields` as `HTTPHeaders`.
public var httpHeaders: HTTPHeaders {
public var headers: HTTPHeaders {
return (allHeaderFields as? [String: String]).map(HTTPHeaders.init) ?? HTTPHeaders()
}
}

extension URLSessionConfiguration {
/// Returns `httpAdditionalHeaders` as `HTTPHeaders`.
public var httpHeaders: HTTPHeaders {
public var headers: HTTPHeaders {
get { return (httpAdditionalHeaders as? [String: String]).map(HTTPHeaders.init) ?? HTTPHeaders() }
set { httpAdditionalHeaders = newValue.dictionary }
}
Expand Down
8 changes: 4 additions & 4 deletions Source/ParameterEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ open class JSONParameterEncoder: ParameterEncoder {
do {
let data = try encoder.encode(parameters)
request.httpBody = data
if request.httpHeaders["Content-Type"] == nil {
request.httpHeaders.update(.contentType("application/json"))
if request.headers["Content-Type"] == nil {
request.headers.update(.contentType("application/json"))
}
} catch {
throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
Expand Down Expand Up @@ -175,8 +175,8 @@ open class URLEncodedFormParameterEncoder: ParameterEncoder {

request.url = newURL
} else {
if request.httpHeaders["Content-Type"] == nil {
request.httpHeaders.update(.contentType("application/x-www-form-urlencoded; charset=utf-8"))
if request.headers["Content-Type"] == nil {
request.headers.update(.contentType("application/x-www-form-urlencoded; charset=utf-8"))
}

request.httpBody = try AFResult<Data> { try encoder.encode(parameters) }
Expand Down
4 changes: 2 additions & 2 deletions Source/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ extension DataResponse: CustomStringConvertible, CustomDebugStringConvertible {
let requestDescription = request.map { "\($0.httpMethod!) \($0)" } ?? "nil"
let requestBody = request?.httpBody.map { String(decoding: $0, as: UTF8.self) } ?? "None"
let responseDescription = response.map { (response) in
let sortedHeaders = response.httpHeaders.sorted()
let sortedHeaders = response.headers.sorted()

return """
[Status Code]: \(response.statusCode)
Expand Down Expand Up @@ -278,7 +278,7 @@ extension DownloadResponse: CustomStringConvertible, CustomDebugStringConvertibl
let requestDescription = request.map { "\($0.httpMethod!) \($0)" } ?? "nil"
let requestBody = request?.httpBody.map { String(decoding: $0, as: UTF8.self) } ?? "None"
let responseDescription = response.map { (response) in
let sortedHeaders = response.httpHeaders.sorted()
let sortedHeaders = response.headers.sorted()

return """
[Status Code]: \(response.statusCode)
Expand Down
2 changes: 1 addition & 1 deletion Source/URLSessionConfiguration+Alamofire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension URLSessionConfiguration: AlamofireExtended { }
extension AlamofireExtension where ExtendedType: URLSessionConfiguration {
public static var `default`: URLSessionConfiguration {
let configuration = URLSessionConfiguration.default
configuration.httpHeaders = .default
configuration.headers = .default

return configuration
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/CacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class CacheTestCase: BaseTestCase {
manager = {
let configuration: URLSessionConfiguration = {
let configuration = URLSessionConfiguration.default
configuration.httpHeaders = HTTPHeaders.default
configuration.headers = HTTPHeaders.default
configuration.requestCachePolicy = .useProtocolCachePolicy
configuration.urlCache = urlCache

Expand Down
4 changes: 2 additions & 2 deletions Tests/HTTPBin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ extension URLRequest {
headers: HTTPHeaders = .init()) -> URLRequest {
var request = URLRequest(url: .makeHTTPBinURL(path: path))
request.httpMethod = method.rawValue
request.httpHeaders = headers
request.headers = headers

return request
}

static func make(url: URL = URL(string: "https://httpbin.org/get")!, method: HTTPMethod = .get, headers: HTTPHeaders = .init()) -> URLRequest {
var request = URLRequest(url:url)
request.method = method
request.httpHeaders = headers
request.headers = headers

return request
}
Expand Down
12 changes: 6 additions & 6 deletions Tests/ParameterEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ final class JSONParameterEncoderTests: BaseTestCase {
let newRequest = try encoder.encode(HTTPBinParameters.default, into: request)

// Then
XCTAssertEqual(newRequest.httpHeaders["Content-Type"], "application/json")
XCTAssertEqual(newRequest.headers["Content-Type"], "application/json")
XCTAssertEqual(newRequest.httpBody?.asString, "{\"property\":\"property\"}")
}

func testThatDataIsProperlyEncodedButContentTypeIsNotSetIfRequestAlreadyHasAContentType() throws {
// Given
let encoder = JSONParameterEncoder()
var request = URLRequest.makeHTTPBinRequest()
request.httpHeaders.update(.contentType("type"))
request.headers.update(.contentType("type"))

// When
let newRequest = try encoder.encode(HTTPBinParameters.default, into: request)

// Then
XCTAssertEqual(newRequest.httpHeaders["Content-Type"], "type")
XCTAssertEqual(newRequest.headers["Content-Type"], "type")
XCTAssertEqual(newRequest.httpBody?.asString, "{\"property\":\"property\"}")
}

Expand Down Expand Up @@ -133,21 +133,21 @@ final class URLEncodedFormParameterEncoderTests: BaseTestCase {
let newRequest = try encoder.encode(HTTPBinParameters.default, into: request)

// Then
XCTAssertEqual(newRequest.httpHeaders["Content-Type"], "application/x-www-form-urlencoded; charset=utf-8")
XCTAssertEqual(newRequest.headers["Content-Type"], "application/x-www-form-urlencoded; charset=utf-8")
XCTAssertEqual(newRequest.httpBody?.asString, "property=property")
}

func testThatQueryIsBodyEncodedButContentTypeIsNotSetWhenRequestAlreadyHasContentType() throws {
// Given
let encoder = URLEncodedFormParameterEncoder()
var request = URLRequest.makeHTTPBinRequest(method: .post)
request.httpHeaders.update(.contentType("type"))
request.headers.update(.contentType("type"))

// When
let newRequest = try encoder.encode(HTTPBinParameters.default, into: request)

// Then
XCTAssertEqual(newRequest.httpHeaders["Content-Type"], "type")
XCTAssertEqual(newRequest.headers["Content-Type"], "type")
XCTAssertEqual(newRequest.httpBody?.asString, "property=property")
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/RequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ class RequestDebugDescriptionTestCase: BaseTestCase {
headers["Accept-Language"] = "en-US"

let configuration = URLSessionConfiguration.af.default
configuration.httpHeaders = headers
configuration.headers = headers

let manager = Session(configuration: configuration)

Expand All @@ -581,7 +581,7 @@ class RequestDebugDescriptionTestCase: BaseTestCase {
headers["Content-Type"] = "application/json"

let configuration = URLSessionConfiguration.af.default
configuration.httpHeaders = headers
configuration.headers = headers

let manager = Session(configuration: configuration)

Expand Down
8 changes: 4 additions & 4 deletions Tests/SessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ class SessionTestCase: BaseTestCase {

var urlRequest = urlRequest

var finalHeaders = urlRequest.httpHeaders
var finalHeaders = urlRequest.headers
headers.forEach { finalHeaders.add($0) }

urlRequest.httpHeaders = finalHeaders
urlRequest.headers = finalHeaders

return urlRequest
}
Expand Down Expand Up @@ -121,7 +121,7 @@ class SessionTestCase: BaseTestCase {
adaptedCount += 1

if shouldApplyAuthorizationHeader && adaptedCount > 1 {
urlRequest.httpHeaders.update(.authorization(username: "user", password: "password"))
urlRequest.headers.update(.authorization(username: "user", password: "password"))
}

return urlRequest
Expand Down Expand Up @@ -1537,7 +1537,7 @@ class SessionManagerConfigurationHeadersTestCase: BaseTestCase {

var headers = HTTPHeaders.default
headers["Authorization"] = "Bearer 123456"
configuration.httpHeaders = headers
configuration.headers = headers

return configuration
}()
Expand Down
2 changes: 1 addition & 1 deletion Tests/URLProtocolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ProxyURLProtocol: URLProtocol {
lazy var session: URLSession = {
let configuration: URLSessionConfiguration = {
let configuration = URLSessionConfiguration.ephemeral
configuration.httpHeaders = HTTPHeaders.default
configuration.headers = HTTPHeaders.default

return configuration
}()
Expand Down
2 changes: 1 addition & 1 deletion Tests/ValidationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ class ContentTypeValidationTestCase: BaseTestCase {
let manager: Session = {
let configuration: URLSessionConfiguration = {
let configuration = URLSessionConfiguration.ephemeral
configuration.httpHeaders = HTTPHeaders.default
configuration.headers = HTTPHeaders.default

return configuration
}()
Expand Down

0 comments on commit 9d1cec8

Please sign in to comment.