Skip to content

Commit 8cb1887

Browse files
committed
Add unit tests
1 parent 29b83ab commit 8cb1887

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

tests/Unit/HttpWorkerTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use PHPUnit\Framework\Attributes\DataProvider;
88
use PHPUnit\Framework\TestCase;
99
use RoadRunner\HTTP\DTO\V1\HeaderValue;
10+
use RoadRunner\HTTP\DTO\V1\Response;
11+
use Spiral\Goridge\Frame;
1012
use Spiral\RoadRunner\Http\HttpWorker;
1113
use Spiral\RoadRunner\Http\Request;
1214
use Spiral\RoadRunner\Payload;
@@ -75,6 +77,64 @@ public function testWaitRequestWithEmptyData(?Payload $payload): void
7577
$this->assertEquals(null, $worker->waitRequest());
7678
}
7779

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+
78138
public static function requestDataProvider(): \Traversable
79139
{
80140
yield [self::REQUIRED_PAYLOAD_DATA, self::REQUIRED_REQUEST_DATA];

0 commit comments

Comments
 (0)