Skip to content

Commit

Permalink
Update code
Browse files Browse the repository at this point in the history
Signed-off-by: Rafal Augustyniak <[email protected]>
  • Loading branch information
Augustyniak committed Jun 23, 2022
1 parent 8137392 commit 15f722b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 19 deletions.
11 changes: 6 additions & 5 deletions library/swift/HeadersBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public class HeadersBuilder: NSObject {

private var _headers: [String: KeyValuesPair]

var headers: [String: [String]] {
return Dictionary(uniqueKeysWithValues: _headers.map { _, value in
return (value.key, value.values)
})
func headers() -> [String: [String]] {
return Dictionary(uniqueKeysWithValues: self._headers.map { _, value in
return (value.key, value.values)
})
}

/// Append a value to the header name.
Expand Down Expand Up @@ -117,6 +117,7 @@ public class HeadersBuilder: NSObject {
///
/// - parameter headers: The headers with which to start.
required init(headers: [String: [String]]) {

var processedHeaders = [String: KeyValuesPair]()
for (name, values) in headers {
let lowercasedName = name.lowercased()
Expand Down Expand Up @@ -145,6 +146,6 @@ public class HeadersBuilder: NSObject {

extension HeadersBuilder {
public override func isEqual(_ object: Any?) -> Bool {
return (object as? Self)?.headers == self.headers
return (object as? Self)?.headers() == self.headers()
}
}
2 changes: 1 addition & 1 deletion library/swift/RequestHeadersBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ public final class RequestHeadersBuilder: HeadersBuilder {
///
/// - returns: New instance of request headers.
public func build() -> RequestHeaders {
return RequestHeaders(headers: self.headers)
return RequestHeaders(headers: self.headers())
}
}
2 changes: 1 addition & 1 deletion library/swift/RequestTrailersBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public final class RequestTrailersBuilder: HeadersBuilder {
///
/// - returns: New instance of request trailers.
public func build() -> RequestTrailers {
return RequestTrailers(headers: self.headers)
return RequestTrailers(headers: self.headers())
}
}
2 changes: 1 addition & 1 deletion library/swift/ResponseHeadersBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public final class ResponseHeadersBuilder: HeadersBuilder {
///
/// - returns: New instance of response headers.
public func build() -> ResponseHeaders {
return ResponseHeaders(headers: self.headers)
return ResponseHeaders(headers: self.headers())
}
}
2 changes: 1 addition & 1 deletion library/swift/ResponseTrailersBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public final class ResponseTrailersBuilder: HeadersBuilder {
///
/// - returns: New instance of response trailers.
public func build() -> ResponseTrailers {
return ResponseTrailers(headers: self.headers)
return ResponseTrailers(headers: self.headers())
}
}
2 changes: 1 addition & 1 deletion library/swift/grpc/GRPCRequestHeadersBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public final class GRPCRequestHeadersBuilder: HeadersBuilder {
///
/// - returns: New instance of request headers.
public func build() -> GRPCRequestHeaders {
return GRPCRequestHeaders(headers: self.headers)
return GRPCRequestHeaders(headers: self.headers())
}
}
18 changes: 9 additions & 9 deletions test/swift/HeadersBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class HeadersBuilderTests: XCTestCase {
let headers = HeadersBuilder(headers: [:])
.add(name: "x-foo", value: "1")
.add(name: "x-foo", value: "2")
.headers
.headers()
XCTAssertEqual(["1", "2"], headers["x-foo"])
}

Expand All @@ -19,7 +19,7 @@ final class HeadersBuilderTests: XCTestCase {
.add(name: "x-foo", value: "1")
.add(name: "x-foo", value: "2")
.remove(name: "x-foo")
.headers
.headers()
XCTAssertNil(headers["x-foo"])
}

Expand All @@ -28,50 +28,50 @@ final class HeadersBuilderTests: XCTestCase {
.add(name: "x-foo", value: "123")
.add(name: "x-bar", value: "abc")
.remove(name: "x-foo")
.headers
.headers()
XCTAssertEqual(["x-bar": ["abc"]], headers)
}

func testSettingHeaderReplacesExistingHeadersWithMatchingName() {
let headers = HeadersBuilder(headers: [:])
.add(name: "x-foo", value: "123")
.set(name: "x-foo", value: ["abc"])
.headers
.headers()
XCTAssertEqual(["x-foo": ["abc"]], headers)
}

func testInitializationIsCaseInsensitivePreservesCasingAndProcessesConflictingHeadersInAlphabeticalOrder() {
let headers = HeadersBuilder(headers: ["a": ["456"], "A": ["123"]])
XCTAssertEqual(["A": ["123", "456"]], headers.headers)
XCTAssertEqual(["A": ["123", "456"]], headers.headers())
}

func testAddingHeaderIsCaseInsensitiveAndHeaderCasingIsPreserved() {
let headers = HeadersBuilder(headers: [:])
headers.add(name: "fOo", value: "abc")
headers.add(name: "foo", value: "123")
XCTAssertEqual(["fOo": ["abc", "123"]], headers.headers)
XCTAssertEqual(["fOo": ["abc", "123"]], headers.headers())
}

func testSettingHeaderIsCaseInsensitiveAndHeaderCasingIsPreserved() {
let headers = HeadersBuilder(headers: [:])
headers.set(name: "foo", value: ["123"])
headers.set(name: "fOo", value: ["abc"])
XCTAssertEqual(["fOo": ["abc"]], headers.headers)
XCTAssertEqual(["fOo": ["abc"]], headers.headers())
}

func testRemovingHeaderIsCaseInsensitive() {
let headers = HeadersBuilder(headers: [:])
headers.set(name: "foo", value: ["123"])
headers.remove(name: "fOo")
XCTAssertEqual([:], headers.headers)
XCTAssertEqual([:], headers.headers())
}

func testRestrictedHeadersAreNotSettable() {
let headers = RequestHeadersBuilder(method: .get, authority: "example.com", path: "/")
.add(name: "host", value: "example.com")
.set(name: ":scheme", value: ["http"])
.set(name: ":path", value: ["/nope"])
.headers
.headers()
let expected = [
":authority": ["example.com"],
":path": ["/"],
Expand Down

0 comments on commit 15f722b

Please sign in to comment.