Skip to content

Commit

Permalink
Merge pull request #99 from clue-labs/invalid-url
Browse files Browse the repository at this point in the history
Emit error event if request URL is invalid
  • Loading branch information
WyriHaximus authored Jun 16, 2017
2 parents 4e3c82f + 4d5233a commit f77c327
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Evenement\EventEmitterTrait;
use GuzzleHttp\Psr7 as gPsr;
use React\Promise;
use React\Socket\ConnectorInterface;
use React\Stream\WritableStreamInterface;
use React\Socket\ConnectionInterface;
Expand Down Expand Up @@ -80,7 +81,7 @@ function (ConnectionInterface $stream) use ($requestData, &$streamRef, &$stateRe
}
}
},
array($this, 'handleError')
array($this, 'closeError')
);

$this->on('close', function() use ($promise) {
Expand Down Expand Up @@ -247,10 +248,17 @@ protected function parseResponse($data)

protected function connect()
{
$scheme = $this->requestData->getScheme();
if ($scheme !== 'https' && $scheme !== 'http') {
return Promise\reject(
new \InvalidArgumentException('Invalid request URL given')
);
}

$host = $this->requestData->getHost();
$port = $this->requestData->getPort();

if ($this->requestData->getScheme() === 'https') {
if ($scheme === 'https') {
$host = 'tls://' . $host;
}

Expand Down
46 changes: 46 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,52 @@ public function requestShouldEmitErrorIfGuzzleParseThrowsException()
$request->handleData("\r\n\r\n");
}

/**
* @test
*/
public function requestShouldEmitErrorIfUrlIsInvalid()
{
$requestData = new RequestData('GET', 'ftp://www.example.com');
$request = new Request($this->connector, $requestData);

$handler = $this->createCallableMock();
$handler->expects($this->once())
->method('__invoke')
->with(
$this->isInstanceOf('\InvalidArgumentException')
);

$request->on('error', $handler);

$this->connector->expects($this->never())
->method('connect');

$request->end();
}

/**
* @test
*/
public function requestShouldEmitErrorIfUrlHasNoScheme()
{
$requestData = new RequestData('GET', 'www.example.com');
$request = new Request($this->connector, $requestData);

$handler = $this->createCallableMock();
$handler->expects($this->once())
->method('__invoke')
->with(
$this->isInstanceOf('\InvalidArgumentException')
);

$request->on('error', $handler);

$this->connector->expects($this->never())
->method('connect');

$request->end();
}

/**
* @test
* @expectedException Exception
Expand Down

0 comments on commit f77c327

Please sign in to comment.