Skip to content

Commit 0638dcd

Browse files
committed
Update ServerRequest class to build on top of abstract request class
1 parent 3313e1f commit 0638dcd

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,8 +2661,7 @@ This is mostly used internally to represent each incoming request message.
26612661
Likewise, you can also use this class in test cases to test how your web
26622662
application reacts to certain HTTP requests.
26632663

2664-
> Internally, this implementation builds on top of an existing outgoing
2665-
request message and only adds required server methods. This base class is
2664+
> Internally, this implementation builds on top of a base class which is
26662665
considered an implementation detail that may change in the future.
26672666

26682667
#### ResponseException

src/Message/ServerRequest.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
use Psr\Http\Message\ServerRequestInterface;
66
use Psr\Http\Message\StreamInterface;
77
use Psr\Http\Message\UriInterface;
8+
use React\Http\Io\AbstractRequest;
89
use React\Http\Io\BufferedBody;
910
use React\Http\Io\HttpBodyStream;
1011
use React\Stream\ReadableStreamInterface;
11-
use RingCentral\Psr7\Request as BaseRequest;
1212

1313
/**
1414
* Respresents an incoming server request message.
@@ -24,13 +24,12 @@
2424
* Likewise, you can also use this class in test cases to test how your web
2525
* application reacts to certain HTTP requests.
2626
*
27-
* > Internally, this implementation builds on top of an existing outgoing
28-
* request message and only adds required server methods. This base class is
27+
* > Internally, this implementation builds on top of a base class which is
2928
* considered an implementation detail that may change in the future.
3029
*
3130
* @see ServerRequestInterface
3231
*/
33-
final class ServerRequest extends BaseRequest implements ServerRequestInterface
32+
final class ServerRequest extends AbstractRequest implements ServerRequestInterface
3433
{
3534
private $attributes = array();
3635

@@ -57,26 +56,22 @@ public function __construct(
5756
$version = '1.1',
5857
$serverParams = array()
5958
) {
60-
$stream = null;
6159
if (\is_string($body)) {
6260
$body = new BufferedBody($body);
6361
} elseif ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) {
64-
$stream = $body;
65-
$body = null;
62+
$temp = new self($method, '', $headers);
63+
$size = (int) $temp->getHeaderLine('Content-Length');
64+
if (\strtolower($temp->getHeaderLine('Transfer-Encoding')) === 'chunked') {
65+
$size = null;
66+
}
67+
$body = new HttpBodyStream($body, $size);
6668
} elseif (!$body instanceof StreamInterface) {
6769
throw new \InvalidArgumentException('Invalid server request body given');
6870
}
6971

70-
$this->serverParams = $serverParams;
7172
parent::__construct($method, $url, $headers, $body, $version);
7273

73-
if ($stream !== null) {
74-
$size = (int) $this->getHeaderLine('Content-Length');
75-
if (\strtolower($this->getHeaderLine('Transfer-Encoding')) === 'chunked') {
76-
$size = null;
77-
}
78-
$this->stream = new HttpBodyStream($stream, $size);
79-
}
74+
$this->serverParams = $serverParams;
8075

8176
$query = $this->getUri()->getQuery();
8277
if ($query !== '') {

tests/Io/StreamingServerTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function testRequestEvent()
126126
$serverParams = $requestAssertion->getServerParams();
127127

128128
$this->assertSame(1, $i);
129-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
129+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
130130
$this->assertSame('GET', $requestAssertion->getMethod());
131131
$this->assertSame('/', $requestAssertion->getRequestTarget());
132132
$this->assertSame('/', $requestAssertion->getUri()->getPath());
@@ -159,7 +159,7 @@ public function testRequestEventWithSingleRequestHandlerArray()
159159
$serverParams = $requestAssertion->getServerParams();
160160

161161
$this->assertSame(1, $i);
162-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
162+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
163163
$this->assertSame('GET', $requestAssertion->getMethod());
164164
$this->assertSame('/', $requestAssertion->getRequestTarget());
165165
$this->assertSame('/', $requestAssertion->getUri()->getPath());
@@ -182,7 +182,7 @@ public function testRequestGetWithHostAndCustomPort()
182182
$data = "GET / HTTP/1.1\r\nHost: example.com:8080\r\n\r\n";
183183
$this->connection->emit('data', array($data));
184184

185-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
185+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
186186
$this->assertSame('GET', $requestAssertion->getMethod());
187187
$this->assertSame('/', $requestAssertion->getRequestTarget());
188188
$this->assertSame('/', $requestAssertion->getUri()->getPath());
@@ -204,7 +204,7 @@ public function testRequestGetWithHostAndHttpsPort()
204204
$data = "GET / HTTP/1.1\r\nHost: example.com:443\r\n\r\n";
205205
$this->connection->emit('data', array($data));
206206

207-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
207+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
208208
$this->assertSame('GET', $requestAssertion->getMethod());
209209
$this->assertSame('/', $requestAssertion->getRequestTarget());
210210
$this->assertSame('/', $requestAssertion->getUri()->getPath());
@@ -226,7 +226,7 @@ public function testRequestGetWithHostAndDefaultPortWillBeIgnored()
226226
$data = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n";
227227
$this->connection->emit('data', array($data));
228228

229-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
229+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
230230
$this->assertSame('GET', $requestAssertion->getMethod());
231231
$this->assertSame('/', $requestAssertion->getRequestTarget());
232232
$this->assertSame('/', $requestAssertion->getUri()->getPath());
@@ -248,7 +248,7 @@ public function testRequestGetHttp10WithoutHostWillBeIgnored()
248248
$data = "GET / HTTP/1.0\r\n\r\n";
249249
$this->connection->emit('data', array($data));
250250

251-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
251+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
252252
$this->assertSame('GET', $requestAssertion->getMethod());
253253
$this->assertSame('/', $requestAssertion->getRequestTarget());
254254
$this->assertSame('/', $requestAssertion->getUri()->getPath());
@@ -283,7 +283,7 @@ public function testRequestOptionsAsterisk()
283283
$data = "OPTIONS * HTTP/1.1\r\nHost: example.com\r\n\r\n";
284284
$this->connection->emit('data', array($data));
285285

286-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
286+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
287287
$this->assertSame('OPTIONS', $requestAssertion->getMethod());
288288
$this->assertSame('*', $requestAssertion->getRequestTarget());
289289
$this->assertSame('', $requestAssertion->getUri()->getPath());
@@ -316,7 +316,7 @@ public function testRequestConnectAuthorityForm()
316316
$data = "CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n";
317317
$this->connection->emit('data', array($data));
318318

319-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
319+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
320320
$this->assertSame('CONNECT', $requestAssertion->getMethod());
321321
$this->assertSame('example.com:443', $requestAssertion->getRequestTarget());
322322
$this->assertSame('', $requestAssertion->getUri()->getPath());
@@ -338,7 +338,7 @@ public function testRequestConnectWithoutHostWillBePassesAsIs()
338338
$data = "CONNECT example.com:443 HTTP/1.1\r\n\r\n";
339339
$this->connection->emit('data', array($data));
340340

341-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
341+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
342342
$this->assertSame('CONNECT', $requestAssertion->getMethod());
343343
$this->assertSame('example.com:443', $requestAssertion->getRequestTarget());
344344
$this->assertSame('', $requestAssertion->getUri()->getPath());
@@ -360,7 +360,7 @@ public function testRequestConnectAuthorityFormWithDefaultPortWillBePassedAsIs()
360360
$data = "CONNECT example.com:80 HTTP/1.1\r\nHost: example.com:80\r\n\r\n";
361361
$this->connection->emit('data', array($data));
362362

363-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
363+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
364364
$this->assertSame('CONNECT', $requestAssertion->getMethod());
365365
$this->assertSame('example.com:80', $requestAssertion->getRequestTarget());
366366
$this->assertSame('', $requestAssertion->getUri()->getPath());
@@ -382,7 +382,7 @@ public function testRequestConnectAuthorityFormNonMatchingHostWillBePassedAsIs()
382382
$data = "CONNECT example.com:80 HTTP/1.1\r\nHost: other.example.org\r\n\r\n";
383383
$this->connection->emit('data', array($data));
384384

385-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
385+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
386386
$this->assertSame('CONNECT', $requestAssertion->getMethod());
387387
$this->assertSame('example.com:80', $requestAssertion->getRequestTarget());
388388
$this->assertSame('', $requestAssertion->getUri()->getPath());
@@ -434,7 +434,7 @@ public function testRequestWithoutHostEventUsesSocketAddress()
434434
$data = "GET /test HTTP/1.0\r\n\r\n";
435435
$this->connection->emit('data', array($data));
436436

437-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
437+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
438438
$this->assertSame('GET', $requestAssertion->getMethod());
439439
$this->assertSame('/test', $requestAssertion->getRequestTarget());
440440
$this->assertEquals('http://127.0.0.1/test', $requestAssertion->getUri());
@@ -455,7 +455,7 @@ public function testRequestAbsoluteEvent()
455455
$data = "GET http://example.com/test HTTP/1.1\r\nHost: example.com\r\n\r\n";
456456
$this->connection->emit('data', array($data));
457457

458-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
458+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
459459
$this->assertSame('GET', $requestAssertion->getMethod());
460460
$this->assertSame('http://example.com/test', $requestAssertion->getRequestTarget());
461461
$this->assertEquals('http://example.com/test', $requestAssertion->getUri());
@@ -477,7 +477,7 @@ public function testRequestAbsoluteNonMatchingHostWillBePassedAsIs()
477477
$data = "GET http://example.com/test HTTP/1.1\r\nHost: other.example.org\r\n\r\n";
478478
$this->connection->emit('data', array($data));
479479

480-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
480+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
481481
$this->assertSame('GET', $requestAssertion->getMethod());
482482
$this->assertSame('http://example.com/test', $requestAssertion->getRequestTarget());
483483
$this->assertEquals('http://example.com/test', $requestAssertion->getUri());
@@ -511,7 +511,7 @@ public function testRequestOptionsAsteriskEvent()
511511
$data = "OPTIONS * HTTP/1.1\r\nHost: example.com\r\n\r\n";
512512
$this->connection->emit('data', array($data));
513513

514-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
514+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
515515
$this->assertSame('OPTIONS', $requestAssertion->getMethod());
516516
$this->assertSame('*', $requestAssertion->getRequestTarget());
517517
$this->assertEquals('http://example.com', $requestAssertion->getUri());
@@ -533,7 +533,7 @@ public function testRequestOptionsAbsoluteEvent()
533533
$data = "OPTIONS http://example.com HTTP/1.1\r\nHost: example.com\r\n\r\n";
534534
$this->connection->emit('data', array($data));
535535

536-
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
536+
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $requestAssertion);
537537
$this->assertSame('OPTIONS', $requestAssertion->getMethod());
538538
$this->assertSame('http://example.com', $requestAssertion->getRequestTarget());
539539
$this->assertEquals('http://example.com', $requestAssertion->getUri());

0 commit comments

Comments
 (0)