Repository: apple/swift-nio
File: Tests/NIOHTTP1Tests/HTTPServerPipelineHandlerTest.swift (Swift)

Code before:
        }
    }

    func testServerRespondToNothing() throws {
        self.pipelineHandler.failOnPreconditions = false

        // Writing an end whilst in state idle is an error
        XCTAssertThrowsError(try self.channel.writeOutbound(HTTPServerResponsePart.end(nil))) { error in
            XCTAssertEqual(error as? HTTPServerPipelineHandler.ConnectionStateError, .preconditionViolated(message: "Unexpectedly received a response in state idle"))
        }

        // Calling finish surfaces the error again
        XCTAssertThrowsError(try self.channel.finish()) { error in
            XCTAssertEqual(error as? HTTPServerPipelineHandler.ConnectionStateError, .preconditionViolated(message: "Unexpectedly received a response in state idle"))
        }
    }

    func testServerRequestEndFirstIsError() throws {
        self.pipelineHandler.failOnPreconditions = false
        // End sending a request which was never startedq
        XCTAssertThrowsError(try self.channel.writeInbound(HTTPServerRequestPart.end(nil))) { error in
            XCTAssertEqual(error as? HTTPServerPipelineHandler.ConnectionStateError, .preconditionViolated(message: "Received second request"))
        }
    }

    func testForcefulShutdownWhenViolatedPrecondition() async throws {
        self.pipelineHandler.failOnPreconditions = false

        // End sending a request which was never started
        XCTAssertThrowsError(try self.channel.writeInbound(HTTPServerRequestPart.end(nil))) { error in
            XCTAssertEqual(error as? HTTPServerPipelineHandler.ConnectionStateError, .preconditionViolated(message: "Received second request"))
        }
        // The handler should now refuse further io, and forcefully shutdown
        XCTAssertThrowsError(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead))) { error in
            let expectedMessage = "The connection has been forcefully closed because further IO was attempted after a precondition was violated"
            XCTAssertEqual(error as? HTTPServerPipelineHandler.ConnectionStateError, .preconditionViolated(message: expectedMessage))
        }

        self.channel.embeddedEventLoop.run()

        // Ensure the channel is closed soon
        await withTaskGroup(of: Void.self) { group in
            group.addTask {
                do {
                    try await self.channel.closeFuture.get()
                } catch {
                    XCTFail("Close threw an error \(error)")
                }
            }
            group.addTask {
                do {

Review comment (bug):
We cannot use `EmbeddedChannel` in an async test, but we don't need to. EmbeddedChannel runs strictly synchronously, so tests that use it are 100% reliable. This test should either pass or fail.

Code after:
        }
    }

    func testServerRespondToNothing() {
        self.pipelineHandler.failOnPreconditions = false

        // Writing an end whilst in state idle is an error
        XCTAssertThrowsError(try self.channel.writeOutbound(HTTPServerResponsePart.end(nil))) { error in
            XCTAssertEqual(error as? HTTPServerPipelineHandler.ConnectionStateError, .preconditionViolated(message: "Unexpectedly received a response in state idle"))
        }

        // Calling finish surfaces the error again
        XCTAssertThrowsError(try self.channel.finish()) { error in
            XCTAssertEqual(error as? HTTPServerPipelineHandler.ConnectionStateError, .preconditionViolated(message: "Unexpectedly received a response in state idle"))
        }
    }

    func testServerRequestEndFirstIsError() {
        self.pipelineHandler.failOnPreconditions = false
        // End sending a request which was never started
        XCTAssertThrowsError(try self.channel.writeInbound(HTTPServerRequestPart.end(nil))) { error in
            XCTAssertEqual(error as? HTTPServerPipelineHandler.ConnectionStateError, .preconditionViolated(message: "Received second request"))
        }
    }

    func testForcefulShutdownWhenViolatedPrecondition() {
        self.pipelineHandler.failOnPreconditions = false

        // End sending a request which was never started
        XCTAssertThrowsError(try self.channel.writeInbound(HTTPServerRequestPart.end(nil))) { error in
            XCTAssertEqual(error as? HTTPServerPipelineHandler.ConnectionStateError, .preconditionViolated(message: "Received second request"))
        }
        // The handler should now refuse further io, and forcefully shutdown
        XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))

        self.channel.embeddedEventLoop.run()

        // Ensure the channel is closed
        XCTAssertNoThrow(try self.channel.closeFuture.wait())
    }
}