/
XCTHTTPRequest.swift
66 lines (56 loc) 路 2.02 KB
/
XCTHTTPRequest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public struct XCTHTTPRequest {
public var method: HTTPMethod
public var url: URI
public var headers: HTTPHeaders
public var body: ByteBuffer
private struct _ContentContainer: ContentContainer {
var body: ByteBuffer
var headers: HTTPHeaders
var contentType: HTTPMediaType? {
return self.headers.contentType
}
mutating func encode<E>(_ encodable: E, using encoder: ContentEncoder) throws where E : Encodable {
try encoder.encode(encodable, to: &self.body, headers: &self.headers)
}
func decode<D>(_ decodable: D.Type, using decoder: ContentDecoder) throws -> D where D : Decodable {
fatalError("Decoding from test request is not supported.")
}
mutating func encode<C>(_ content: C, using encoder: ContentEncoder) throws where C : Content {
var content = content
try content.beforeEncode()
try encoder.encode(content, to: &self.body, headers: &self.headers)
}
}
public var content: ContentContainer {
get {
_ContentContainer(body: self.body, headers: self.headers)
}
set {
let content = (newValue as! _ContentContainer)
self.body = content.body
self.headers = content.headers
}
}
private struct _URLQueryContainer: URLQueryContainer {
var url: URI
func decode<D>(_ decodable: D.Type, using decoder: URLQueryDecoder) throws -> D
where D: Decodable
{
fatalError("Decoding from test request is not supported.")
}
mutating func encode<E>(_ encodable: E, using encoder: URLQueryEncoder) throws
where E: Encodable
{
try encoder.encode(encodable, to: &self.url)
}
}
public var query: URLQueryContainer {
get {
_URLQueryContainer(url: url)
}
set {
let query = (newValue as! _URLQueryContainer)
self.url = query.url
}
}
}