|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftNIOHTTPClient open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2018-2019 Swift Server Working Group and the SwiftNIOHTTPClient project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftNIOHTTPClient project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import NIO |
| 16 | +import NIOConcurrencyHelpers |
| 17 | +import NIOHTTP1 |
| 18 | +@testable import NIOHTTPClient |
| 19 | +import XCTest |
| 20 | + |
| 21 | +class HTTPClientInternalTests: XCTestCase { |
| 22 | + typealias Request = HTTPClient.Request |
| 23 | + typealias Task = HTTPClient.Task |
| 24 | + |
| 25 | + func testHTTPPartsHandler() throws { |
| 26 | + let channel = EmbeddedChannel() |
| 27 | + let recorder = RecordingHandler<HTTPClientResponsePart, HTTPClientRequestPart>() |
| 28 | + let task = Task<Void>(eventLoop: channel.eventLoop) |
| 29 | + |
| 30 | + try channel.pipeline.addHandler(recorder).wait() |
| 31 | + try channel.pipeline.addHandler(TaskHandler(task: task, delegate: TestHTTPDelegate(), redirectHandler: nil)).wait() |
| 32 | + |
| 33 | + var request = try Request(url: "http://localhost/get") |
| 34 | + request.headers.add(name: "X-Test-Header", value: "X-Test-Value") |
| 35 | + request.body = .string("1234") |
| 36 | + |
| 37 | + XCTAssertNoThrow(try channel.writeOutbound(request)) |
| 38 | + XCTAssertEqual(3, recorder.writes.count) |
| 39 | + var head = HTTPRequestHead(version: HTTPVersion(major: 1, minor: 1), method: .GET, uri: "/get") |
| 40 | + head.headers.add(name: "X-Test-Header", value: "X-Test-Value") |
| 41 | + head.headers.add(name: "Host", value: "localhost") |
| 42 | + head.headers.add(name: "Content-Length", value: "4") |
| 43 | + head.headers.add(name: "Connection", value: "close") |
| 44 | + XCTAssertEqual(HTTPClientRequestPart.head(head), recorder.writes[0]) |
| 45 | + let buffer = ByteBuffer.of(string: "1234") |
| 46 | + XCTAssertEqual(HTTPClientRequestPart.body(.byteBuffer(buffer)), recorder.writes[1]) |
| 47 | + |
| 48 | + XCTAssertNoThrow(try channel.writeInbound(HTTPClientResponsePart.head(HTTPResponseHead(version: HTTPVersion(major: 1, minor: 1), status: HTTPResponseStatus.ok)))) |
| 49 | + XCTAssertNoThrow(try channel.writeInbound(HTTPClientResponsePart.end(nil))) |
| 50 | + } |
| 51 | + |
| 52 | + func testHTTPPartsHandlerMultiBody() throws { |
| 53 | + let channel = EmbeddedChannel() |
| 54 | + let delegate = TestHTTPDelegate() |
| 55 | + let task = Task<Void>(eventLoop: channel.eventLoop) |
| 56 | + let handler = TaskHandler(task: task, delegate: delegate, redirectHandler: nil) |
| 57 | + |
| 58 | + try channel.pipeline.addHandler(handler).wait() |
| 59 | + |
| 60 | + handler.state = .sent |
| 61 | + var body = channel.allocator.buffer(capacity: 4) |
| 62 | + body.writeStaticString("1234") |
| 63 | + |
| 64 | + XCTAssertNoThrow(try channel.writeInbound(HTTPClientResponsePart.head(HTTPResponseHead(version: HTTPVersion(major: 1, minor: 1), status: HTTPResponseStatus.ok)))) |
| 65 | + XCTAssertNoThrow(try channel.writeInbound(HTTPClientResponsePart.body(body))) |
| 66 | + XCTAssertNoThrow(try channel.writeInbound(HTTPClientResponsePart.body(body))) |
| 67 | + |
| 68 | + switch delegate.state { |
| 69 | + case .body(_, let body): |
| 70 | + XCTAssertEqual(8, body.readableBytes) |
| 71 | + default: |
| 72 | + XCTFail("Expecting .body") |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + func testProxyStreaming() throws { |
| 77 | + let httpBin = HttpBin() |
| 78 | + let httpClient = HTTPClient(eventLoopGroupProvider: .createNew) |
| 79 | + defer { |
| 80 | + try! httpClient.syncShutdown() |
| 81 | + httpBin.shutdown() |
| 82 | + } |
| 83 | + |
| 84 | + let body: HTTPClient.Body = .stream(length: 50) { writer in |
| 85 | + do { |
| 86 | + var request = try Request(url: "http://localhost:\(httpBin.port)/events/10/1") |
| 87 | + request.headers.add(name: "Accept", value: "text/event-stream") |
| 88 | + |
| 89 | + let delegate = CopyingDelegate { part in |
| 90 | + writer.write(.byteBuffer(part)) |
| 91 | + } |
| 92 | + return httpClient.execute(request: request, delegate: delegate).futureResult |
| 93 | + } catch { |
| 94 | + return httpClient.eventLoopGroup.next().makeFailedFuture(error) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + let upload = try! httpClient.post(url: "http://localhost:\(httpBin.port)/post", body: body).wait() |
| 99 | + let bytes = upload.body.flatMap { $0.getData(at: 0, length: $0.readableBytes) } |
| 100 | + let data = try! JSONDecoder().decode(RequestInfo.self, from: bytes!) |
| 101 | + |
| 102 | + XCTAssertEqual(.ok, upload.status) |
| 103 | + XCTAssertEqual("id: 0id: 1id: 2id: 3id: 4id: 5id: 6id: 7id: 8id: 9", data.data) |
| 104 | + } |
| 105 | + |
| 106 | + func testProxyStreamingFailure() throws { |
| 107 | + let httpBin = HttpBin() |
| 108 | + let httpClient = HTTPClient(eventLoopGroupProvider: .createNew) |
| 109 | + defer { |
| 110 | + try! httpClient.syncShutdown() |
| 111 | + httpBin.shutdown() |
| 112 | + } |
| 113 | + |
| 114 | + var body: HTTPClient.Body = .stream(length: 50) { _ in |
| 115 | + httpClient.eventLoopGroup.next().makeFailedFuture(HTTPClientError.invalidProxyResponse) |
| 116 | + } |
| 117 | + |
| 118 | + XCTAssertThrowsError(try httpClient.post(url: "http://localhost:\(httpBin.port)/post", body: body).wait()) |
| 119 | + |
| 120 | + body = .stream(length: 50) { _ in |
| 121 | + do { |
| 122 | + var request = try Request(url: "http://localhost:\(httpBin.port)/events/10/1") |
| 123 | + request.headers.add(name: "Accept", value: "text/event-stream") |
| 124 | + |
| 125 | + let delegate = CopyingDelegate { _ in |
| 126 | + httpClient.eventLoopGroup.next().makeFailedFuture(HTTPClientError.invalidProxyResponse) |
| 127 | + } |
| 128 | + return httpClient.execute(request: request, delegate: delegate).futureResult |
| 129 | + } catch { |
| 130 | + return httpClient.eventLoopGroup.next().makeFailedFuture(error) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + XCTAssertThrowsError(try httpClient.post(url: "http://localhost:\(httpBin.port)/post", body: body).wait()) |
| 135 | + } |
| 136 | + |
| 137 | + func testUploadStreamingBackpressure() throws { |
| 138 | + class BackpressureTestDelegate: HTTPClientResponseDelegate { |
| 139 | + typealias Response = Void |
| 140 | + |
| 141 | + var _reads = 0 |
| 142 | + let lock: Lock |
| 143 | + let promise: EventLoopPromise<Void> |
| 144 | + |
| 145 | + init(promise: EventLoopPromise<Void>) { |
| 146 | + self.lock = Lock() |
| 147 | + self.promise = promise |
| 148 | + } |
| 149 | + |
| 150 | + var reads: Int { |
| 151 | + return self.lock.withLock { |
| 152 | + self._reads |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + func didReceivePart(task: HTTPClient.Task<Response>, _ buffer: ByteBuffer) -> EventLoopFuture<Void> { |
| 157 | + self.lock.withLockVoid { |
| 158 | + self._reads += 1 |
| 159 | + } |
| 160 | + return self.promise.futureResult |
| 161 | + } |
| 162 | + |
| 163 | + func didFinishRequest(task: HTTPClient.Task<Response>) throws {} |
| 164 | + } |
| 165 | + |
| 166 | + let httpClient = HTTPClient(eventLoopGroupProvider: .createNew) |
| 167 | + let promise: EventLoopPromise<Channel> = httpClient.eventLoopGroup.next().makePromise() |
| 168 | + let httpBin = HttpBin(channelPromise: promise) |
| 169 | + |
| 170 | + defer { |
| 171 | + try! httpClient.syncShutdown() |
| 172 | + httpBin.shutdown() |
| 173 | + } |
| 174 | + |
| 175 | + let request = try Request(url: "http://localhost:\(httpBin.port)/custom") |
| 176 | + let delegate = BackpressureTestDelegate(promise: httpClient.eventLoopGroup.next().makePromise()) |
| 177 | + let future = httpClient.execute(request: request, delegate: delegate).futureResult |
| 178 | + |
| 179 | + let channel = try promise.futureResult.wait() |
| 180 | + |
| 181 | + // Send 3 parts, but only one should be received until the future is complete |
| 182 | + let buffer = ByteBuffer.of(string: "1234") |
| 183 | + try channel.writeAndFlush(HTTPServerResponsePart.body(.byteBuffer(buffer))).wait() |
| 184 | + try channel.writeAndFlush(HTTPServerResponsePart.body(.byteBuffer(buffer))).wait() |
| 185 | + try channel.writeAndFlush(HTTPServerResponsePart.body(.byteBuffer(buffer))).wait() |
| 186 | + |
| 187 | + XCTAssertEqual(delegate.reads, 1) |
| 188 | + |
| 189 | + delegate.promise.succeed(()) |
| 190 | + |
| 191 | + try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait() |
| 192 | + try future.wait() |
| 193 | + |
| 194 | + XCTAssertEqual(delegate.reads, 3) |
| 195 | + } |
| 196 | +} |
| 197 | + |
0 commit comments