Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce absolute URIs via new Uri class #40

Merged
merged 5 commits into from
Jul 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,41 @@ See [`Sender`](#sender) for more details.
The `Message` is an abstract base class for the [`Response`](#response) and [`Request`](#request).
It provides a common interface for these message types.

#### Response
See its [class outline](src/Message/Message.php) for more details.

### Response

The `Response` value object represents the incoming response received from the [`Browser`](#browser).
It shares all properties of the [`Message`](#message) parent class.

#### Request
See its [class outline](src/Message/Response.php) for more details.

### Request

The `Request` value object represents the outgoing request to be sent via the [`Browser`](#browser).
It shares all properties of the [`Message`](#message) parent class.

See its [class outline](src/Message/Request.php) for more details.

#### getUri()

The `getUri()` method can be used to get its [`Uri`](#sender) instance.

### Uri

Each [`Request`](#request) contains a (full) absolute request URI.

```
$request = new Request('GET', 'http://www.google.com/');
$uri = $request->getUri();

assert('http' == $uri->getScheme());
assert('www.google.com' == $uri->getHost());
assert('/' == $uri->getPath());
```

See its [class outline](src/Message/Uri.php) for more details.

### ResponseException

The `ResponseException` is an `Exception` sub-class that will be used to reject
Expand Down
2 changes: 1 addition & 1 deletion src/Io/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function send(Request $request)

$deferred = new Deferred();

$requestStream = $this->http->request($request->getMethod(), $request->getUrl(), $headers);
$requestStream = $this->http->request($request->getMethod(), (string)$request->getUri(), $headers);

$requestStream->on('error', function($error) use ($deferred) {
$deferred->reject($error);
Expand Down
15 changes: 9 additions & 6 deletions src/Message/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
class Request implements Message
{
private $method;
private $url;
private $uri;
private $headers;
private $body;

/**
* instantiate new Request object
*
* @param string $method all uppercase HTTP method
* @param string $url full request URL
* @param Uri|string $uri full request URI
* @param Headers|array $headers HTTP header object or array
* @param Body|string $body HTTP request message body
*/
public function __construct($method, $url, $headers = array(), $body = '')
public function __construct($method, $uri, $headers = array(), $body = '')
{
if (!($uri instanceof Uri)) {
$uri = new Uri($uri);
}
if (!($headers instanceof Headers)) {
$headers = new Headers($headers);
}
Expand All @@ -27,7 +30,7 @@ public function __construct($method, $url, $headers = array(), $body = '')
}

$this->method = $method;
$this->url = $url;
$this->uri = $uri;
$this->headers = $headers;
$this->body = $body;
}
Expand All @@ -37,9 +40,9 @@ public function getMethod()
return $this->method;
}

public function getUrl()
public function getUri()
{
return $this->url;
return $this->uri;
}

public function getHttpVersion()
Expand Down
66 changes: 66 additions & 0 deletions src/Message/Uri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Clue\React\Buzz\Message;

use InvalidArgumentException;

class Uri
{
private $scheme;
private $host;
private $path;
private $query;

public function __construct($uri)
{
$parts = parse_url($uri);
if (!$parts || !isset($parts['scheme'], $parts['host'], $parts['path'])) {
throw new InvalidArgumentException('Not a valid absolute URI');
}

$this->scheme = $parts['scheme'];
$this->host = $parts['host'];
$this->port = isset($parts['port']) ? $parts['port'] : null;
$this->path = $parts['path'];
$this->query = isset($parts['query']) ? $parts['query'] : '';
}

public function __toString()
{
$url = $this->scheme . '://' . $this->host;
if ($this->port !== null) {
$url .= ':' . $this->port;
}
$url .= $this->path;
if ($this->query !== '') {
$url .= '?' . $this->query;
}

return $url;
}

public function getScheme()
{
return $this->scheme;
}

public function getHost()
{
return $this->host;
}

public function getPort()
{
return $this->port;
}

public function getPath()
{
return $this->path;
}

public function getQuery()
{
return $this->query;
}
}
2 changes: 1 addition & 1 deletion tests/FunctionalBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testRejectingRedirectsRejects()

public function testInvalidPort()
{
$this->expectPromiseReject($this->browser->get('http://www.google.com:443'));
$this->expectPromiseReject($this->browser->get('http://www.google.com:443/'));

$this->loop->run();
}
Expand Down
30 changes: 28 additions & 2 deletions tests/Message/RequestTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
<?php

use Clue\React\Buzz\Message\Request;
use Clue\React\Buzz\Message\Uri;

class RequestTest extends TestCase
{
public function testCtor()
{
$request = new Request('GET', '/');
$request = new Request('GET', 'http://example.com/');

$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('/', $request->getUrl());
$this->assertEquals('http://example.com/', $request->getUri());
$this->assertEquals('HTTP/1.1', $request->getHttpVersion());

$this->assertEquals(array(), $request->getHeaders()->getAll());
$this->assertTrue($request->getBody()->isEmpty());
}

/**
* @expectedException InvalidArgumentException
*/
public function testCtorRequiresAbsoluteUri()
{
new Request('GET', '/test');
}

public function testUriString()
{
$request = new Request('GET', 'http://example.com/demo?just=testing');

$this->assertInstanceOf('Clue\React\Buzz\Message\Uri', $request->getUri());
$this->assertEquals('http://example.com/demo?just=testing', $request->getUri());
}

public function testUriInstance()
{
$uri = new Uri('http://example.com/demo?just=testing');
$request = new Request('GET', $uri);

$this->assertSame($uri, $request->getUri());
$this->assertEquals('http://example.com/demo?just=testing', $request->getUri());
}
}
37 changes: 37 additions & 0 deletions tests/Message/UriTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Clue\React\Buzz\Message\Uri;

class UriTest extends TestCase
{
public function testUriSimple()
{
$uri = new Uri('http://www.lueck.tv/');

$this->assertEquals('http', $uri->getScheme());
$this->assertEquals('www.lueck.tv', $uri->getHost());
$this->assertEquals('/', $uri->getPath());

$this->assertEquals(null, $uri->getPort());
$this->assertEquals('', $uri->getQuery());
}

public function testUriComplete()
{
$uri = new Uri('https://example.com:8080/?just=testing');

$this->assertEquals('https', $uri->getScheme());
$this->assertEquals('example.com', $uri->getHost());
$this->assertEquals(8080, $uri->getPort());
$this->assertEquals('/', $uri->getPath());
$this->assertEquals('just=testing', $uri->getQuery());
}

/**
* @expectedException InvalidArgumentException
*/
public function testInvalidUri()
{
new Uri('invalid');
}
}