From 4b87c0f359835406184ac04f31563f8e29d9f883 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sun, 15 Dec 2024 11:14:18 -0600 Subject: [PATCH] Add tests for delayed request handler --- test/Driver/Http1DriverTest.php | 31 +++++++++++++++++++++++++ test/Driver/Http2DriverTest.php | 41 ++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/test/Driver/Http1DriverTest.php b/test/Driver/Http1DriverTest.php index 62cb0a45..27642492 100644 --- a/test/Driver/Http1DriverTest.php +++ b/test/Driver/Http1DriverTest.php @@ -1178,4 +1178,35 @@ public function testSwitchingProtocolsUpgrade(): void self::assertStringStartsWith("HTTP/1.1 101 Switching Protocols\r\n", $output->buffer()); } + + public function testTimeoutSuspendedDuringRequestHandler(): void + { + $requestHandler = new ClosureRequestHandler(function (): Response { + delay(2); + return new Response(HttpStatus::ACCEPTED, body: 'Hello World!'); + }); + + $driver = new Http1Driver( + $requestHandler, + $this->createMock(ErrorHandler::class), + new NullLogger, + connectionTimeout: 1, + ); + + $client = $this->createClientMock(); + $client->expects(self::never()) + ->method('close'); + + $output = new WritableBuffer(); + + $driver->handleClient( + $client, + new ReadableBuffer("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"), + $output, + ); + + $output->close(); + + self::assertStringStartsWith('HTTP/1.1 202', $output->buffer()); + } } diff --git a/test/Driver/Http2DriverTest.php b/test/Driver/Http2DriverTest.php index 7a86c4de..7fdb155d 100644 --- a/test/Driver/Http2DriverTest.php +++ b/test/Driver/Http2DriverTest.php @@ -521,7 +521,6 @@ public function testFlowControl(): void self::markTestSkipped('Not supported with nghttp2, disable ffi for this test.'); } - $this->initDriver(); $request = async(fn () => $this->whenRequestIsReceived()); $input = new Queue; @@ -868,6 +867,46 @@ public function testSendingLargeHeaders(): void self::assertSame($value, $request->getHeader($header)); } + public function testTimeoutSuspendedDuringRequestHandler(): void + { + $requestHandler = new ClosureRequestHandler(function (): Response { + delay(2); + return new Response(HttpStatus::ACCEPTED, body: 'Hello World!'); + }); + + $this->driver = new Http2Driver( + $requestHandler, + $this->createMock(ErrorHandler::class), + new NullLogger, + streamTimeout: 1, + connectionTimeout: 1, + ); + + $headers = [ + ":authority" => ["localhost:8888"], + ":path" => ["/"], + ":scheme" => ["https"], + ":method" => ["GET"], + ]; + + $input = new Queue(); + $this->givenInput(new ReadableIterableStream($input->iterate())); + $frames = $this->whenReceivingFrames(); + + $input->push(Http2Parser::PREFACE); + $input->push(self::packHeader($headers)); + + $frames->continue(); // Skip settings frame. + + self::assertTrue($frames->continue()); + $frame = $frames->getValue(); + self::assertSame(Http2Parser::HEADERS, $frame['type']); + self::assertSame(Http2Parser::END_HEADERS, $frame['flags']); + self::assertSame(1, $frame['stream']); + + $input->complete(); + } + protected function givenPush(string $uri): void { $this->pushes[] = $uri;