|
| 1 | +@testable import HTTPServer |
| 2 | +import HTTPTypes |
| 3 | +import NIOCore |
| 4 | +import NIOHTTP1 |
| 5 | +import NIOHTTPTypes |
| 6 | +import NIOPosix |
| 7 | +import Testing |
| 8 | + |
| 9 | +@Suite |
| 10 | +struct HTTPRequestConcludingAsyncReaderTests { |
| 11 | + @Test("Head request not allowed") |
| 12 | + @available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) |
| 13 | + func testWriteHeadRequestPartFatalError() async throws { |
| 14 | + // The request body reader should fatal error if it receives a head part |
| 15 | + await #expect(processExitsWith: .failure) { |
| 16 | + let (stream, source) = NIOAsyncChannelInboundStream<HTTPRequestPart>.makeTestingStream() |
| 17 | + |
| 18 | + // Write just a request head |
| 19 | + source.yield(.head(.init(method: .get, scheme: "http", authority: "", path: ""))) |
| 20 | + source.finish() |
| 21 | + |
| 22 | + let requestReader = HTTPRequestConcludingAsyncReader( |
| 23 | + iterator: stream.makeAsyncIterator(), |
| 24 | + readerState: .init() |
| 25 | + ) |
| 26 | + |
| 27 | + _ = try await requestReader.consumeAndConclude { bodyReader in |
| 28 | + var bodyReader = bodyReader |
| 29 | + try await bodyReader.read { element in () } |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + @Test( |
| 35 | + "Request with concluding element", |
| 36 | + arguments: [ByteBuffer(repeating: 1, count: 100), ByteBuffer()], |
| 37 | + [ |
| 38 | + HTTPFields([.init(name: .cookie, value: "test_cookie")]), |
| 39 | + HTTPFields([.init(name: .cookie, value: "first_cookie"), .init(name: .cookie, value: "second_cookie")]) |
| 40 | + ] |
| 41 | + ) |
| 42 | + @available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) |
| 43 | + func testRequestWithConcludingElement(body: ByteBuffer, trailers: HTTPFields) async throws { |
| 44 | + let (stream, source) = NIOAsyncChannelInboundStream<HTTPRequestPart>.makeTestingStream() |
| 45 | + |
| 46 | + // First write the request |
| 47 | + source.yield(.body(body)) |
| 48 | + source.yield(.end(trailers)) |
| 49 | + source.finish() |
| 50 | + |
| 51 | + // Then start reading the request |
| 52 | + let requestReader = HTTPRequestConcludingAsyncReader(iterator: stream.makeAsyncIterator(), readerState: .init()) |
| 53 | + let (requestBody, finalElement) = try await requestReader.consumeAndConclude { bodyReader in |
| 54 | + var bodyReader = bodyReader |
| 55 | + |
| 56 | + var buffer = ByteBuffer() |
| 57 | + // Read just once: we only sent one body chunk |
| 58 | + try await bodyReader.read { element in |
| 59 | + if let element { |
| 60 | + buffer.writeBytes(element.bytes) |
| 61 | + } else { |
| 62 | + Issue.record("Unexpectedly failed to read the client's request body") |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Attempting to read again should result in a `nil` element (we only sent one body chunk) |
| 67 | + try await bodyReader.read { element in |
| 68 | + if element != nil { |
| 69 | + Issue.record("Received a non-nil value after the request body was completely read") |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return buffer |
| 74 | + } |
| 75 | + |
| 76 | + #expect(requestBody == body) |
| 77 | + #expect(finalElement == trailers) |
| 78 | + } |
| 79 | + |
| 80 | + @Test( |
| 81 | + "Streamed request with concluding element", |
| 82 | + arguments: [ |
| 83 | + (0..<10).map { i in ByteBuffer() }, // 10 empty ByteBuffers |
| 84 | + (0..<100).map { i in ByteBuffer(bytes: [i]) } // 100 single-byte ByteBuffers |
| 85 | + ], |
| 86 | + [ |
| 87 | + HTTPFields([.init(name: .cookie, value: "test")]), |
| 88 | + HTTPFields([.init(name: .cookie, value: "first_cookie"), .init(name: .cookie, value: "second_cookie")]), |
| 89 | + ] |
| 90 | + ) |
| 91 | + @available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) |
| 92 | + func testStreamedRequestBody(bodyChunks: [ByteBuffer], trailers: HTTPFields) async throws { |
| 93 | + let (stream, source) = NIOAsyncChannelInboundStream<HTTPRequestPart>.makeTestingStream() |
| 94 | + |
| 95 | + // Execute the writer and reader tasks concurrently |
| 96 | + await withThrowingTaskGroup { group in |
| 97 | + group.addTask { |
| 98 | + for chunk in bodyChunks { |
| 99 | + source.yield(.body(chunk)) |
| 100 | + } |
| 101 | + source.yield(.end(trailers)) |
| 102 | + source.finish() |
| 103 | + } |
| 104 | + |
| 105 | + group.addTask { |
| 106 | + let requestReader = HTTPRequestConcludingAsyncReader( |
| 107 | + iterator: stream.makeAsyncIterator(), |
| 108 | + readerState: .init() |
| 109 | + ) |
| 110 | + let finalElement = try await requestReader.consumeAndConclude { bodyReader in |
| 111 | + var bodyReader = bodyReader |
| 112 | + |
| 113 | + for chunk in bodyChunks { |
| 114 | + try await bodyReader.read { element in |
| 115 | + if let element { |
| 116 | + var buffer = ByteBuffer() |
| 117 | + buffer.writeBytes(element.bytes) |
| 118 | + #expect(chunk == buffer) |
| 119 | + } else { |
| 120 | + Issue.record("Received a nil element before the request body was completely read") |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + try await bodyReader.read { element in |
| 126 | + if element != nil { |
| 127 | + Issue.record("Received a non-nil element after the request body was completely read") |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + #expect(finalElement == trailers) |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Test("Throw while reading request") |
| 138 | + @available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) |
| 139 | + func testThrowingWhileReadingRequest() async throws { |
| 140 | + let (stream, source) = NIOAsyncChannelInboundStream<HTTPRequestPart>.makeTestingStream() |
| 141 | + |
| 142 | + let bodyChunks = (0..<10).map { i in ByteBuffer(bytes: [i]) } |
| 143 | + for chunk in bodyChunks { |
| 144 | + source.yield(.body(chunk)) |
| 145 | + } |
| 146 | + source.yield(.end([.cookie: "test"])) |
| 147 | + source.finish() |
| 148 | + |
| 149 | + // Check that the read error is propagated |
| 150 | + try await #require(throws: TestError.errorWhileReading) { |
| 151 | + let requestReader = HTTPRequestConcludingAsyncReader( |
| 152 | + iterator: stream.makeAsyncIterator(), |
| 153 | + readerState: .init() |
| 154 | + ) |
| 155 | + |
| 156 | + _ = try await requestReader.consumeAndConclude { bodyReader in |
| 157 | + var bodyReader = bodyReader |
| 158 | + |
| 159 | + try await bodyReader.read { element in |
| 160 | + throw TestError.errorWhileReading |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) |
| 167 | + @Test("More bytes available than consumption limit") |
| 168 | + func testCollectMoreBytesThanAvailable() async throws { |
| 169 | + let (stream, source) = NIOAsyncChannelInboundStream<HTTPRequestPart>.makeTestingStream() |
| 170 | + |
| 171 | + // Write 10 bytes |
| 172 | + source.yield(.body(.init(repeating: 5, count: 10))) |
| 173 | + source.finish() |
| 174 | + |
| 175 | + let requestReader = HTTPRequestConcludingAsyncReader(iterator: stream.makeAsyncIterator(), readerState: .init()) |
| 176 | + |
| 177 | + _ = try await requestReader.consumeAndConclude { requestBodyReader in |
| 178 | + var requestBodyReader = requestBodyReader |
| 179 | + |
| 180 | + // Attempting to collect a maximum of 9 bytes should result in a LimitExceeded error. |
| 181 | + await #expect(throws: LimitExceeded.self) { |
| 182 | + try await requestBodyReader.collect(upTo: 9) { element in |
| 183 | + () |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments