mirror of
https://github.com/apple/swift-http-structured-headers
synced 2026-11-28 05:04:22 +00:00
Add support for Date type to StructuredFieldValues (#49)
Motivation: The parser and serializer for the Date type has been implemented. The high-level encoder and decoder should follow. Modifications: - Implement the encoder and decoder for Date. Result: The StructuredFieldValues module will support the Date type.
This commit is contained in:
1 parent
c2e79ce44c
commit
6e527aa16a
12 files changed
+222
No files matched your search
@@ -490,4 +490,125 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
try StructuredFieldValueDecoder().decode(DictionaryField.self, from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingDateAsTopLevelData() throws {
|
||||
let headerField = "@4294967296"
|
||||
XCTAssertEqual(
|
||||
ItemField(Date(timeIntervalSince1970: 4_294_967_296)),
|
||||
try StructuredFieldValueDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingDateAsParameterisedData() throws {
|
||||
struct Item: StructuredFieldValue, Equatable {
|
||||
static let structuredFieldType: StructuredFieldType = .item
|
||||
var item: Date
|
||||
var parameters: [String: Float]
|
||||
}
|
||||
|
||||
let headerFieldNoParameters = "@4294967296"
|
||||
let headerFieldParameters = "@4294967296;q=0.8"
|
||||
|
||||
XCTAssertEqual(
|
||||
Item(
|
||||
item: Date(timeIntervalSince1970: 4_294_967_296),
|
||||
parameters: [:]
|
||||
),
|
||||
try StructuredFieldValueDecoder().decode(
|
||||
Item.self,
|
||||
from: Array(headerFieldNoParameters.utf8)
|
||||
)
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
Item(item: Date(timeIntervalSince1970: 4_294_967_296), parameters: ["q": 0.8]),
|
||||
try StructuredFieldValueDecoder().decode(
|
||||
Item.self,
|
||||
from: Array(headerFieldParameters.utf8)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingDateInParameterField() throws {
|
||||
struct Item: StructuredFieldValue, Equatable {
|
||||
static let structuredFieldType: StructuredFieldType = .item
|
||||
var item: Int
|
||||
var parameters: [String: Date]
|
||||
}
|
||||
|
||||
let headerField = "1;q=@4294967296"
|
||||
XCTAssertEqual(
|
||||
Item(item: 1, parameters: ["q": Date(timeIntervalSince1970: 4_294_967_296)]),
|
||||
try StructuredFieldValueDecoder().decode(Item.self, from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingDateInOuterListRaw() throws {
|
||||
let headerField = "@4294967296, @-1659578233"
|
||||
XCTAssertEqual(
|
||||
List(
|
||||
[
|
||||
Date(timeIntervalSince1970: 4_294_967_296),
|
||||
Date(timeIntervalSince1970: -1_659_578_233),
|
||||
]
|
||||
),
|
||||
try StructuredFieldValueDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingDateInInnerListRaw() throws {
|
||||
let headerField = "(@4294967296 @-1659578233), (@4294967296 @-1659578233)"
|
||||
XCTAssertEqual(
|
||||
List(
|
||||
Array(
|
||||
repeating: [
|
||||
Date(timeIntervalSince1970: 4_294_967_296),
|
||||
Date(timeIntervalSince1970: -1_659_578_233),
|
||||
],
|
||||
count: 2
|
||||
)
|
||||
),
|
||||
try StructuredFieldValueDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingDateInInnerListKeyed() throws {
|
||||
struct ListField: Codable, Equatable {
|
||||
var items: [Date]
|
||||
var parameters: [String: Bool]
|
||||
}
|
||||
let headerField = "(@4294967296 @-1659578233);foo, (@4294967296 @-1659578233);foo"
|
||||
XCTAssertEqual(
|
||||
List(
|
||||
Array(
|
||||
repeating: ListField(
|
||||
items: [
|
||||
Date(timeIntervalSince1970: 4_294_967_296),
|
||||
Date(timeIntervalSince1970: -1_659_578_233),
|
||||
],
|
||||
parameters: ["foo": true]
|
||||
),
|
||||
count: 2
|
||||
)
|
||||
),
|
||||
try StructuredFieldValueDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingDateInDictionaries() throws {
|
||||
struct DictionaryField: StructuredFieldValue, Equatable {
|
||||
static let structuredFieldType: StructuredFieldType = .dictionary
|
||||
var bin: Date
|
||||
var box: Date
|
||||
}
|
||||
|
||||
let headerField = "bin=@4294967296, box=@-1659578233"
|
||||
XCTAssertEqual(
|
||||
DictionaryField(
|
||||
bin: Date(timeIntervalSince1970: 4_294_967_296),
|
||||
box: Date(timeIntervalSince1970: -1_659_578_233)
|
||||
),
|
||||
try StructuredFieldValueDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user