Skip to content

Commit

Permalink
Some minor API changes, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Aug 25, 2015
1 parent f197869 commit 4e19113
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -16,7 +16,7 @@
],
"require": {
"icicleio/icicle": "0.8",
"icicleio/stream": "^0.2",
"icicleio/stream": "^0.2.1",
"icicleio/socket": "^0.2",
"icicleio/dns": "^0.5"
},
Expand Down
24 changes: 12 additions & 12 deletions src/Client/Client.php
Expand Up @@ -45,18 +45,16 @@ public function request(
$uri,
array $headers = [],
ReadableStreamInterface $body = null,
$timeout = RequesterInterface::DEFAULT_TIMEOUT,
array $options = []
) {
return $this->send(new Request($method, $uri, $headers, $body), $timeout, $options);
return $this->send(new Request($method, $uri, $headers, $body), $options);
}

/**
* {@inheritdoc}
*/
public function send(
RequestInterface $request,
$timeout = RequesterInterface::DEFAULT_TIMEOUT,
array $options = []
) {
$uri = $request->getUri();
Expand All @@ -68,16 +66,18 @@ public function send(
throw new FailureException('Could not connect to server.');
}

if ($uri->getScheme() === 'https') {
$cryptoMethod = isset($options['crypto_method'])
? (int) $options['crypto_method']
: self::DEFAULT_CRYPTO_METHOD;
try {
if ($uri->getScheme() === 'https') {
$cryptoMethod = isset($options['crypto_method'])
? (int) $options['crypto_method']
: self::DEFAULT_CRYPTO_METHOD;

yield $client->enableCrypto($cryptoMethod);
}

$options['timeout'] = $timeout;
yield $client->enableCrypto($cryptoMethod);
}

yield $this->requester->request($client, $request, $options);
yield $this->requester->request($client, $request, $options);
} finally {
$client->close();
}
}
}
2 changes: 0 additions & 2 deletions src/Client/ClientInterface.php
Expand Up @@ -30,7 +30,6 @@ public function request(
$uri,
array $headers = null,
ReadableStreamInterface $body = null,
$timeout = RequesterInterface::DEFAULT_TIMEOUT,
array $options = null
);

Expand All @@ -50,7 +49,6 @@ public function request(
*/
public function send(
RequestInterface $request,
$timeout = RequesterInterface::DEFAULT_TIMEOUT,
array $options = null
);
}
10 changes: 6 additions & 4 deletions src/Message/Request.php
Expand Up @@ -23,7 +23,7 @@ class Request extends Message implements RequestInterface
private $hostFromUri = false;

/**
* @var string|null
* @var string
*/
private $target;

Expand All @@ -50,9 +50,7 @@ public function __construct(
$this->method = $this->filterMethod($method);
$this->uri = $uri instanceof UriInterface ? $uri : new Uri($uri);

if (null !== $target) {
$this->target = $this->filterTarget($target);
}
$this->target = $this->filterTarget($target);

if (!$this->hasHeader('Host')) {
$this->setHostFromUri();
Expand Down Expand Up @@ -206,6 +204,10 @@ protected function filterMethod($method)
*/
protected function filterTarget($target)
{
if (!is_string($target)) {
throw new InvalidMethodException('Request target must be a string.');
}

if (preg_match('/\s/', $target)) {
throw new InvalidValueException('Request target cannot contain whitespace.');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Message/Uri.php
Expand Up @@ -131,7 +131,7 @@ public function getHost()
*/
public function getPort()
{
if (null === $this->port) {
if (0 === $this->port) {
return $this->getPortForScheme();
}

Expand Down Expand Up @@ -341,7 +341,7 @@ protected function getPortForScheme()
$scheme = $this->getScheme();

if (!$scheme) {
return null;
return 0;
}

return $this->allowedSchemes()[$scheme];
Expand Down
2 changes: 1 addition & 1 deletion src/Reader/Reader.php
Expand Up @@ -68,7 +68,7 @@ public function readRequest(ReadableStreamInterface $stream, $timeout = 0)

if ('/' === $target[0]) { // origin-form
$uri = new Uri($this->filterHost($this->findHost($headers)) . $target);
$target = null; // null $target since it was a path.
$target = ''; // Empty request target since it was a path.
} elseif ('*' === $target) { // asterisk-form
$uri = new Uri($this->filterHost($this->findHost($headers)));
} elseif (preg_match('/^https?:\/\//i', $target)) { // absolute-form
Expand Down
4 changes: 2 additions & 2 deletions src/Server/Server.php
Expand Up @@ -77,7 +77,7 @@ class Server implements ServerInterface

/**
* @param callable $onRequest
* @param mixed[]|null $options
* @param mixed[] $options
*/
public function __construct(callable $onRequest, array $options = [])
{
Expand Down Expand Up @@ -153,7 +153,7 @@ public function close()
}

/**
* @param string|int $address
* @param string $address
* @param int $port
* @param mixed[] $options
*
Expand Down
10 changes: 5 additions & 5 deletions tests/Message/MessageTest.php
Expand Up @@ -21,7 +21,7 @@ public function createStream()
*
* @return \Icicle\Http\Message\Message
*/
public function createMessage(ReadableStreamInterface $stream = null, array $headers = null, $protocol = '1.1')
public function createMessage(ReadableStreamInterface $stream = null, array $headers = [], $protocol = '1.1')
{
return $this->getMockForAbstractClass('Icicle\Http\Message\Message', [$headers, $stream, $protocol]);
}
Expand All @@ -30,7 +30,7 @@ public function testGetProtocol()
{
$protocol = '1.0';

$message = $this->createMessage(null, null, $protocol);
$message = $this->createMessage(null, [], $protocol);

$this->assertSame($protocol, $message->getProtocolVersion());
}
Expand All @@ -43,7 +43,7 @@ public function testWithProtocol()
$original = '1.1';
$protocol = '1.0';

$message = $this->createMessage(null, null, $original);
$message = $this->createMessage(null, [], $original);
$new = $message->withProtocolVersion($protocol);

$this->assertNotSame($message, $new);
Expand All @@ -58,7 +58,7 @@ public function testCreateWithInvalidProtocol()
{
$protocol = 'protocol';

$message = $this->createMessage(null, null, $protocol);
$message = $this->createMessage(null, [], $protocol);
}

/**
Expand All @@ -69,7 +69,7 @@ public function testWithInvalidProtocol()
$original = '1.1';
$protocol = 'protocol';

$message = $this->createMessage(null, null, $original);
$message = $this->createMessage(null, [], $original);
$new = $message->withProtocolVersion($protocol);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Message/RequestTest.php
Expand Up @@ -188,7 +188,7 @@ public function testWithUriSetsHostHeader()
*/
public function testConstructWithInvalidTarget()
{
new Request('GET', '', null, null, 'Invalid target');
new Request('GET', '', [], null, 'Invalid target');
}

public function getUris()
Expand Down Expand Up @@ -233,7 +233,7 @@ public function getTargets()
*/
public function testConstructWithTarget($target)
{
$request = new Request('GET', 'http://example.org/different/path', null, null, $target);
$request = new Request('GET', 'http://example.org/different/path', [], null, $target);
$this->assertSame($target, $request->getRequestTarget());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Message/ResponseTest.php
Expand Up @@ -62,7 +62,7 @@ public function testConstructWithReason($code)
{
$reason = 'Custom Reason';

$response = new Response($code, null, null, $reason);
$response = new Response($code, [], null, $reason);
$this->assertSame((int) $code, $response->getStatusCode());
$this->assertSame($reason, $response->getReasonPhrase());
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Message/UriTest.php
Expand Up @@ -39,7 +39,7 @@ public function getUris()
'',
'',
'example.com',
null,
0,
'example.com',
'',
'',
Expand All @@ -61,7 +61,7 @@ public function getUris()
'',
'',
'example.com',
null,
0,
'example.com',
'/path/to/resource',
'',
Expand All @@ -72,7 +72,7 @@ public function getUris()
'',
'',
'',
null,
0,
'',
'/path/without/host',
'name=value',
Expand All @@ -94,7 +94,7 @@ public function getUris()
'',
'',
'',
null,
0,
'',
'',
'',
Expand Down
7 changes: 4 additions & 3 deletions tests/Reader/ReaderTest.php
Expand Up @@ -35,7 +35,8 @@ protected function readMessage($filename)
$data = file_get_contents(dirname(__DIR__) . '/data/' . $filename);

$stream = new Stream();
$stream->end($data);
$coroutine = new Coroutine($stream->end($data));
$coroutine->wait();

return $stream;
}
Expand Down Expand Up @@ -81,7 +82,7 @@ public function testReadRequest($filename, $method, $target, $protocolVersion, $
$this->assertSame(strlen($body), $stream->getLength());
}

$promise = $stream->read();
$promise = new Coroutine($stream->read());

$callback = $this->createCallback(1);
$callback->method('__invoke')
Expand Down Expand Up @@ -167,7 +168,7 @@ public function testReadResponse($filename, $code, $reason, $protocolVersion, $h
$this->assertSame(strlen($body), $stream->getLength());
}

$promise = $stream->read();
$promise = new Coroutine($stream->read());

$callback = $this->createCallback(1);
$callback->method('__invoke')
Expand Down

0 comments on commit 4e19113

Please sign in to comment.