/** * Creates a Guzzle client for communicating with a Keystone service. * * @param Tenant $tenant The keystone tenant to authenticate with * @param array $config The client configuration * @param string $class Optionally override the Guzzle client class * * @throws RequestException When a new token could not be requested. * * @return ClientInterface */ public function createClient(Tenant $tenant, array $config = [], $class = null) { if (null === $class) { $class = $this->clientClass; } $pool = new TokenPool($tenant, $this->cache, $this->logger); $signer = new RequestSigner($pool); $stack = HandlerStack::create(); $stack->before('http_errors', Middleware::signRequest($signer), 'signer'); $stack->before('http_errors', Middleware::reauthenticate($signer), 'reauth'); return new $class(array_merge($config, ['handler' => $stack, 'token_pool' => $pool])); }
/** * @test */ public function it_signs_requests() { $signer = $this->getMockBuilder(RequestSigner::class)->disableOriginalConstructor()->setMethods(['signRequest'])->getMock(); $tokenId = 'abcd1234'; $request = new Request('GET', 'http://foo.com', ['X-Auth-Token' => $tokenId]); $signer->expects($this->once())->method('signRequest')->willReturn($request); $middleware = Middleware::signRequest($signer); $handler = new MockHandler([function (RequestInterface $request) use($tokenId) { $this->assertTrue($request->hasHeader('X-Auth-Token')); $this->assertEquals($tokenId, $request->getHeaderLine('X-Auth-Token')); return new Response(200); }]); $callback = $middleware($handler); /** @var ResponseInterface $response */ $response = $callback(new Request('GET', 'http://foo.com'), [])->wait(); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertEquals(200, $response->getStatusCode()); }