|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Hummingbird server framework project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 the Hummingbird authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import HummingbirdCore |
| 16 | +import NIOCore |
| 17 | +import NIOHTTPTypes |
| 18 | +import XCTest |
| 19 | + |
| 20 | +final class RequestBodyTests: XCTestCase { |
| 21 | + func testSingleRequestBody() async throws { |
| 22 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 23 | + let (httpSource, httpStream) = NIOAsyncChannelInboundStream<HTTPRequestPart>.makeTestingStream() |
| 24 | + let httpSourceIterator = httpSource.makeAsyncIterator() |
| 25 | + let requestBody = RequestBody(nioAsyncChannelInbound: .init(iterator: httpSourceIterator)) |
| 26 | + group.addTask { |
| 27 | + httpStream.yield(.body(ByteBuffer(string: "hello "))) |
| 28 | + httpStream.yield(.body(ByteBuffer(string: "world"))) |
| 29 | + httpStream.yield(.end(nil)) |
| 30 | + httpStream.finish() |
| 31 | + } |
| 32 | + group.addTask { |
| 33 | + let buffer = try await requestBody.collect(upTo: .max) |
| 34 | + XCTAssertEqual(String(buffer: buffer), "hello world") |
| 35 | + } |
| 36 | + try await group.waitForAll() |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + func testMultipleRequestBodies() async throws { |
| 41 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 42 | + let (httpSource, httpStream) = NIOAsyncChannelInboundStream<HTTPRequestPart>.makeTestingStream() |
| 43 | + let httpSourceIterator = httpSource.makeAsyncIterator() |
| 44 | + let requestBody = RequestBody(nioAsyncChannelInbound: .init(iterator: httpSourceIterator)) |
| 45 | + group.addTask { |
| 46 | + httpStream.yield(.body(ByteBuffer(string: "hello "))) |
| 47 | + httpStream.yield(.body(ByteBuffer(string: "world"))) |
| 48 | + httpStream.yield(.end(nil)) |
| 49 | + httpStream.yield(.head(.init(method: .get, scheme: nil, authority: nil, path: "/test"))) |
| 50 | + httpStream.yield(.end(nil)) |
| 51 | + httpStream.finish() |
| 52 | + } |
| 53 | + group.addTask { |
| 54 | + let buffer = try await requestBody.collect(upTo: .max) |
| 55 | + XCTAssertEqual(String(buffer: buffer), "hello world") |
| 56 | + } |
| 57 | + try await group.waitForAll() |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + #if compiler(>=6.0) |
| 62 | + func testInboundClosureParsingStream() async throws { |
| 63 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 64 | + let (httpSource, httpStream) = NIOAsyncChannelInboundStream<HTTPRequestPart>.makeTestingStream() |
| 65 | + let httpSourceIterator = httpSource.makeAsyncIterator() |
| 66 | + let requestBody = RequestBody(nioAsyncChannelInbound: .init(iterator: httpSourceIterator)) |
| 67 | + let (stream, cont) = AsyncStream.makeStream(of: Void.self) |
| 68 | + group.addTask { |
| 69 | + httpStream.yield(.body(ByteBuffer(string: "hello "))) |
| 70 | + httpStream.yield(.body(ByteBuffer(string: "world"))) |
| 71 | + httpStream.yield(.end(nil)) |
| 72 | + httpStream.finish() |
| 73 | + } |
| 74 | + group.addTask { |
| 75 | + try await requestBody.consumeWithInboundCloseHandler { requestBody in |
| 76 | + let buffer = try await requestBody.collect(upTo: .max) |
| 77 | + XCTAssertEqual(String(buffer: buffer), "hello world") |
| 78 | + await stream.first { _ in true } |
| 79 | + } onInboundClosed: { |
| 80 | + cont.yield() |
| 81 | + } |
| 82 | + } |
| 83 | + try await group.waitForAll() |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + func testInboundClosureWithoutParsingStream() async throws { |
| 88 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 89 | + let (httpSource, httpStream) = NIOAsyncChannelInboundStream<HTTPRequestPart>.makeTestingStream() |
| 90 | + let httpSourceIterator = httpSource.makeAsyncIterator() |
| 91 | + let requestBody = RequestBody(nioAsyncChannelInbound: .init(iterator: httpSourceIterator)) |
| 92 | + let (stream, cont) = AsyncStream.makeStream(of: Void.self) |
| 93 | + group.addTask { |
| 94 | + httpStream.yield(.body(ByteBuffer(string: "hello "))) |
| 95 | + httpStream.yield(.body(ByteBuffer(string: "world"))) |
| 96 | + httpStream.yield(.end(nil)) |
| 97 | + httpStream.finish() |
| 98 | + } |
| 99 | + group.addTask { |
| 100 | + try await requestBody.consumeWithInboundCloseHandler { requestBody in |
| 101 | + await stream.first { _ in true } |
| 102 | + } onInboundClosed: { |
| 103 | + cont.yield() |
| 104 | + } |
| 105 | + } |
| 106 | + try await group.waitForAll() |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + func testInboundClosureWithStreamError() async throws { |
| 111 | + struct TestError: Error {} |
| 112 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 113 | + let (httpSource, httpStream) = NIOAsyncChannelInboundStream<HTTPRequestPart>.makeTestingStream() |
| 114 | + let httpSourceIterator = httpSource.makeAsyncIterator() |
| 115 | + let requestBody = RequestBody(nioAsyncChannelInbound: .init(iterator: httpSourceIterator)) |
| 116 | + let (stream, cont) = AsyncStream.makeStream(of: Void.self) |
| 117 | + group.addTask { |
| 118 | + httpStream.yield(.body(ByteBuffer(string: "hello "))) |
| 119 | + httpStream.yield(.end(nil)) |
| 120 | + httpStream.finish(throwing: TestError()) |
| 121 | + } |
| 122 | + group.addTask { |
| 123 | + try await requestBody.consumeWithInboundCloseHandler { requestBody in |
| 124 | + await stream.first { _ in true } |
| 125 | + } onInboundClosed: { |
| 126 | + cont.yield() |
| 127 | + } |
| 128 | + } |
| 129 | + try await group.waitForAll() |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + func testInboundClosureWithStreamErrorIsPassedOn() async throws { |
| 134 | + struct TestError: Error {} |
| 135 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 136 | + let (httpSource, httpStream) = NIOAsyncChannelInboundStream<HTTPRequestPart>.makeTestingStream() |
| 137 | + let httpSourceIterator = httpSource.makeAsyncIterator() |
| 138 | + let requestBody = RequestBody(nioAsyncChannelInbound: .init(iterator: httpSourceIterator)) |
| 139 | + let (stream, cont) = AsyncStream.makeStream(of: Void.self) |
| 140 | + group.addTask { |
| 141 | + httpStream.yield(.body(ByteBuffer(string: "hello "))) |
| 142 | + httpStream.yield(.body(ByteBuffer(string: "world"))) |
| 143 | + httpStream.finish(throwing: TestError()) |
| 144 | + } |
| 145 | + group.addTask { |
| 146 | + try await requestBody.consumeWithInboundCloseHandler { requestBody in |
| 147 | + do { |
| 148 | + _ = try await requestBody.collect(upTo: .max) |
| 149 | + XCTFail("Should not get here") |
| 150 | + } catch is TestError { |
| 151 | + // |
| 152 | + } |
| 153 | + await stream.first { _ in true } |
| 154 | + } onInboundClosed: { |
| 155 | + cont.yield() |
| 156 | + } |
| 157 | + } |
| 158 | + try await group.waitForAll() |
| 159 | + } |
| 160 | + } |
| 161 | + #endif // compiler(>=6.0) |
| 162 | +} |
0 commit comments