public function testSetManualResponse() { $response = new Response(200, array('Date' => 'Sat, 16 Oct 2010 17:27:14 GMT', 'Expires' => '-1', 'Cache-Control' => 'private, max-age=0', 'Content-Type' => 'text/html; charset=ISO-8859-1'), 'response body'); $this->assertSame($this->request, $this->request->setResponse($response), '-> setResponse() must use a fluent interface'); $this->assertEquals('complete', $this->request->getState(), '-> setResponse() must change the state of the request to complete'); $this->assertSame($response, $this->request->getResponse(), '-> setResponse() must set the exact same response that was passed in to it'); }
public function sendRequest() { $client = new Client(); // make a request object $request = $client->createRequest($this->requestType, $this->host . $this->resource); // set headers foreach ($this->headers as $header => $value) { $request->setHeader($header, $value); } // set body if (!empty($this->body)) { $request->setBody($this->body); } $this->request = $request; try { // finally send the request $this->response = $request->send(); } catch (\Exception $e) { $this->response = $this->request->getResponse(); } }
/** * Send the request */ public function send() { // make a request object $this->request = $this->client->createRequest($this->method, $this->host . $this->resource); // set headers foreach ($this->headers as $header => $value) { $this->request->setHeader($header, $value); } // set body if (!empty($this->body)) { $this->request->setBody($this->body); } try { // finally send the request $this->response = $this->client->send($this->request); } catch (BadResponseException $e) { $this->response = $this->request->getResponse(); } $this->sent = true; }
/** * * @return \Guzzle\Http\Message\Response */ public function getRawResponse() { return $this->_request->getResponse(); }
/** * @param Request $request * @param string $body */ private function processSuccessfulRequest(Request $request, $body) { $response = $request->getResponse(); $headers = $request->getHeaders()->getAll(); $responseBody = $response->getBody(true); $status = $response->getStatusCode(); $this->lastRequest['response']['body'] = $responseBody; $this->lastRequest['response']['status'] = $status; $this->logRequestSuccess($request->getMethod(), $request->getUrl(), $body, $headers, $status, $responseBody, $response->getInfo('total_time')); }
/** * Many RESTful frameworks omit the text status from the header. That * provides a response like "HTTP/1.1 200". Prevent an Undefined offset * by checking to see how many parts of the status line are provided * before trying to assign them. * * @covers Guzzle\Http\Message\Request::receiveResponseHeader */ public function testReceivingShortStatusLineResponse() { $request = new Request('GET', $this->getServer()->getUrl()); $request->receiveResponseHeader('HTTP/1.1 200'); $this->assertSame(200, $request->getResponse()->getStatusCode()); $this->assertSame('OK', $request->getResponse()->getReasonPhrase()); }
/** * Users sometimes want to use a custom stream when receiving a response body. * Because of the various potential for retrying failed requests, the stream * specified by the user should only be written to in the event that a * successful response was received. Otherwise, a new temp stream is created * to store the body of the failed request. * * @covers Guzzle\Http\Message\Request::receiveResponseHeader */ public function testReceivingUnsuccessfulResponseUsesOtherResponseBody() { $request = new Request('GET', $this->getServer()->getUrl()); $body = EntityBody::factory(); $request->setResponseBody($body); $request->receiveResponseHeader('HTTP/1.1 503 Service Unavailable'); $this->assertNotSame($body, $request->getResponse()->getBody()); }
/** * @dataProvider unsatisfiableOnErrorProvider */ public function testDoesNotInjectUnsatisfiableResponsesOnException($requestCanCache, $requestHeaders, $responseParts) { $storage = $this->getMockBuilder('Guzzle\\Plugin\\Cache\\CacheStorageInterface')->setMethods(array('fetch'))->getMockForAbstractClass(); $storage->expects($this->exactly($requestCanCache ? 2 : 0))->method('fetch')->will($this->returnValue($responseParts)); $plugin = new CachePlugin(array('storage' => $storage)); $request = new Request('GET', 'http://foo.com', $requestHeaders); $plugin->onRequestBeforeSend(new Event(array('request' => $request))); $plugin->onRequestException($event = new Event(array('request' => $request, 'response' => $response = $request->getResponse(), 'exception' => $this->getMock('Guzzle\\Http\\Exception\\CurlException')))); $this->assertSame($response, $request->getResponse()); }
public function testInjectsSatisfiableResponses() { $storage = $this->getMockBuilder('Guzzle\\Plugin\\Cache\\CacheStorageInterface')->setMethods(array('fetch'))->getMockForAbstractClass(); $storage->expects($this->once())->method('fetch')->will($this->returnValue(array(200, array(), 'foo'))); $plugin = new CachePlugin(array('storage' => $storage)); $request = new Request('GET', 'http://foo.com'); $plugin->onRequestBeforeSend(new Event(array('request' => $request))); $this->assertEquals(200, $request->getResponse()->getStatusCode()); $this->assertEquals('foo', $request->getResponse()->getBody(true)); $this->assertContains('key=', (string) $request->getResponse()->getHeader('X-Guzzle-Cache')); $this->assertTrue($request->getResponse()->hasHeader('Age')); }