mirror of
https://github.com/apple/swift-http-structured-headers
synced 2026-11-24 04:37:50 +00:00
Motivation: [RFC 9651](https://www.ietf.org/rfc/rfc9651.html) permits serializing an integer in the range of -999,999,999,999,999 to 999,999,999,999,999 inclusive ([Section 4.1.4-2.1.1](https://www.ietf.org/rfc/rfc9651.html#section-4.1.4-2.1.1)). As `RFC9651BareItem` takes values `integer` or `date` with an associated value of type `Int`, that range is implicitly more constrained on 32-bit platforms to a subset range of `Int32.min` to `Int32.max` inclusive. Modifications: - Adjust `RFC9651BareItem` to take values `integer` or `date` with an associated value of type `Int64` instead, which is sufficient for the permitted range. Result: Compatibility for the Integer and Date Structured Types on 32-bit platforms.
155 lines
5.8 KiB
Swift
155 lines
5.8 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
import Foundation
|
|
|
|
enum FixturesLoader {
|
|
private static var fixturesDirectory: URL {
|
|
let myURL = URL(fileURLWithPath: #filePath, isDirectory: false)
|
|
return URL(string: "../TestFixtures/", relativeTo: myURL)!.absoluteURL
|
|
}
|
|
|
|
private static var serializationFixturesDirectory: URL {
|
|
fixturesDirectory.appendingPathComponent("serialisation-tests").absoluteURL
|
|
}
|
|
|
|
static var parsingFixtures: [StructuredHeaderTestFixture] {
|
|
// ContentsOfDirectory can throw if it hits EINTR, just spin
|
|
var files: [URL]?
|
|
for _ in 0..<1000 {
|
|
do {
|
|
files = try FileManager.default.contentsOfDirectory(
|
|
at: fixturesDirectory,
|
|
includingPropertiesForKeys: nil,
|
|
options: []
|
|
)
|
|
break
|
|
} catch let error as NSError {
|
|
guard let underlyingError = error.userInfo[NSUnderlyingErrorKey] as? NSError,
|
|
underlyingError.domain == NSPOSIXErrorDomain, underlyingError.code == EINTR
|
|
else {
|
|
fatalError("\(error)")
|
|
}
|
|
// Ok, we'll continue
|
|
}
|
|
}
|
|
guard let realFiles = files else {
|
|
fatalError("Hit EINTR 1000 times!")
|
|
}
|
|
let decoder = JSONDecoder()
|
|
decoder.keyDecodingStrategy = .convertFromSnakeCase
|
|
|
|
return realFiles.filter { $0.pathExtension == "json" }.flatMap { file -> [StructuredHeaderTestFixture] in
|
|
let content = try! Data(contentsOf: file, options: [.uncached, .mappedIfSafe])
|
|
return try! decoder.decode([StructuredHeaderTestFixture].self, from: content)
|
|
}
|
|
}
|
|
|
|
static var serializingFixtures: [StructuredHeaderTestFixture] {
|
|
// ContentsOfDirectory can throw if it hits EINTR, just spin
|
|
var files: [URL]?
|
|
for _ in 0..<1000 {
|
|
do {
|
|
files = try FileManager.default.contentsOfDirectory(
|
|
at: serializationFixturesDirectory,
|
|
includingPropertiesForKeys: nil,
|
|
options: []
|
|
)
|
|
break
|
|
} catch let error as NSError {
|
|
guard let underlyingError = error.userInfo[NSUnderlyingErrorKey] as? NSError,
|
|
underlyingError.domain == NSPOSIXErrorDomain, underlyingError.code == EINTR
|
|
else {
|
|
fatalError("\(error)")
|
|
}
|
|
// Ok, we'll continue
|
|
}
|
|
}
|
|
guard let realFiles = files else {
|
|
fatalError("Hit EINTR 1000 times!")
|
|
}
|
|
let decoder = JSONDecoder()
|
|
decoder.keyDecodingStrategy = .convertFromSnakeCase
|
|
|
|
return realFiles.filter { $0.pathExtension == "json" }.flatMap { file -> [StructuredHeaderTestFixture] in
|
|
let content = try! Data(contentsOf: file, options: [.uncached, .mappedIfSafe])
|
|
return try! decoder.decode([StructuredHeaderTestFixture].self, from: content)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct StructuredHeaderTestFixture: Decodable {
|
|
/// The name of this test.
|
|
var name: String
|
|
|
|
/// The list of header fields that should be parsed. Not present if this is a serialization test.
|
|
var raw: [String]?
|
|
|
|
/// The type to use to parse the fields
|
|
var headerType: String
|
|
|
|
/// The expected data structure to be produced. Note that this is stored as a serialized JSON object:
|
|
/// this is because the shape of this data structure varies from test-case to test-case, so we can't
|
|
/// incorporate it into the type system. This type is also preventing us from using Codable here: we just
|
|
/// don't know what the heck we're expecting to see.
|
|
var expected: JSONSchema?
|
|
|
|
/// Whether this test is required to fail.
|
|
var mustFail: Bool?
|
|
|
|
/// Whether this test is allowed to fail.
|
|
var canFail: Bool?
|
|
|
|
/// The canonical form if the field value, if different from raw. Not applicable if `mustFail` is `true`.
|
|
var canonical: [String]?
|
|
}
|
|
|
|
/// This defines the JSON types that can be used to encode structured headers. It allows us to express the expected outcome of
|
|
/// a parse in a type-safe way.
|
|
enum JSONSchema: Decodable {
|
|
case dictionary([String: JSONSchema])
|
|
case array([JSONSchema])
|
|
case integer(Int64)
|
|
case double(Double)
|
|
case string(String)
|
|
case bool(Bool)
|
|
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.singleValueContainer()
|
|
if let value = try? container.decode(String.self) {
|
|
self = .string(value)
|
|
} else if let bool = try? container.decode(Bool.self) {
|
|
self = .bool(bool)
|
|
} else if let value = try? container.decode(Int64.self) {
|
|
self = .integer(value)
|
|
} else if let value = try? container.decode(Double.self) {
|
|
self = .double(value)
|
|
} else if let value = try? container.decode([String: JSONSchema].self) {
|
|
self = .dictionary(value)
|
|
} else if let value = try? container.decode([JSONSchema].self) {
|
|
self = .array(value)
|
|
} else {
|
|
preconditionFailure("Failed to decode at \(container.codingPath)")
|
|
}
|
|
}
|
|
|
|
var isArray: Bool {
|
|
switch self {
|
|
case .array:
|
|
return true
|
|
case .dictionary, .integer, .double, .string, .bool:
|
|
return false
|
|
}
|
|
}
|
|
}
|