Skip to content

Commit

Permalink
Accept dictionary literals for HTTPHeaders (apple#824)
Browse files Browse the repository at this point in the history
### Motivation:
A convenience/shorthand for developers.

### Modifications:
Add conformance to ExpressibleByDictionaryLiteral along with implementation.

### Result:
The following is would then be legal in Vapor:

```
        return HTTPResponse(status: .ok, headers: ["Content-Type": "text/html"],
                            body: "<table border=1><tr><td><i>Hello</i>, plugin!")
```
  • Loading branch information
johnno1962 authored and weissi committed Feb 14, 2019
1 parent 30446ee commit f84b5b8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Sources/NIOHTTP1/HTTPTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ private extension UInt8 {
/// field when needed. It also supports recomposing headers to a maximally joined
/// or split representation, such that header fields that are able to be repeated
/// can be represented appropriately.
public struct HTTPHeaders: CustomStringConvertible {
public struct HTTPHeaders: CustomStringConvertible, ExpressibleByDictionaryLiteral {

private final class _Storage {
var buffer: ByteBuffer
Expand Down Expand Up @@ -556,6 +556,14 @@ public struct HTTPHeaders: CustomStringConvertible {
}
}

/// Construct a `HTTPHeaders` structure.
///
/// - parameters
/// - elements: name, value pairs provided by a dictionary literal.
public init(dictionaryLiteral elements: (String, String)...) {
self.init(elements)
}

private func isConnectionHeader(_ header: HTTPHeaderIndex) -> Bool {
return self.buffer.equalCaseInsensitiveASCII(view: "connection".utf8, at: header)
}
Expand Down
1 change: 1 addition & 0 deletions Tests/NIOHTTP1Tests/HTTPHeadersTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extension HTTPHeadersTest {
static var allTests : [(String, (HTTPHeadersTest) -> () throws -> Void)] {
return [
("testCasePreservedButInsensitiveLookup", testCasePreservedButInsensitiveLookup),
("testDictionaryLiteralAlternative", testDictionaryLiteralAlternative),
("testWriteHeadersSeparately", testWriteHeadersSeparately),
("testRevealHeadersSeparately", testRevealHeadersSeparately),
("testSubscriptDoesntSplitHeaders", testSubscriptDoesntSplitHeaders),
Expand Down
31 changes: 31 additions & 0 deletions Tests/NIOHTTP1Tests/HTTPHeadersTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ class HTTPHeadersTest : XCTestCase {
}
}

func testDictionaryLiteralAlternative() {
let headers: HTTPHeaders = [ "User-Agent": "1",
"host": "2",
"X-SOMETHING": "3",
"SET-COOKIE": "foo=bar",
"Set-Cookie": "buz=cux"]

// looking up headers value is case-insensitive
XCTAssertEqual(["1"], headers["User-Agent"])
XCTAssertEqual(["1"], headers["User-agent"])
XCTAssertEqual(["2"], headers["Host"])
XCTAssertEqual(["foo=bar", "buz=cux"], headers["set-cookie"])

for (key,value) in headers {
switch key {
case "User-Agent":
XCTAssertEqual("1", value)
case "host":
XCTAssertEqual("2", value)
case "X-SOMETHING":
XCTAssertEqual("3", value)
case "SET-COOKIE":
XCTAssertEqual("foo=bar", value)
case "Set-Cookie":
XCTAssertEqual("buz=cux", value)
default:
XCTFail("Unexpected key: \(key)")
}
}
}

func testWriteHeadersSeparately() {
let originalHeaders = [ ("User-Agent", "1"),
("host", "2"),
Expand Down

0 comments on commit f84b5b8

Please sign in to comment.