Skip to content

Commit

Permalink
Requests: Add tests for requests.failed hook
Browse files Browse the repository at this point in the history
  • Loading branch information
pprkut committed Dec 17, 2021
1 parent 7022ce0 commit bc3e608
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/Fixtures/TransportFailedMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace WpOrg\Requests\Tests\Fixtures;

use WpOrg\Requests\Exception;
use WpOrg\Requests\Transport;

final class TransportFailedMock implements Transport {
public function request($url, $headers = [], $data = [], $options = []) {
throw new Exception('Transport failed!', 'transporterror');
}
public function request_multiple($requests, $options) {
throw new Exception('Transport failed!', 'transporterror');
}
public static function test($capabilities = []) {
return true;
}
}
18 changes: 18 additions & 0 deletions tests/Fixtures/TransportInvalidArgumentMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace WpOrg\Requests\Tests\Fixtures;

use WpOrg\Requests\Exception\InvalidArgument;
use WpOrg\Requests\Transport;

final class TransportInvalidArgumentMock implements Transport {
public function request($url, $headers = [], $data = [], $options = []) {
throw InvalidArgument::create(1, '$url', 'string|Stringable', gettype($url));
}
public function request_multiple($requests, $options) {
throw InvalidArgument::create(1, '$requests', 'array|ArrayAccess&Traversable', gettype($requests));
}
public static function test($capabilities = []) {
return true;
}
}
105 changes: 105 additions & 0 deletions tests/RequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
use WpOrg\Requests\Capability;
use WpOrg\Requests\Exception;
use WpOrg\Requests\Exception\InvalidArgument;
use WpOrg\Requests\Hooks;
use WpOrg\Requests\Iri;
use WpOrg\Requests\Requests;
use WpOrg\Requests\Response\Headers;
use WpOrg\Requests\Tests\Fixtures\ArrayAccessibleObject;
use WpOrg\Requests\Tests\Fixtures\RawTransportMock;
use WpOrg\Requests\Tests\Fixtures\StringableObject;
use WpOrg\Requests\Tests\Fixtures\TestTransportMock;
use WpOrg\Requests\Tests\Fixtures\TransportFailedMock;
use WpOrg\Requests\Tests\Fixtures\TransportInvalidArgumentMock;
use WpOrg\Requests\Tests\Fixtures\TransportMock;

final class RequestsTest extends TestCase {
Expand Down Expand Up @@ -172,6 +175,42 @@ public function testDefaultTransport() {
$this->assertSame(200, $request->status_code);
}

public function testTransportFailedTriggersRequestsFailedCallback() {
$mock = $this->getMockBuilder(stdClass::class)->setMethods(['failed'])->getMock();
$mock->expects($this->atLeastOnce())->method('failed');
$hooks = new Hooks();
$hooks->register('requests.failed', [$mock, 'failed']);

$transport = new TransportFailedMock();

$options = [
'hooks' => $hooks,
'transport' => $transport,
];

$this->expectException(Exception::class);
$this->expectExceptionMessage('Transport failed!');
Requests::get('http://example.com/', [], $options);
}

public function testTransportInvalidArgumentTriggersRequestsFailedCallback() {
$mock = $this->getMockBuilder(stdClass::class)->setMethods(['failed'])->getMock();
$mock->expects($this->atLeastOnce())->method('failed');
$hooks = new Hooks();
$hooks->register('requests.failed', [$mock, 'failed']);

$transport = new TransportInvalidArgumentMock();

$options = [
'hooks' => $hooks,
'transport' => $transport,
];

$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage('Argument #1 ($url) must be of type string|Stringable');
Requests::get('http://example.com/', [], $options);
}

/**
* Standard response header parsing
*/
Expand Down Expand Up @@ -272,6 +311,31 @@ public function testInvalidProtocolVersion() {
Requests::get('http://example.com/', [], $options);
}

/**
* Check that invalid protocols are not accepted
*
* We do not support HTTP/0.9. If this is really an issue for you, file a
* new issue, and update your server/proxy to support a proper protocol.
*/
public function testInvalidProtocolVersionTriggersRequestsFailedCallback() {
$mock = $this->getMockBuilder(stdClass::class)->setMethods(['failed'])->getMock();
$mock->expects($this->atLeastOnce())->method('failed');
$hooks = new Hooks();
$hooks->register('requests.failed', [$mock, 'failed']);

$transport = new RawTransportMock();
$transport->data = "HTTP/0.9 200 OK\r\n\r\n<p>Test";

$options = [
'hooks' => $hooks,
'transport' => $transport,
];

$this->expectException(Exception::class);
$this->expectExceptionMessage('Response could not be parsed');
Requests::get('http://example.com/', [], $options);
}

/**
* HTTP/0.9 also appears to use a single CRLF instead of two.
*/
Expand All @@ -288,6 +352,28 @@ public function testSingleCRLFSeparator() {
Requests::get('http://example.com/', [], $options);
}

/**
* HTTP/0.9 also appears to use a single CRLF instead of two.
*/
public function testSingleCRLFSeparatorTriggersRequestsFailedCallback() {
$mock = $this->getMockBuilder(stdClass::class)->setMethods(['failed'])->getMock();
$mock->expects($this->atLeastOnce())->method('failed');
$hooks = new Hooks();
$hooks->register('requests.failed', [$mock, 'failed']);

$transport = new RawTransportMock();
$transport->data = "HTTP/0.9 200 OK\r\n<p>Test";

$options = [
'hooks' => $hooks,
'transport' => $transport,
];

$this->expectException(Exception::class);
$this->expectExceptionMessage('Missing header/body separator');
Requests::get('http://example.com/', [], $options);
}

public function testInvalidStatus() {
$transport = new RawTransportMock();
$transport->data = "HTTP/1.1 OK\r\nTest: value\nAnother-Test: value\r\n\r\nTest";
Expand All @@ -301,6 +387,25 @@ public function testInvalidStatus() {
Requests::get('http://example.com/', [], $options);
}

public function testInvalidStatusTriggersRequestsFailedCallback() {
$mock = $this->getMockBuilder(stdClass::class)->setMethods(['failed'])->getMock();
$mock->expects($this->atLeastOnce())->method('failed');
$hooks = new Hooks();
$hooks->register('requests.failed', [$mock, 'failed']);

$transport = new RawTransportMock();
$transport->data = "HTTP/1.1 OK\r\nTest: value\nAnother-Test: value\r\n\r\nTest";

$options = [
'hooks' => $hooks,
'transport' => $transport,
];

$this->expectException(Exception::class);
$this->expectExceptionMessage('Response could not be parsed');
Requests::get('http://example.com/', [], $options);
}

public function test30xWithoutLocation() {
$transport = new TransportMock();
$transport->code = 302;
Expand Down

0 comments on commit bc3e608

Please sign in to comment.