public function testUrl() { $app = $this->getApp(); $factory = new MessageFactory(); $request = $factory->createRequest('GET', '/'); $response = new Response(Response::HTTP_OK); $guzzle = $this->getMock('GuzzleHttp\\Client', ['get']); $guzzle->expects($this->once())->method('get')->with('http://loripsum.net/api/1/veryshort')->will($this->returnValue($request)); $app['guzzle.client'] = $guzzle; $app['prefill']->get('/1/veryshort'); }
public function testUrl() { $app = $this->getApp(); if ($app['deprecated.php']) { $factory = new RequestFactory(); $request = $factory->create('GET', '/'); $response = new V3Response('200'); $guzzle = $this->getMock('Guzzle\\Service\\Client', array('get', 'send')); $request->setClient($guzzle); $guzzle->expects($this->once())->method('send')->will($this->returnValue($response)); } else { $factory = new MessageFactory(); $request = $factory->createRequest('GET', '/'); $response = new Response('200'); $guzzle = $this->getMock('GuzzleHttp\\Client', array('get')); } $guzzle->expects($this->once())->method('get')->with('http://loripsum.net/api/1/veryshort')->will($this->returnValue($request)); $app['guzzle.client'] = $guzzle; $app['prefill']->get('/1/veryshort'); }
/** * Create a new request based on the HTTP method. * * This method accepts an associative array of request options. Below is a * brief description of each parameter. See * http://docs.guzzlephp.org/clients.html#request-options for a much more * in-depth description of each parameter. * * - headers: Associative array of headers to add to the request * - body: string|resource|array|StreamInterface request body to send * - json: mixed Uploads JSON encoded data using an application/json Content-Type header. * - query: Associative array of query string values to add to the request * - auth: array|string HTTP auth settings (user, pass[, type="basic"]) * - version: The HTTP protocol version to use with the request * - cookies: true|false|CookieJarInterface To enable or disable cookies * - allow_redirects: true|false|array Controls HTTP redirects * - save_to: string|resource|StreamInterface Where the response is saved * - events: Associative array of event names to callables or arrays * - subscribers: Array of event subscribers to add to the request * - exceptions: Specifies whether or not exceptions are thrown for HTTP protocol errors * - timeout: Timeout of the request in seconds. Use 0 to wait indefinitely * - connect_timeout: Number of seconds to wait while trying to connect. (0 to wait indefinitely) * - verify: SSL validation. True/False or the path to a PEM file * - cert: Path a SSL cert or array of (path, pwd) * - ssl_key: Path to a private SSL key or array of (path, pwd) * - proxy: Specify an HTTP proxy or hash of protocols to proxies * - debug: Set to true or a resource to view handler specific debug info * - stream: Set to true to stream a response body rather than download it all up front * - expect: true/false/integer Controls the "Expect: 100-Continue" header * - config: Associative array of request config collection options * - decode_content: true/false/string to control decoding content-encoding responses * * @param string $method HTTP method (GET, POST, PUT, etc.) * @param string|Url $url HTTP URL to connect to * @param array $options Array of options to apply to the request * * @return RequestInterface * @link http://docs.guzzlephp.org/clients.html#request-options */ public function createRequest($method, $url, array $options = []) { $request = parent::createRequest($method, $url, $options); $query = $request->getQuery(); $auth = $request->getConfig()->get('auth'); // The 'auth' configuration must be valid if (!is_array($auth) || count($auth) < 3 || $auth[2] != Client::$AUTH_TYPE) { throw new RuntimeException("Authorization information not provided"); } $id = $auth[0]; $secret = $auth[1]; // Add API User ID to the query $query->set('api_id', $id); // Add timestamp to the query if (!$query->hasKey('api_ts')) { $query->set('api_ts', time()); } // Add version to the query $query->set('api_ver', '2'); // Add hash to the query $hash = $this->generateMac($request->getUrl(), $request->getQuery()->toArray(), $secret); $query->set('api_mac', $hash); return $request; }
public function testCanUseCustomRequestOptions() { $c = false; $f = new MessageFactory(['foo' => function (RequestInterface $request, $value) use(&$c) { $c = true; $this->assertEquals('bar', $value); }]); $f->createRequest('PUT', 'http://f.com', ['headers' => ['Content-Type' => 'foo'], 'foo' => 'bar']); $this->assertTrue($c); }
public function testCanUseCustomSubclassesWithMethods() { (new ExtendedFactory())->createRequest('PUT', 'http://f.com', ['headers' => ['Content-Type' => 'foo'], 'foo' => 'bar']); try { $f = new MessageFactory(); $f->createRequest('PUT', 'http://f.com', ['headers' => ['Content-Type' => 'foo'], 'foo' => 'bar']); } catch (\InvalidArgumentException $e) { $this->assertContains('foo config', $e->getMessage()); } }
public function testAddsCookieUsingTrue() { $factory = new MessageFactory(); $request1 = $factory->createRequest('GET', '/', ['cookies' => true]); $request2 = $factory->createRequest('GET', '/', ['cookies' => true]); $listeners = function ($r) { return array_filter($r->getEmitter()->listeners('before'), function ($l) { return $l[0] instanceof Cookie; }); }; $this->assertSame($listeners($request1), $listeners($request2)); }