| 1 |
<?php |
| 2 |
namespace GuzzleHttp\Tests; |
| 3 |
|
| 4 |
use GuzzleHttp\Cookie\CookieJar; |
| 5 |
use GuzzleHttp\Handler\MockHandler; |
| 6 |
use GuzzleHttp\HandlerStack; |
| 7 |
use GuzzleHttp\Psr7\Request; |
| 8 |
use GuzzleHttp\Psr7\Response; |
| 9 |
use PHPUnit\Framework\TestCase; |
| 10 |
|
| 11 |
class HandlerStackTest extends TestCase |
| 12 |
{ |
| 13 |
public function testSetsHandlerInCtor() |
| 14 |
{ |
| 15 |
$f = function () { |
| 16 |
}; |
| 17 |
$m1 = function () { |
| 18 |
}; |
| 19 |
$h = new HandlerStack($f, [$m1]); |
| 20 |
self::assertTrue($h->hasHandler()); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @doesNotPerformAssertions |
| 25 |
*/ |
| 26 |
public function testCanSetDifferentHandlerAfterConstruction() |
| 27 |
{ |
| 28 |
$f = function () { |
| 29 |
}; |
| 30 |
$h = new HandlerStack(); |
| 31 |
$h->setHandler($f); |
| 32 |
$h->resolve(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @expectedException \LogicException |
| 37 |
*/ |
| 38 |
public function testEnsuresHandlerIsSet() |
| 39 |
{ |
| 40 |
$h = new HandlerStack(); |
| 41 |
$h->resolve(); |
| 42 |
} |
| 43 |
|
| 44 |
public function testPushInOrder() |
| 45 |
{ |
| 46 |
$meths = $this->getFunctions(); |
| 47 |
$builder = new HandlerStack(); |
| 48 |
$builder->setHandler($meths[1]); |
| 49 |
$builder->push($meths[2]); |
| 50 |
$builder->push($meths[3]); |
| 51 |
$builder->push($meths[4]); |
| 52 |
$composed = $builder->resolve(); |
| 53 |
self::assertSame('Hello - test123', $composed('test')); |
| 54 |
self::assertSame( |
| 55 |
[['a', 'test'], ['b', 'test1'], ['c', 'test12']], |
| 56 |
$meths[0] |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
public function testUnshiftsInReverseOrder() |
| 61 |
{ |
| 62 |
$meths = $this->getFunctions(); |
| 63 |
$builder = new HandlerStack(); |
| 64 |
$builder->setHandler($meths[1]); |
| 65 |
$builder->unshift($meths[2]); |
| 66 |
$builder->unshift($meths[3]); |
| 67 |
$builder->unshift($meths[4]); |
| 68 |
$composed = $builder->resolve(); |
| 69 |
self::assertSame('Hello - test321', $composed('test')); |
| 70 |
self::assertSame( |
| 71 |
[['c', 'test'], ['b', 'test3'], ['a', 'test32']], |
| 72 |
$meths[0] |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
public function testCanRemoveMiddlewareByInstance() |
| 77 |
{ |
| 78 |
$meths = $this->getFunctions(); |
| 79 |
$builder = new HandlerStack(); |
| 80 |
$builder->setHandler($meths[1]); |
| 81 |
$builder->push($meths[2]); |
| 82 |
$builder->push($meths[2]); |
| 83 |
$builder->push($meths[3]); |
| 84 |
$builder->push($meths[4]); |
| 85 |
$builder->push($meths[2]); |
| 86 |
$builder->remove($meths[3]); |
| 87 |
$composed = $builder->resolve(); |
| 88 |
self::assertSame('Hello - test1131', $composed('test')); |
| 89 |
} |
| 90 |
|
| 91 |
public function testCanPrintMiddleware() |
| 92 |
{ |
| 93 |
$meths = $this->getFunctions(); |
| 94 |
$builder = new HandlerStack(); |
| 95 |
$builder->setHandler($meths[1]); |
| 96 |
$builder->push($meths[2], 'a'); |
| 97 |
$builder->push([__CLASS__, 'foo']); |
| 98 |
$builder->push([$this, 'bar']); |
| 99 |
$builder->push(__CLASS__ . '::' . 'foo'); |
| 100 |
$lines = explode("\n", (string) $builder); |
| 101 |
self::assertContains("> 4) Name: 'a', Function: callable(", $lines[0]); |
| 102 |
self::assertContains("> 3) Name: '', Function: callable(GuzzleHttp\\Tests\\HandlerStackTest::foo)", $lines[1]); |
| 103 |
self::assertContains("> 2) Name: '', Function: callable(['GuzzleHttp\\Tests\\HandlerStackTest', 'bar'])", $lines[2]); |
| 104 |
self::assertContains("> 1) Name: '', Function: callable(GuzzleHttp\\Tests\\HandlerStackTest::foo)", $lines[3]); |
| 105 |
self::assertContains("< 0) Handler: callable(", $lines[4]); |
| 106 |
self::assertContains("< 1) Name: '', Function: callable(GuzzleHttp\\Tests\\HandlerStackTest::foo)", $lines[5]); |
| 107 |
self::assertContains("< 2) Name: '', Function: callable(['GuzzleHttp\\Tests\\HandlerStackTest', 'bar'])", $lines[6]); |
| 108 |
self::assertContains("< 3) Name: '', Function: callable(GuzzleHttp\\Tests\\HandlerStackTest::foo)", $lines[7]); |
| 109 |
self::assertContains("< 4) Name: 'a', Function: callable(", $lines[8]); |
| 110 |
} |
| 111 |
|
| 112 |
public function testCanAddBeforeByName() |
| 113 |
{ |
| 114 |
$meths = $this->getFunctions(); |
| 115 |
$builder = new HandlerStack(); |
| 116 |
$builder->setHandler($meths[1]); |
| 117 |
$builder->push($meths[2], 'foo'); |
| 118 |
$builder->before('foo', $meths[3], 'baz'); |
| 119 |
$builder->before('baz', $meths[4], 'bar'); |
| 120 |
$builder->before('baz', $meths[4], 'qux'); |
| 121 |
$lines = explode("\n", (string) $builder); |
| 122 |
self::assertContains('> 4) Name: \'bar\'', $lines[0]); |
| 123 |
self::assertContains('> 3) Name: \'qux\'', $lines[1]); |
| 124 |
self::assertContains('> 2) Name: \'baz\'', $lines[2]); |
| 125 |
self::assertContains('> 1) Name: \'foo\'', $lines[3]); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @expectedException \InvalidArgumentException |
| 130 |
*/ |
| 131 |
public function testEnsuresHandlerExistsByName() |
| 132 |
{ |
| 133 |
$builder = new HandlerStack(); |
| 134 |
$builder->before('foo', function () { |
| 135 |
}); |
| 136 |
} |
| 137 |
|
| 138 |
public function testCanAddAfterByName() |
| 139 |
{ |
| 140 |
$meths = $this->getFunctions(); |
| 141 |
$builder = new HandlerStack(); |
| 142 |
$builder->setHandler($meths[1]); |
| 143 |
$builder->push($meths[2], 'a'); |
| 144 |
$builder->push($meths[3], 'b'); |
| 145 |
$builder->after('a', $meths[4], 'c'); |
| 146 |
$builder->after('b', $meths[4], 'd'); |
| 147 |
$lines = explode("\n", (string) $builder); |
| 148 |
self::assertContains('4) Name: \'a\'', $lines[0]); |
| 149 |
self::assertContains('3) Name: \'c\'', $lines[1]); |
| 150 |
self::assertContains('2) Name: \'b\'', $lines[2]); |
| 151 |
self::assertContains('1) Name: \'d\'', $lines[3]); |
| 152 |
} |
| 153 |
|
| 154 |
public function testPicksUpCookiesFromRedirects() |
| 155 |
{ |
| 156 |
$mock = new MockHandler([ |
| 157 |
new Response(301, [ |
| 158 |
'Location' => 'http://foo.com/baz', |
| 159 |
'Set-Cookie' => 'foo=bar; Domain=foo.com' |
| 160 |
]), |
| 161 |
new Response(200) |
| 162 |
]); |
| 163 |
$handler = HandlerStack::create($mock); |
| 164 |
$request = new Request('GET', 'http://foo.com/bar'); |
| 165 |
$jar = new CookieJar(); |
| 166 |
$response = $handler($request, [ |
| 167 |
'allow_redirects' => true, |
| 168 |
'cookies' => $jar |
| 169 |
])->wait(); |
| 170 |
self::assertSame(200, $response->getStatusCode()); |
| 171 |
$lastRequest = $mock->getLastRequest(); |
| 172 |
self::assertSame('http://foo.com/baz', (string) $lastRequest->getUri()); |
| 173 |
self::assertSame('foo=bar', $lastRequest->getHeaderLine('Cookie')); |
| 174 |
} |
| 175 |
|
| 176 |
private function getFunctions() |
| 177 |
{ |
| 178 |
$calls = []; |
| 179 |
|
| 180 |
$a = function (callable $next) use (&$calls) { |
| 181 |
return function ($v) use ($next, &$calls) { |
| 182 |
$calls[] = ['a', $v]; |
| 183 |
return $next($v . '1'); |
| 184 |
}; |
| 185 |
}; |
| 186 |
|
| 187 |
$b = function (callable $next) use (&$calls) { |
| 188 |
return function ($v) use ($next, &$calls) { |
| 189 |
$calls[] = ['b', $v]; |
| 190 |
return $next($v . '2'); |
| 191 |
}; |
| 192 |
}; |
| 193 |
|
| 194 |
$c = function (callable $next) use (&$calls) { |
| 195 |
return function ($v) use ($next, &$calls) { |
| 196 |
$calls[] = ['c', $v]; |
| 197 |
return $next($v . '3'); |
| 198 |
}; |
| 199 |
}; |
| 200 |
|
| 201 |
$handler = function ($v) { |
| 202 |
return 'Hello - ' . $v; |
| 203 |
}; |
| 204 |
|
| 205 |
return [&$calls, $handler, $a, $b, $c]; |
| 206 |
} |
| 207 |
|
| 208 |
public static function foo() |
| 209 |
{ |
| 210 |
} |
| 211 |
public function bar() |
| 212 |
{ |
| 213 |
} |
| 214 |
} |
| 215 |
|