|
7 | 7 | use PHPUnit\Framework\Attributes\DataProvider; |
8 | 8 | use PHPUnit\Framework\TestCase; |
9 | 9 | use RoadRunner\HTTP\DTO\V1\HeaderValue; |
| 10 | +use RoadRunner\HTTP\DTO\V1\Response; |
| 11 | +use Spiral\Goridge\Frame; |
10 | 12 | use Spiral\RoadRunner\Http\HttpWorker; |
11 | 13 | use Spiral\RoadRunner\Http\Request; |
12 | 14 | use Spiral\RoadRunner\Payload; |
@@ -75,6 +77,64 @@ public function testWaitRequestWithEmptyData(?Payload $payload): void |
75 | 77 | $this->assertEquals(null, $worker->waitRequest()); |
76 | 78 | } |
77 | 79 |
|
| 80 | + public function testEmptyBodyShouldBeConvertedIntoEmptyArrayWithParsedTrue(): void |
| 81 | + { |
| 82 | + $request = self::createProtoRequest(\array_merge(self::REQUIRED_REQUEST_DATA, ['parsed' => true])); |
| 83 | + |
| 84 | + $worker = $this->createMock(WorkerInterface::class); |
| 85 | + $worker->expects($this->once()) |
| 86 | + ->method('waitPayload') |
| 87 | + ->willReturn(new Payload('', $request->serializeToString())); |
| 88 | + |
| 89 | + $worker = new HttpWorker($worker); |
| 90 | + |
| 91 | + $request = $worker->waitRequest(); |
| 92 | + $this->assertSame([], $request->getParsedBody()); |
| 93 | + } |
| 94 | + |
| 95 | + public function testRespondUnableToSendBodyWithInfoStatusException(): void |
| 96 | + { |
| 97 | + $worker = new HttpWorker($this->createMock(WorkerInterface::class)); |
| 98 | + |
| 99 | + $this->expectException(\InvalidArgumentException::class); |
| 100 | + $this->expectExceptionMessage('Unable to send a body with informational status code.'); |
| 101 | + $worker->respond(100, 'foo'); |
| 102 | + } |
| 103 | + |
| 104 | + public function testRespondWithProtoCodec(): void |
| 105 | + { |
| 106 | + $expectedHeader = new Response([ |
| 107 | + 'status' => 200, |
| 108 | + 'headers' => ['Content-Type' => new HeaderValue(['value' => ['application/x-www-form-urlencoded']])], |
| 109 | + ]); |
| 110 | + |
| 111 | + $worker = $this->createMock(WorkerInterface::class); |
| 112 | + $worker->expects($this->once()) |
| 113 | + ->method('respond') |
| 114 | + ->with(new Payload('foo', $expectedHeader->serializeToString()), Frame::CODEC_PROTO); |
| 115 | + |
| 116 | + (new \ReflectionProperty(HttpWorker::class, 'codec'))->setValue(Frame::CODEC_PROTO); |
| 117 | + $worker = new HttpWorker($worker); |
| 118 | + |
| 119 | + $worker->respond(200, 'foo', ['Content-Type' => ['application/x-www-form-urlencoded']]); |
| 120 | + } |
| 121 | + |
| 122 | + public function testRespondWithJsonCodec(): void |
| 123 | + { |
| 124 | + $worker = $this->createMock(WorkerInterface::class); |
| 125 | + $worker->expects($this->once()) |
| 126 | + ->method('respond') |
| 127 | + ->with(new Payload('foo', \json_encode([ |
| 128 | + 'status' => 200, |
| 129 | + 'headers' => ['Content-Type' => ['application/x-www-form-urlencoded']] |
| 130 | + ])), Frame::CODEC_JSON); |
| 131 | + |
| 132 | + (new \ReflectionProperty(HttpWorker::class, 'codec'))->setValue(Frame::CODEC_JSON); |
| 133 | + $worker = new HttpWorker($worker); |
| 134 | + |
| 135 | + $worker->respond(200, 'foo', ['Content-Type' => ['application/x-www-form-urlencoded']]); |
| 136 | + } |
| 137 | + |
78 | 138 | public static function requestDataProvider(): \Traversable |
79 | 139 | { |
80 | 140 | yield [self::REQUIRED_PAYLOAD_DATA, self::REQUIRED_REQUEST_DATA]; |
|
0 commit comments