|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Spiral\RoadRunner\Tests\Http\Unit; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use RoadRunner\HTTP\DTO\V1\HeaderValue; |
| 10 | +use Spiral\RoadRunner\Http\HttpWorker; |
| 11 | +use Spiral\RoadRunner\Http\Request; |
| 12 | +use Spiral\RoadRunner\Payload; |
| 13 | +use Spiral\RoadRunner\WorkerInterface; |
| 14 | + |
| 15 | +final class HttpWorkerTest extends TestCase |
| 16 | +{ |
| 17 | + private const REQUIRED_PAYLOAD_DATA = [ |
| 18 | + 'rawQuery' => 'first=value&arr[]=foo+bar&arr[]=baz', |
| 19 | + 'remoteAddr' => '127.0.0.1', |
| 20 | + 'protocol' => 'HTTP/1.1', |
| 21 | + 'method' => 'GET', |
| 22 | + 'uri' => 'http://localhost', |
| 23 | + 'parsed' => false, |
| 24 | + ]; |
| 25 | + |
| 26 | + private const REQUIRED_REQUEST_DATA = [ |
| 27 | + 'remoteAddr' => '127.0.0.1', |
| 28 | + 'protocol' => 'HTTP/1.1', |
| 29 | + 'method' => 'GET', |
| 30 | + 'uri' => 'http://localhost', |
| 31 | + 'attributes' => [Request::PARSED_BODY_ATTRIBUTE_NAME => false], |
| 32 | + 'query' => ['first' => 'value', 'arr' => ['foo bar', 'baz']], |
| 33 | + 'parsed' => false |
| 34 | + ]; |
| 35 | + |
| 36 | + #[DataProvider('requestDataProvider')] |
| 37 | + public function testWaitRequestFromArray(array $header, array $expected): void |
| 38 | + { |
| 39 | + $worker = $this->createMock(WorkerInterface::class); |
| 40 | + $worker->expects($this->once()) |
| 41 | + ->method('waitPayload') |
| 42 | + ->willReturn(new Payload(null, \json_encode($header))); |
| 43 | + |
| 44 | + $worker = new HttpWorker($worker); |
| 45 | + |
| 46 | + $this->assertEquals(new Request(...$expected), $worker->waitRequest()); |
| 47 | + } |
| 48 | + |
| 49 | + #[DataProvider('requestDataProvider')] |
| 50 | + public function testWaitRequestFromProto(array $header, array $expected): void |
| 51 | + { |
| 52 | + $request = self::createProtoRequest($header); |
| 53 | + |
| 54 | + $worker = $this->createMock(WorkerInterface::class); |
| 55 | + $worker->expects($this->once()) |
| 56 | + ->method('waitPayload') |
| 57 | + ->willReturn(new Payload(null, $request->serializeToString())); |
| 58 | + |
| 59 | + $worker = new HttpWorker($worker); |
| 60 | + |
| 61 | + $this->assertEquals(new Request(...$expected), $worker->waitRequest()); |
| 62 | + } |
| 63 | + |
| 64 | + #[DataProvider('emptyRequestDataProvider')] |
| 65 | + public function testWaitRequestWithEmptyData(?Payload $payload): void |
| 66 | + { |
| 67 | + $worker = $this->createMock(WorkerInterface::class); |
| 68 | + $worker->expects($this->once()) |
| 69 | + ->method('waitPayload') |
| 70 | + ->willReturn($payload); |
| 71 | + |
| 72 | + $worker = new HttpWorker($worker); |
| 73 | + |
| 74 | + $this->assertEquals(null, $worker->waitRequest()); |
| 75 | + } |
| 76 | + |
| 77 | + public static function requestDataProvider(): \Traversable |
| 78 | + { |
| 79 | + yield [self::REQUIRED_PAYLOAD_DATA, self::REQUIRED_REQUEST_DATA]; |
| 80 | + yield [ |
| 81 | + \array_merge(self::REQUIRED_PAYLOAD_DATA, ['parsed' => true]), |
| 82 | + \array_merge( |
| 83 | + self::REQUIRED_REQUEST_DATA, |
| 84 | + ['parsed' => true, 'attributes' => [Request::PARSED_BODY_ATTRIBUTE_NAME => true]] |
| 85 | + ) |
| 86 | + ]; |
| 87 | + yield [ |
| 88 | + \array_merge(self::REQUIRED_PAYLOAD_DATA, [ |
| 89 | + 'headers' => [ |
| 90 | + 'Content-Type' => ['application/x-www-form-urlencoded'], |
| 91 | + 111 => ['invalid-non-string-key'], |
| 92 | + '' => ['invalid-empty-string-key'], |
| 93 | + ], |
| 94 | + ]), |
| 95 | + \array_merge(self::REQUIRED_REQUEST_DATA, [ |
| 96 | + 'headers' => ['Content-Type' => ['application/x-www-form-urlencoded']], |
| 97 | + ]) |
| 98 | + ]; |
| 99 | + yield [ |
| 100 | + \array_merge(self::REQUIRED_PAYLOAD_DATA, [ |
| 101 | + 'cookies' => [ |
| 102 | + 'theme' => 'light', |
| 103 | + ], |
| 104 | + ]), |
| 105 | + \array_merge(self::REQUIRED_REQUEST_DATA, [ |
| 106 | + 'cookies' => ['theme' => 'light'], |
| 107 | + ]) |
| 108 | + ]; |
| 109 | + yield [ |
| 110 | + \array_merge(self::REQUIRED_PAYLOAD_DATA, [ |
| 111 | + 'uploads' => [ |
| 112 | + 'single-file' => [ |
| 113 | + 'name' => 'test.png', |
| 114 | + 'mime' => 'image/png', |
| 115 | + 'size' => 123, |
| 116 | + 'error' => 0, |
| 117 | + 'tmpName' => '/tmp/php/php1h4j1o', |
| 118 | + ], |
| 119 | + 'multiple' => [ |
| 120 | + [ |
| 121 | + 'name' => 'test.png', |
| 122 | + 'mime' => 'image/png', |
| 123 | + 'size' => 123, |
| 124 | + 'error' => 0, |
| 125 | + 'tmpName' => '/tmp/php/php1h4j1o', |
| 126 | + ], |
| 127 | + [ |
| 128 | + 'name' => 'test2.jpg', |
| 129 | + 'mime' => 'image/jpeg', |
| 130 | + 'size' => 1235, |
| 131 | + 'error' => 0, |
| 132 | + 'tmpName' => '/tmp/php/php2h4j1o', |
| 133 | + ] |
| 134 | + ], |
| 135 | + 'nested' => [ |
| 136 | + 'some-key' => [ |
| 137 | + 'name' => 'test.png', |
| 138 | + 'mime' => 'image/png', |
| 139 | + 'size' => 123, |
| 140 | + 'error' => 0, |
| 141 | + 'tmpName' => '/tmp/php/php1h4j1o', |
| 142 | + ], |
| 143 | + ] |
| 144 | + ], |
| 145 | + ]), |
| 146 | + \array_merge(self::REQUIRED_REQUEST_DATA, [ |
| 147 | + 'uploads' => [ |
| 148 | + 'single-file' => [ |
| 149 | + 'name' => 'test.png', |
| 150 | + 'mime' => 'image/png', |
| 151 | + 'size' => 123, |
| 152 | + 'error' => 0, |
| 153 | + 'tmpName' => '/tmp/php/php1h4j1o', |
| 154 | + ], |
| 155 | + 'multiple' => [ |
| 156 | + [ |
| 157 | + 'name' => 'test.png', |
| 158 | + 'mime' => 'image/png', |
| 159 | + 'size' => 123, |
| 160 | + 'error' => 0, |
| 161 | + 'tmpName' => '/tmp/php/php1h4j1o', |
| 162 | + ], |
| 163 | + [ |
| 164 | + 'name' => 'test2.jpg', |
| 165 | + 'mime' => 'image/jpeg', |
| 166 | + 'size' => 1235, |
| 167 | + 'error' => 0, |
| 168 | + 'tmpName' => '/tmp/php/php2h4j1o', |
| 169 | + ] |
| 170 | + ], |
| 171 | + 'nested' => [ |
| 172 | + 'some-key' => [ |
| 173 | + 'name' => 'test.png', |
| 174 | + 'mime' => 'image/png', |
| 175 | + 'size' => 123, |
| 176 | + 'error' => 0, |
| 177 | + 'tmpName' => '/tmp/php/php1h4j1o', |
| 178 | + ], |
| 179 | + ] |
| 180 | + ], |
| 181 | + ]) |
| 182 | + ]; |
| 183 | + yield [ |
| 184 | + \array_merge(self::REQUIRED_PAYLOAD_DATA, [ |
| 185 | + 'attributes' => [ |
| 186 | + 'foo' => 'bar', |
| 187 | + ], |
| 188 | + ]), |
| 189 | + \array_merge(self::REQUIRED_REQUEST_DATA, [ |
| 190 | + 'attributes' => [ |
| 191 | + Request::PARSED_BODY_ATTRIBUTE_NAME => false, |
| 192 | + 'foo' => 'bar' |
| 193 | + ], |
| 194 | + ]) |
| 195 | + ]; |
| 196 | + } |
| 197 | + |
| 198 | + public static function emptyRequestDataProvider(): \Traversable |
| 199 | + { |
| 200 | + yield [null]; |
| 201 | + yield [new Payload(null, null)]; |
| 202 | + } |
| 203 | + |
| 204 | + private static function createProtoRequest(array $values): \RoadRunner\HTTP\DTO\V1\Request |
| 205 | + { |
| 206 | + $toHeaderValue = static function (string $key, bool $wrap = true) use (&$values): void { |
| 207 | + if (isset($values[$key])) { |
| 208 | + foreach ($values[$key] as $valueKey => $value) { |
| 209 | + $values[$key][$valueKey] = new HeaderValue(['value' => $wrap ? [$value] : $value]); |
| 210 | + } |
| 211 | + } |
| 212 | + }; |
| 213 | + |
| 214 | + $toHeaderValue('headers', false); |
| 215 | + $toHeaderValue('attributes'); |
| 216 | + $toHeaderValue('cookies'); |
| 217 | + |
| 218 | + return new \RoadRunner\HTTP\DTO\V1\Request([ |
| 219 | + 'remote_addr' => $values['remoteAddr'], |
| 220 | + 'protocol' => $values['protocol'], |
| 221 | + 'method' => $values['method'], |
| 222 | + 'uri' => $values['uri'], |
| 223 | + 'header' => $values['headers'] ?? [], |
| 224 | + 'cookies' => $values['cookies'] ?? [], |
| 225 | + 'raw_query' => $values['rawQuery'], |
| 226 | + 'parsed' => $values['parsed'], |
| 227 | + 'uploads' => \json_encode($values['uploads'] ?? []), |
| 228 | + 'attributes' => $values['attributes'] ?? [], |
| 229 | + ]); |
| 230 | + } |
| 231 | + |
| 232 | + protected function tearDown(): void |
| 233 | + { |
| 234 | + (new \ReflectionProperty(HttpWorker::class, 'codec'))->setValue(null); |
| 235 | + } |
| 236 | +} |
0 commit comments