-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestServerResponse.swift
176 lines (140 loc) · 4.13 KB
/
TestServerResponse.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// TestServerResponse.swift
// Macro
//
// Created by Helge Hess.
// Copyright © 2020-2023 ZeeZide GmbH. All rights reserved.
//
import struct Logging.Logger
import struct NIOHTTP1.HTTPHeaders
import enum NIOHTTP1.HTTPResponseStatus
import struct NIOHTTP1.HTTPResponseHead
import enum MacroCore.WritableError
import struct MacroCore.Buffer
import class http.ServerResponse
public let MacroTestLogger = Logger(label: "μ.tests")
/**
* A ServerResponse for testing purposes. Doesn't write to an actual Channel.
*/
open class TestServerResponse: ServerResponse {
public var continueCount = 0
public var writtenContent = Buffer()
public var errorToTrigger : Swift.Error?
public init() {
super.init(unsafeChannel: nil, log: MacroTestLogger)
}
// MARK: - Emit Header
@usableFromInline
internal func primaryWriteHead(_ part: HTTPResponseHead) {
assert(!headersSent)
guard !headersSent else { return }
headersSent = true
}
@usableFromInline
internal func primaryWriteHead() {
let head = HTTPResponseHead(version: version,
status: status, headers: headers)
primaryWriteHead(head)
}
@inlinable
override open func writeHead(_ status : HTTPResponseStatus = .ok,
headers : HTTPHeaders = [:])
{
if !headers.isEmpty {
for ( name, value ) in headers {
setHeader(name, value)
}
}
let head = HTTPResponseHead(version: version,
status: status, headers: headers)
primaryWriteHead(head)
}
// MARK: - End
override open func end() {
guard !writableEnded else { return }
if !headersSent { primaryWriteHead() }
state = .finished
finishListeners.emit()
_clearListenersOnFinish()
}
private func _clearListenersOnFinish() {
finishListeners.removeAll()
errorListeners .removeAll()
}
@inlinable
override open func writeHead(_ statusCode : Int,
_ statusMessage : String?,
_ headers : [ String : Any ] = [ : ])
{
assert(!headersSent)
guard !headersSent else { return }
self.statusCode = statusCode
if let s = statusMessage { self.statusMessage = s }
// merge in headers
for ( key, value ) in headers {
setHeader(key, value)
}
primaryWriteHead()
}
// MARK: - 100-continue
override open func writeContinue() {
guard !writableEnded else {
handleError(WritableError.writableEnded)
return
}
continueCount += 1
}
// MARK: - WritableByteStream
private func consumeErrorToTrigger() -> Swift.Error? {
guard let error = errorToTrigger else { return nil }
errorToTrigger = nil
return error
}
@discardableResult
override
open func write(_ bytes: Buffer, whenDone: @escaping ( Error? ) -> Void)
-> Bool
{
guard !writableEnded else {
handleError(WritableError.writableEnded)
return true
}
if !headersSent { primaryWriteHead() }
if let error = consumeErrorToTrigger() {
handleError(error)
whenDone(error)
}
else {
writtenContent.append(bytes)
whenDone(nil)
}
return true
}
@discardableResult
override open func write(_ bytes: Buffer,
whenDone: @escaping () -> Void = {}) -> Bool
{
return write(bytes) { _ in whenDone() }
}
@discardableResult @inlinable override
open func write(_ string: String, whenDone: @escaping () -> Void = {}) -> Bool
{
return write(Buffer(string), whenDone: whenDone)
}
// MARK: - CustomStringConvertible
override open var description: String {
let id = String(Int(bitPattern: ObjectIdentifier(self)), radix: 16)
var ms = "<TestResponse[0x\(id)]:"
defer { ms += ">" }
ms += " \(statusCode)"
if writableEnded { ms += " ended" }
if writableCorked { ms += " corked" }
for ( key, value ) in environment.loggingDictionary {
ms += " \(key)=\(value)"
}
if !writtenContent.isEmpty {
ms += " #written=\(writtenContent.count)"
}
return ms
}
}