public function testAddsUserAgentIfAvailable() { $response = new Response(200); $response->setRequest(new Request('GET', 'http://www.foo.com', array('User-Agent' => 'Foo/Bar'))); $e = new ServiceResponseException('Foo!'); $e->setExceptionCode('foo'); $e->setExceptionType('client'); $e->setRequestId('xyz'); $e->setResponse($response); $this->assertEquals('Aws\\Common\\Exception\\ServiceResponseException: AWS Error Code: foo, Status Code: 200, AWS Request ID: xyz, AWS Error Type: client, AWS Error Message: Foo!, User-Agent: Foo/Bar', (string) $e); }
/** * {@inheritdoc} */ public function setResponse(Response $response, $queued = false) { // Never overwrite the request associated with the response (useful for redirect history) if (!$response->getRequest()) { $response->setRequest($this); } if ($queued) { $this->getEventDispatcher()->addListener('request.before_send', function ($e) use($response) { $e['request']->setResponse($response); }, -9999); } else { $this->response = $response; // If a specific response body is specified, then use it instead of the response's body if ($this->responseBody && !$this->responseBody->getCustomData('default')) { $this->getResponseBody()->write((string) $this->response->getBody()); } else { $this->responseBody = $this->response->getBody(); } $this->processResponse(); } return $this; }
/** * @covers Guzzle\Http\Message\Response::setRequest */ public function testSetRequest() { $response = new Response(200); $this->assertNull($response->getRequest()); $r = new \Guzzle\Http\Message\Request('GET', 'http://www.guzzle-project.com/'); $response->setRequest($r); $this->assertEquals($r, $response->getRequest()); }
/** * {@inheritdoc} */ public function setResponse(Response $response, $queued = false) { // Never overwrite the request associated with the response (useful for redirect history) if (!$response->getRequest()) { $response->setRequest($this); } if ($queued) { $this->getParams()->set('queued_response', $response); } else { $this->getParams()->remove('queued_response'); $this->response = $response; $this->responseBody = $response->getBody(); $this->processResponse(); } $this->dispatch('request.set_response', $this->getEventArray()); return $this; }
/** * If possible, return a cache response on an error * * @param Event $event */ public function onRequestError(Event $event) { $request = $event['request']; if (!$this->canCache->canCacheRequest($request)) { return; } $cacheKey = $this->keyProvider->getCacheKey($request); if ($cachedData = $this->storage->fetch($cacheKey)) { $response = new Response($cachedData[0], $cachedData[1], $cachedData[2]); $response->setRequest($request); $response->setHeader('Age', time() - strtotime($response->getLastModified() ?: $response->getDate() ?: 'now')); if ($this->canResponseSatisfyFailedRequest($request, $response)) { $request->getParams()->set('cache.hit', 'error'); $this->addResponseHeaders($cacheKey, $request, $response); $event['response'] = $response; $event->stopPropagation(); } } }
/** * {@inheritdoc} */ public function setResponse(Response $response, $queued = false) { $response->setRequest($this); if ($queued) { $this->getParams()->set('queued_response', $response); } else { $this->getParams()->remove('queued_response'); $this->response = $response; $this->responseBody = $response->getBody(); $this->processResponse(); } $this->dispatch('request.set_response', $this->getEventArray()); return $this; }
public function testAddsCookiesFromResponseWithRequest() { $response = new Response(200, array('Set-Cookie' => "fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT;")); $response->setRequest(new Request('GET', 'http://www.example.com')); $this->jar->addCookiesFromResponse($response); $this->assertEquals(1, count($this->jar)); }
/** * Set a request closure on a response * * @param Response $response * @deprecated */ protected function setRequestOnResponse(Response $response) { $headers = $this->getRawHeaders(); $response->setEffectiveUrl((string) $this->url); $response->setRequest(function () use($headers) { return RequestFactory::getInstance()->fromMessage($headers); }); }
/** * @covers Guzzle\Http\Message\Response::canCache */ public function testDeterminesIfItCanBeCached() { $this->assertTrue($this->getResponse(200)->canCache()); $this->assertTrue($this->getResponse(410)->canCache()); $this->assertFalse($this->getResponse(404)->canCache()); $this->assertTrue($this->getResponse(200, array('Cache-Control' => 'public'))->canCache()); // This has the no-store directive $this->assertFalse($this->getResponse(200, array('Cache-Control' => 'private, no-store'))->canCache()); // The body cannot be read, so it cannot be cached $tmp = tempnam('/tmp', 'not-readable'); $resource = fopen($tmp, 'w'); $this->assertFalse($this->getResponse(200, array('Transfer-Encoding' => 'chunked'), EntityBody::factory($resource, 10))->canCache()); unlink($tmp); // The body is 0 length, cannot be read, so it can be cached $tmp = tempnam('/tmp', 'not-readable'); $resource = fopen($tmp, 'w'); $this->assertTrue($this->getResponse(200, array(array('Content-Length' => 0)), EntityBody::factory($resource, 0))->canCache()); unlink($tmp); // When an HTTPS request is sent and the Cache-Control directive does // not include a 'public' value, then the response is not to be cached $request = RequestFactory::getInstance()->create('GET', 'https://www.test.com/'); $response = new Response(200); $response->setRequest($request); $this->assertFalse($response->canCache()); // This response can be cache because it is public $response->setHeader('Cache-Control', 'public'); $this->assertTrue($response->canCache()); }
/** * Overrides default Guzzle usage of curl_multi_exec by a simple curl call * and adds some metrics in Newrelic * * @param EntityEnclosingRequest $request * @return Response */ public static function sendRequest( EntityEnclosingRequest $request ) { $ch = curl_init($request->getUrl()); $curlOptions = array( CURLOPT_URL => $request->getUrl(), CURLOPT_TIMEOUT => \eZINI::instance('merck.ini')->variable('WebService', 'ESBTimeout'), CURLOPT_CONNECTTIMEOUT => \eZINI::instance('merck.ini')->variable('WebService', 'ESBConnectTimeout'), CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_USERAGENT => (string) $request->getHeader('User-Agent'), CURLOPT_PORT => $request->getPort(), CURLOPT_HTTPHEADER => $request->getHeaderLines(), CURLOPT_HTTP_VERSION => $request->getProtocolVersion() === '1.0' ? CURL_HTTP_VERSION_1_0 : CURL_HTTP_VERSION_1_1, // Verifies the authenticity of the peer's certificate CURLOPT_SSL_VERIFYPEER => 1, // Certificate must indicate that the server is the server to which you meant to connect CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_POST => true, CURLOPT_POSTFIELDS => (string) $request->getBody(), CURLOPT_VERBOSE => false, ); curl_setopt_array($ch, $curlOptions); $start = microtime(true); $curlResult = curl_exec( $ch ); $curlCallTime = ( microtime(true) - $start ) * 1000; $newRelicApi = new \klpNrApi(); $methodLabel = preg_match('#/(?P<method>[^/?]+)(?:\?.*)?$#', $request->getUrl(), $m) ? ucfirst( $m['method'] ) : 'Unknown'; $newRelicApi->addParameter( 'ESB/'.$methodLabel.':'.$request->getPort(), $curlCallTime ); $newRelicApi->setCustomMetric( 'Custom/ESB/'.$methodLabel, $curlCallTime ); $body = $curlResult; $maxHeaderBlocks = 5; $index = 0; // We have to deal with HTTP/1.1 100 continue headers do { list( $header, $body ) = explode("\r\n\r\n", $body, 2); $index++; } while( $index < $maxHeaderBlocks && strpos($body, 'HTTP/1') === 0 ); $parsedHeaders = array(); foreach( explode("\n", $header) as $h ) { list( $key, $val ) = explode(':', $h); $parsedHeaders[trim($key)] = trim($val); } $http_status = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); if ( \eZINI::instance('merck.ini')->variable( 'WebService', 'CurlGetInfo' ) == 'enabled' ) { $curlInfo = curl_getinfo($ch); $logMessage = \ClusterTool::clusterIdentifier().'::'.$methodLabel."\n"; $logMessage .= var_export( $curlInfo, true ); \eZLog::write($logMessage, 'esbcurldetails.log'); } curl_close($ch); $response = new Response( $http_status, $parsedHeaders, $body ); $response->setRequest($request); $request->setResponse($response); return $response; }