-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #480 from clue-labs/request
Add `Request` class to represent outgoing HTTP request message
- Loading branch information
Showing
9 changed files
with
149 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace React\Http\Message; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
use Psr\Http\Message\UriInterface; | ||
use React\Http\Io\BufferedBody; | ||
use React\Http\Io\ReadableBodyStream; | ||
use React\Stream\ReadableStreamInterface; | ||
use RingCentral\Psr7\Request as BaseRequest; | ||
|
||
/** | ||
* Respresents an outgoing HTTP request message. | ||
* | ||
* This class implements the | ||
* [PSR-7 `RequestInterface`](https://www.php-fig.org/psr/psr-7/#32-psrhttpmessagerequestinterface) | ||
* which extends the | ||
* [PSR-7 `MessageInterface`](https://www.php-fig.org/psr/psr-7/#31-psrhttpmessagemessageinterface). | ||
* | ||
* This is mostly used internally to represent each outgoing HTTP request | ||
* message for the HTTP client implementation. Likewise, you can also use this | ||
* class with other HTTP client implementations and for tests. | ||
* | ||
* > Internally, this implementation builds on top of an existing outgoing | ||
* request message and only adds support for streaming. This base class is | ||
* considered an implementation detail that may change in the future. | ||
* | ||
* @see RequestInterface | ||
*/ | ||
final class Request extends BaseRequest implements RequestInterface | ||
{ | ||
/** | ||
* @param string $method HTTP method for the request. | ||
* @param string|UriInterface $url URL for the request. | ||
* @param array<string,string|string[]> $headers Headers for the message. | ||
* @param string|ReadableStreamInterface|StreamInterface $body Message body. | ||
* @param string $version HTTP protocol version. | ||
* @throws \InvalidArgumentException for an invalid URL or body | ||
*/ | ||
public function __construct( | ||
$method, | ||
$url, | ||
array $headers = array(), | ||
$body = '', | ||
$version = '1.1' | ||
) { | ||
if (\is_string($body)) { | ||
$body = new BufferedBody($body); | ||
} elseif ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) { | ||
$body = new ReadableBodyStream($body); | ||
} elseif (!$body instanceof StreamInterface) { | ||
throw new \InvalidArgumentException('Invalid request body given'); | ||
} | ||
|
||
parent::__construct($method, $url, $headers, $body, $version); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace React\Tests\Http\Message; | ||
|
||
use React\Http\Io\HttpBodyStream; | ||
use React\Http\Message\Request; | ||
use React\Stream\ThroughStream; | ||
use React\Tests\Http\TestCase; | ||
|
||
class RequestTest extends TestCase | ||
{ | ||
public function testConstructWithStringRequestBodyReturnsStringBodyWithAutomaticSize() | ||
{ | ||
$request = new Request( | ||
'GET', | ||
'http://localhost', | ||
array(), | ||
'foo' | ||
); | ||
|
||
$body = $request->getBody(); | ||
$this->assertSame(3, $body->getSize()); | ||
$this->assertEquals('foo', (string) $body); | ||
} | ||
|
||
public function testConstructWithStreamingRequestBodyReturnsBodyWhichImplementsReadableStreamInterfaceWithUnknownSize() | ||
{ | ||
$request = new Request( | ||
'GET', | ||
'http://localhost', | ||
array(), | ||
new ThroughStream() | ||
); | ||
|
||
$body = $request->getBody(); | ||
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $body); | ||
$this->assertInstanceOf('React\Stream\ReadableStreamInterface', $body); | ||
$this->assertNull($body->getSize()); | ||
} | ||
|
||
public function testConstructWithHttpBodyStreamReturnsBodyAsIs() | ||
{ | ||
$request = new Request( | ||
'GET', | ||
'http://localhost', | ||
array(), | ||
$body = new HttpBodyStream(new ThroughStream(), 100) | ||
); | ||
|
||
$this->assertSame($body, $request->getBody()); | ||
} | ||
|
||
public function testConstructWithNullBodyThrows() | ||
{ | ||
$this->setExpectedException('InvalidArgumentException', 'Invalid request body given'); | ||
new Request( | ||
'GET', | ||
'http://localhost', | ||
array(), | ||
null | ||
); | ||
} | ||
} |