|
1 | 1 | <?php |
2 | 2 | namespace ScriptFUSIONTest\Functional\Porter\Net\Http; |
3 | 3 |
|
| 4 | +use ScriptFUSION\Porter\Connector\Connector; |
| 5 | +use ScriptFUSION\Porter\Net\Http\HttpConnectionException; |
4 | 6 | use ScriptFUSION\Porter\Net\Http\HttpConnector; |
5 | 7 | use ScriptFUSION\Porter\Net\Http\HttpOptions; |
| 8 | +use ScriptFUSION\Porter\Net\Http\HttpServerException; |
| 9 | +use ScriptFUSION\Retry\FailingTooHardException; |
6 | 10 | use Symfony\Component\Process\Process; |
7 | 11 |
|
8 | 12 | final class HttpConnectorTest extends \PHPUnit_Framework_TestCase |
9 | 13 | { |
10 | 14 | const HOST = 'localhost:12345'; |
11 | 15 | const URI = '/test?baz=qux'; |
12 | 16 |
|
13 | | - public function testConnectionToLocalWebserver() |
| 17 | + private static $dir; |
| 18 | + |
| 19 | + /** @var HttpConnector */ |
| 20 | + private $connector; |
| 21 | + |
| 22 | + public static function setUpBeforeClass() |
| 23 | + { |
| 24 | + self::$dir = __DIR__ . '/servers'; |
| 25 | + } |
| 26 | + |
| 27 | + protected function setUp() |
14 | 28 | { |
15 | | - $connector = new HttpConnector((new HttpOptions)->addHeader($header = 'Foo: Bar')); |
| 29 | + $this->connector = new HttpConnector; |
| 30 | + } |
16 | 31 |
|
17 | | - $process = (new Process(sprintf('php -S %s feedback.php', self::HOST)))->setWorkingDirectory(__DIR__); |
18 | | - $process->start(); |
19 | | - $response = $connector->fetch('http://' . self::HOST . self::URI); |
20 | | - $process->stop(); |
| 32 | + public function testConnectionToLocalWebserver() |
| 33 | + { |
| 34 | + $server = $this->startServer('feedback'); |
| 35 | + $response = $this->fetch(new HttpConnector((new HttpOptions)->addHeader($header = 'Foo: Bar'))); |
| 36 | + $this->stopServer($server); |
21 | 37 |
|
22 | 38 | self::assertRegExp('[\AGET \Q' . self::HOST . self::URI . '\E HTTP/\d+\.\d+$]m', $response); |
23 | 39 | self::assertRegExp("[^$header$]m", $response); |
24 | 40 | } |
| 41 | + |
| 42 | + public function testConnectionTimeout() |
| 43 | + { |
| 44 | + try { |
| 45 | + $this->fetch($this->connector->setTries(1)); |
| 46 | + } catch (FailingTooHardException $exception) { |
| 47 | + self::assertInstanceOf(HttpConnectionException::class, $exception->getPrevious()); |
| 48 | + |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + self::fail('Expected exception not thrown.'); |
| 53 | + } |
| 54 | + |
| 55 | + public function testErrorResponse() |
| 56 | + { |
| 57 | + $server = $this->startServer('404'); |
| 58 | + try { |
| 59 | + $this->fetch(); |
| 60 | + } catch (FailingTooHardException $exception) { |
| 61 | + self::assertInstanceOf(HttpServerException::class, $exception->getPrevious(), $exception->getMessage()); |
| 62 | + self::assertStringEndsWith('foo', $exception->getPrevious()->getMessage()); |
| 63 | + |
| 64 | + return; |
| 65 | + } finally { |
| 66 | + $this->stopServer($server); |
| 67 | + } |
| 68 | + |
| 69 | + self::fail('Expected exception not thrown.'); |
| 70 | + } |
| 71 | + |
| 72 | + public function testOneTry() |
| 73 | + { |
| 74 | + $this->setExpectedException(FailingTooHardException::class, '1'); |
| 75 | + |
| 76 | + $this->fetch($this->connector->setTries(1)); |
| 77 | + } |
| 78 | + |
| 79 | + public function testDefaultTries() |
| 80 | + { |
| 81 | + $this->setExpectedException(FailingTooHardException::class, (string)HttpConnector::DEFAULT_TRIES); |
| 82 | + |
| 83 | + $this->fetch(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param string $script |
| 88 | + * |
| 89 | + * @return Process |
| 90 | + */ |
| 91 | + private function startServer($script) |
| 92 | + { |
| 93 | + $server = (new Process(sprintf('php -S %s %s.php', self::HOST, $script)))->setWorkingDirectory(self::$dir); |
| 94 | + $server->start(); |
| 95 | + |
| 96 | + return $server; |
| 97 | + } |
| 98 | + |
| 99 | + private function stopServer(Process $server) |
| 100 | + { |
| 101 | + /* |
| 102 | + * Bizarrely, process IDs are one less than they should be on Travis. |
| 103 | + * See https://github.com/symfony/symfony/issues/19611 |
| 104 | + */ |
| 105 | + if (array_key_exists('TRAVIS', $_SERVER)) { |
| 106 | + posix_kill($server->getPid() + 1, SIGINT); |
| 107 | + |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + $server->stop(); |
| 112 | + } |
| 113 | + |
| 114 | + private function fetch(Connector $connector = null) |
| 115 | + { |
| 116 | + $connector = $connector ?: $this->connector; |
| 117 | + |
| 118 | + return $connector->fetch('http://' . self::HOST . self::URI); |
| 119 | + } |
25 | 120 | } |
0 commit comments