File: HTTPTest.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (269 lines) | stat: -rw-r--r-- 10,699 bytes parent folder | download
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 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 XCTest
@testable import NIO
@testable import NIOHTTP1

private final class TestChannelInboundHandler: ChannelInboundHandler {
    public typealias InboundIn = HTTPServerRequestPart
    public typealias InboundOut = HTTPServerRequestPart

    private let body: (HTTPServerRequestPart) -> HTTPServerRequestPart

    init(_ body: @escaping (HTTPServerRequestPart) -> HTTPServerRequestPart) {
        self.body = body
    }

    public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
        context.fireChannelRead(self.wrapInboundOut(self.body(self.unwrapInboundIn(data))))
    }
}


class HTTPTest: XCTestCase {

    func checkHTTPRequest(_ expected: HTTPRequestHead, body: String? = nil, trailers: HTTPHeaders? = nil) throws {
        try checkHTTPRequests([expected], body: body, trailers: trailers)
    }

    func checkHTTPRequests(_ expecteds: [HTTPRequestHead], body: String? = nil, trailers: HTTPHeaders? = nil) throws {
        func httpRequestStrForRequest(_ req: HTTPRequestHead) -> String {
            var s = "\(req.method) \(req.uri) HTTP/\(req.version.major).\(req.version.minor)\r\n"
            for (k, v) in req.headers {
                s += "\(k): \(v)\r\n"
            }
            if trailers != nil {
                s += "Transfer-Encoding: chunked\r\n"
                s += "\r\n"
                if let body = body {
                    s += String(body.utf8.count, radix: 16)
                    s += "\r\n"
                    s += body
                    s += "\r\n"
                }
                s += "0\r\n"
                if let trailers = trailers {
                    for (k, v) in trailers {
                        s += "\(k): \(v)\r\n"
                    }
                }
                s += "\r\n"
            } else if let body = body {
                let bodyData = body.data(using: .utf8)!
                s += "Content-Length: \(bodyData.count)\r\n"
                s += "\r\n"
                s += body
            } else {
                s += "\r\n"
            }
            return s
        }

        func sendAndCheckRequests(_ expecteds: [HTTPRequestHead], body: String?, trailers: HTTPHeaders?, sendStrategy: (String, EmbeddedChannel) -> EventLoopFuture<Void>) throws -> String? {
            var step = 0
            var index = 0
            let channel = EmbeddedChannel()
            defer {
                XCTAssertNoThrow(try channel.finish())
            }
            try channel.pipeline.addHandler(ByteToMessageHandler(HTTPRequestDecoder())).wait()
            var bodyData: [UInt8]? = nil
            var allBodyDatas: [[UInt8]] = []
            try channel.pipeline.addHandler(TestChannelInboundHandler { reqPart in
                switch reqPart {
                case .head(var req):
                    XCTAssertEqual((index * 2), step)
                    req.headers.remove(name: "Content-Length")
                    req.headers.remove(name: "Transfer-Encoding")
                    XCTAssertEqual(expecteds[index], req)
                    step += 1
                case .body(var buffer):
                    if bodyData == nil {
                        bodyData = buffer.readBytes(length: buffer.readableBytes)!
                    } else {
                        bodyData!.append(contentsOf: buffer.readBytes(length: buffer.readableBytes)!)
                    }
                case .end(let receivedTrailers):
                    XCTAssertEqual(trailers, receivedTrailers)
                    step += 1
                    XCTAssertEqual(((index + 1) * 2), step)
                }
                return reqPart
            }).wait()

            var writeFutures: [EventLoopFuture<Void>] = []
            for expected in expecteds {
                writeFutures.append(sendStrategy(httpRequestStrForRequest(expected), channel))
                index += 1
                if let bodyData = bodyData {
                    allBodyDatas.append(bodyData)
                }
                bodyData = nil
            }
            channel.pipeline.flush()
            XCTAssertNoThrow(try EventLoopFuture.andAllSucceed(writeFutures, on: channel.eventLoop).wait())
            XCTAssertEqual(2 * expecteds.count, step)

            if body != nil {
                XCTAssertGreaterThan(allBodyDatas.count, 0)
                let firstBodyData = allBodyDatas[0]
                for bodyData in allBodyDatas {
                    XCTAssertEqual(firstBodyData, bodyData)
                }
                return String(decoding: firstBodyData, as: Unicode.UTF8.self)
            } else {
                XCTAssertEqual(0, allBodyDatas.count, "left with \(allBodyDatas)")
                return nil
            }
        }

        /* send all bytes in one go */
        let bd1 = try sendAndCheckRequests(expecteds, body: body, trailers: trailers, sendStrategy: { (reqString, chan) in
            var buf = chan.allocator.buffer(capacity: 1024)
            buf.writeString(reqString)
            return chan.eventLoop.makeSucceededFuture(()).flatMapThrowing {
                try chan.writeInbound(buf)
            }
        })

        /* send the bytes one by one */
        let bd2 = try sendAndCheckRequests(expecteds, body: body, trailers: trailers, sendStrategy: { (reqString, chan) in
            var writeFutures: [EventLoopFuture<Void>] = []
            for c in reqString {
                var buf = chan.allocator.buffer(capacity: 1024)

                buf.writeString("\(c)")
                writeFutures.append(chan.eventLoop.makeSucceededFuture(()).flatMapThrowing {
                    try chan.writeInbound(buf)
                })
            }
            return EventLoopFuture.andAllSucceed(writeFutures, on: chan.eventLoop)
        })

        XCTAssertEqual(bd1, bd2)
        XCTAssertEqual(body, bd1)
    }

    func testHTTPSimpleNoHeaders() throws {
        try checkHTTPRequest(HTTPRequestHead(version: .http1_1, method: .GET, uri: "/"))
    }

    func testHTTPSimple1Header() throws {
        var req = HTTPRequestHead(version: .http1_1, method: .GET, uri: "/hello/world")
        req.headers.add(name: "foo", value: "bar")
        try checkHTTPRequest(req)
    }

    func testHTTPSimpleSomeHeader() throws {
        var req = HTTPRequestHead(version: .http1_1, method: .GET, uri: "/foo/bar/buz?qux=quux")
        req.headers.add(name: "foo", value: "bar")
        req.headers.add(name: "qux", value: "quuux")
        try checkHTTPRequest(req)
    }

    func testHTTPPipelining() throws {
        var req1 = HTTPRequestHead(version: .http1_1, method: .GET, uri: "/foo/bar/buz?qux=quux")
        req1.headers.add(name: "foo", value: "bar")
        req1.headers.add(name: "qux", value: "quuux")
        var req2 = HTTPRequestHead(version: .http1_1, method: .GET, uri: "/")
        req2.headers.add(name: "a", value: "b")
        req2.headers.add(name: "C", value: "D")

        try checkHTTPRequests([req1, req2])
        try checkHTTPRequests(Array(repeating: req1, count: 10))
    }

    func testHTTPBody() throws {
        try checkHTTPRequest(HTTPRequestHead(version: .http1_1, method: .GET, uri: "/"),
                             body: "hello world")
    }

    func test1ByteHTTPBody() throws {
        try checkHTTPRequest(HTTPRequestHead(version: .http1_1, method: .GET, uri: "/"),
                             body: "1")
    }

    func testHTTPPipeliningWithBody() throws {
        try checkHTTPRequests(Array(repeating: HTTPRequestHead(version: .http1_1,
                                                               method: .GET, uri: "/"),
                                    count: 20),
                             body: "1")
    }

    func testChunkedBody() throws {
        var trailers = HTTPHeaders()
        trailers.add(name: "X-Key", value: "X-Value")
        trailers.add(name: "Something", value: "Else")
        try checkHTTPRequest(HTTPRequestHead(version: .http1_1, method: .POST, uri: "/"), body: "100", trailers: trailers)
    }

    func testHTTPRequestHeadCoWWorks() throws {
        let headers = HTTPHeaders([("foo", "bar")])
        var httpReq = HTTPRequestHead(version: .http1_1, method: .GET, uri: "/uri")
        httpReq.headers = headers

        var modVersion = httpReq
        modVersion.version = .http2
        XCTAssertEqual(.http1_1, httpReq.version)
        XCTAssertEqual(.http2, modVersion.version)

        var modMethod = httpReq
        modMethod.method = .POST
        XCTAssertEqual(.GET, httpReq.method)
        XCTAssertEqual(.POST, modMethod.method)

        var modURI = httpReq
        modURI.uri = "/changed"
        XCTAssertEqual("/uri", httpReq.uri)
        XCTAssertEqual("/changed", modURI.uri)

        var modHeaders = httpReq
        modHeaders.headers.add(name: "qux", value: "quux")
        XCTAssertEqual(httpReq.headers, headers)
        XCTAssertNotEqual(httpReq, modHeaders)
        modHeaders.headers.remove(name: "foo")
        XCTAssertEqual(httpReq.headers, headers)
        XCTAssertNotEqual(httpReq, modHeaders)
        modHeaders.headers.remove(name: "qux")
        modHeaders.headers.add(name: "foo", value: "bar")
        XCTAssertEqual(httpReq, modHeaders)
    }

    func testHTTPResponseHeadCoWWorks() throws {
        let headers = HTTPHeaders([("foo", "bar")])
        let httpRes = HTTPResponseHead(version: .http1_1, status: .ok, headers: headers)

        var modVersion = httpRes
        modVersion.version = .http2
        XCTAssertEqual(.http1_1, httpRes.version)
        XCTAssertEqual(.http2, modVersion.version)

        var modStatus = httpRes
        modStatus.status = .notFound
        XCTAssertEqual(.ok, httpRes.status)
        XCTAssertEqual(.notFound, modStatus.status)

        var modHeaders = httpRes
        modHeaders.headers.add(name: "qux", value: "quux")
        XCTAssertEqual(httpRes.headers, headers)
        XCTAssertNotEqual(httpRes, modHeaders)
        modHeaders.headers.remove(name: "foo")
        XCTAssertEqual(httpRes.headers, headers)
        XCTAssertNotEqual(httpRes, modHeaders)
        modHeaders.headers.remove(name: "qux")
        modHeaders.headers.add(name: "foo", value: "bar")
        XCTAssertEqual(httpRes, modHeaders)
    }
}