mirror of
https://github.com/apple/swift-http-types
synced 2026-11-29 05:14:22 +00:00
Merge main into format
This commit is contained in:
commit
a489393bc9
4 files changed
+80
-2
No files matched your search
@@ -22,7 +22,7 @@
|
||||
/// the "Host" header.
|
||||
public struct HTTPRequest: Sendable, Hashable {
|
||||
/// The HTTP request method
|
||||
public struct Method: Hashable, RawRepresentable, LosslessStringConvertible {
|
||||
public struct Method: Sendable, Hashable, RawRepresentable, LosslessStringConvertible {
|
||||
/// The string value of the request.
|
||||
public let rawValue: String
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
public struct HTTPResponse: Sendable, Hashable {
|
||||
/// The response status consisting of a 3-digit status code and a reason phrase. The reason
|
||||
/// phrase is ignored by modern HTTP versions.
|
||||
public struct Status: Hashable, ExpressibleByIntegerLiteral, CustomStringConvertible {
|
||||
public struct Status: Sendable, Hashable, ExpressibleByIntegerLiteral, CustomStringConvertible {
|
||||
/// The 3-digit status code.
|
||||
public let code: Int
|
||||
/// The reason phrase.
|
||||
|
||||
@@ -126,4 +126,55 @@ extension URLSession {
|
||||
return (data, response)
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
||||
extension URLSession {
|
||||
/// Convenience method to load data using an `HTTPRequest`; creates and resumes a `URLSessionDataTask` internally.
|
||||
///
|
||||
/// - Parameter request: The `HTTPRequest` for which to load data.
|
||||
/// - Returns: Data and response.
|
||||
public func data(for request: HTTPRequest) async throws -> (Data, HTTPResponse) {
|
||||
guard let urlRequest = URLRequest(httpRequest: request) else {
|
||||
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
|
||||
}
|
||||
let (data, urlResponse) = try await data(for: urlRequest)
|
||||
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
|
||||
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
|
||||
}
|
||||
return (data, response)
|
||||
}
|
||||
|
||||
/// Convenience method to upload data using an `HTTPRequest`; creates and resumes a `URLSessionUploadTask` internally.
|
||||
///
|
||||
/// - Parameter request: The `HTTPRequest` for which to upload data.
|
||||
/// - Parameter fileURL: File to upload.
|
||||
/// - Returns: Data and response.
|
||||
public func upload(for request: HTTPRequest, fromFile fileURL: URL) async throws -> (Data, HTTPResponse) {
|
||||
guard let urlRequest = URLRequest(httpRequest: request) else {
|
||||
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
|
||||
}
|
||||
let (data, urlResponse) = try await upload(for: urlRequest, fromFile: fileURL)
|
||||
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
|
||||
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
|
||||
}
|
||||
return (data, response)
|
||||
}
|
||||
|
||||
/// Convenience method to upload data using an `HTTPRequest`, creates and resumes a `URLSessionUploadTask` internally.
|
||||
///
|
||||
/// - Parameter request: The `HTTPRequest` for which to upload data.
|
||||
/// - Parameter bodyData: Data to upload.
|
||||
/// - Returns: Data and response.
|
||||
public func upload(for request: HTTPRequest, from bodyData: Data) async throws -> (Data, HTTPResponse) {
|
||||
guard let urlRequest = URLRequest(httpRequest: request) else {
|
||||
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
|
||||
}
|
||||
let (data, urlResponse) = try await upload(for: urlRequest, from: bodyData)
|
||||
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
|
||||
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
|
||||
}
|
||||
return (data, response)
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -72,4 +72,31 @@ final class HTTPTypesTests: XCTestCase {
|
||||
XCTAssertEqual(response2.headerFields.count, 3)
|
||||
XCTAssertEqual(response2.headerFields[.server], "HTTPServer/1.0")
|
||||
}
|
||||
|
||||
func testSendable() {
|
||||
func isSendable(_ value: some Sendable) -> Bool { true }
|
||||
func isSendable(_ value: Any) -> Bool { false }
|
||||
|
||||
let field: HTTPField = HTTPField(name: .userAgent, value: "")
|
||||
let indexingStrategy: HTTPField.DynamicTableIndexingStrategy = field.indexingStrategy
|
||||
let name: HTTPField.Name = field.name
|
||||
let fields: HTTPFields = [:]
|
||||
let request: HTTPRequest = HTTPRequest(method: .post, scheme: nil, authority: nil, path: nil)
|
||||
let method: HTTPRequest.Method = request.method
|
||||
let requestPseudoHeaderFields: HTTPRequest.PseudoHeaderFields = request.pseudoHeaderFields
|
||||
let response: HTTPResponse = HTTPResponse(status: .ok)
|
||||
let status: HTTPResponse.Status = response.status
|
||||
let responsePseudoHeaderFields: HTTPResponse.PseudoHeaderFields = response.pseudoHeaderFields
|
||||
|
||||
XCTAssertTrue(isSendable(field))
|
||||
XCTAssertTrue(isSendable(indexingStrategy))
|
||||
XCTAssertTrue(isSendable(name))
|
||||
XCTAssertTrue(isSendable(fields))
|
||||
XCTAssertTrue(isSendable(request))
|
||||
XCTAssertTrue(isSendable(method))
|
||||
XCTAssertTrue(isSendable(requestPseudoHeaderFields))
|
||||
XCTAssertTrue(isSendable(response))
|
||||
XCTAssertTrue(isSendable(status))
|
||||
XCTAssertTrue(isSendable(responsePseudoHeaderFields))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user