|
| 1 | +<?php |
| 2 | + |
| 3 | +use Farzai\Transport\Request; |
| 4 | +use GuzzleHttp\Psr7\Uri; |
| 5 | +use Psr\Http\Message\StreamInterface; |
| 6 | + |
| 7 | +it('can get the request method', function () { |
| 8 | + $request = new Request('GET', new Uri('https://example.com')); |
| 9 | + |
| 10 | + expect($request->getMethod())->toBe('GET'); |
| 11 | +}); |
| 12 | + |
| 13 | +it('can set the request method', function () { |
| 14 | + $request = new Request('GET', new Uri('https://example.com')); |
| 15 | + |
| 16 | + $newRequest = $request->withMethod('POST'); |
| 17 | + |
| 18 | + expect($newRequest->getMethod())->toBe('POST'); |
| 19 | +}); |
| 20 | + |
| 21 | +it('can get request target', function () { |
| 22 | + $request = new Request('GET', new Uri('https://example.com/foo')); |
| 23 | + |
| 24 | + expect($request->getRequestTarget())->toBe('/foo'); |
| 25 | +}); |
| 26 | + |
| 27 | +it('can set request target', function () { |
| 28 | + $request = new Request('GET', new Uri('https://example.com')); |
| 29 | + |
| 30 | + $newRequest = $request->withRequestTarget('/foo'); |
| 31 | + |
| 32 | + expect($newRequest->getRequestTarget())->toBe('/foo'); |
| 33 | +}); |
| 34 | + |
| 35 | +it('can get the request URI', function () { |
| 36 | + $uri = new Uri('https://example.com'); |
| 37 | + $request = new Request('GET', $uri); |
| 38 | + |
| 39 | + expect($request->getUri())->toBe($uri); |
| 40 | +}); |
| 41 | + |
| 42 | +it('can set the request URI', function () { |
| 43 | + $uri = new Uri('https://example.com'); |
| 44 | + $request = new Request('GET', $uri); |
| 45 | + |
| 46 | + $newUri = new Uri('https://example.org'); |
| 47 | + $newRequest = $request->withUri($newUri); |
| 48 | + |
| 49 | + expect($newRequest->getUri())->toBe($newUri); |
| 50 | +}); |
| 51 | + |
| 52 | +it('can get the request protocol version', function () { |
| 53 | + $request = new Request('GET', new Uri('https://example.com')); |
| 54 | + |
| 55 | + expect($request->getProtocolVersion())->toBe('1.1'); |
| 56 | +}); |
| 57 | + |
| 58 | +it('can set the request protocol version', function () { |
| 59 | + $request = new Request('GET', new Uri('https://example.com')); |
| 60 | + |
| 61 | + $newRequest = $request->withProtocolVersion('2.0'); |
| 62 | + |
| 63 | + expect($newRequest->getProtocolVersion())->toBe('2.0'); |
| 64 | +}); |
| 65 | + |
| 66 | +it('can get the request headers', function () { |
| 67 | + $request = new Request('GET', new Uri('https://example.com'), [ |
| 68 | + 'Content-Type' => 'application/json', |
| 69 | + ]); |
| 70 | + |
| 71 | + expect($request->getHeaders())->toBe([ |
| 72 | + 'Host' => ['example.com'], |
| 73 | + 'Content-Type' => ['application/json'], |
| 74 | + ]); |
| 75 | +}); |
| 76 | + |
| 77 | +it('can set a request header', function () { |
| 78 | + $request = new Request('GET', new Uri('https://example.com')); |
| 79 | + |
| 80 | + $newRequest = $request->withHeader('Content-Type', 'application/json'); |
| 81 | + |
| 82 | + expect($newRequest->getHeaders())->toBe([ |
| 83 | + 'Host' => ['example.com'], |
| 84 | + 'Content-Type' => ['application/json'], |
| 85 | + ]); |
| 86 | +}); |
| 87 | + |
| 88 | +it('can add a request header', function () { |
| 89 | + $request = new Request('GET', new Uri('https://example.com'), [ |
| 90 | + 'Content-Type' => 'application/json', |
| 91 | + ]); |
| 92 | + |
| 93 | + $newRequest = $request->withAddedHeader('Accept', 'application/json'); |
| 94 | + |
| 95 | + expect($newRequest->getHeaders())->toBe([ |
| 96 | + 'Host' => ['example.com'], |
| 97 | + 'Content-Type' => ['application/json'], |
| 98 | + 'Accept' => ['application/json'], |
| 99 | + ]); |
| 100 | +}); |
| 101 | + |
| 102 | +it('can check exists header', function () { |
| 103 | + $request = new Request('GET', new Uri('https://example.com'), [ |
| 104 | + 'Content-Type' => 'application/json', |
| 105 | + ]); |
| 106 | + |
| 107 | + expect($request->hasHeader('Content-Type'))->toBeTrue(); |
| 108 | + expect($request->hasHeader('Accept'))->toBeFalse(); |
| 109 | +}); |
| 110 | + |
| 111 | +it('can get header', function () { |
| 112 | + $request = new Request('GET', new Uri('https://example.com'), [ |
| 113 | + 'Content-Type' => 'application/json', |
| 114 | + ]); |
| 115 | + |
| 116 | + expect($request->getHeader('Content-Type'))->toBe(['application/json']); |
| 117 | + expect($request->getHeader('Accept'))->toBe([]); |
| 118 | +}); |
| 119 | + |
| 120 | +it('can remove a request header', function () { |
| 121 | + $request = new Request('GET', new Uri('https://example.com'), [ |
| 122 | + 'Content-Type' => 'application/json', |
| 123 | + 'Accept' => 'application/json', |
| 124 | + ]); |
| 125 | + |
| 126 | + $newRequest = $request->withoutHeader('Accept'); |
| 127 | + |
| 128 | + expect($newRequest->getHeaders())->toBe([ |
| 129 | + 'Host' => ['example.com'], |
| 130 | + 'Content-Type' => ['application/json'], |
| 131 | + ]); |
| 132 | +}); |
| 133 | + |
| 134 | +it('can get a request header line', function () { |
| 135 | + $request = new Request('GET', new Uri('https://example.com'), [ |
| 136 | + 'Content-Type' => 'application/json', |
| 137 | + ]); |
| 138 | + |
| 139 | + expect($request->getHeaderLine('Content-Type'))->toBe('application/json'); |
| 140 | +}); |
| 141 | + |
| 142 | +it('can set the request body', function () { |
| 143 | + $request = new Request('GET', new Uri('https://example.com')); |
| 144 | + |
| 145 | + $stream = $this->createMock(StreamInterface::class); |
| 146 | + $stream->method('__toString')->willReturn('{"foo":"bar"}'); |
| 147 | + |
| 148 | + $newRequest = $request->withBody($stream); |
| 149 | + |
| 150 | + expect((string) $newRequest->getBody())->toBe('{"foo":"bar"}'); |
| 151 | +}); |
0 commit comments