Example #1
0
 private function getContent(Response $response)
 {
     if ($response->isContentType('xml')) {
         return $response->xml();
     } elseif ($response->isContentType('audio')) {
         return $response->getBody()->getStream();
     } else {
         return $response->getBody();
     }
 }
 /**
  * @param Response $response
  * @param string $clazz
  * @return mixed
  * @throws ClassCastException
  */
 protected function computeResponse($response, $clazz)
 {
     if (false === ($response->isContentType(Constants::CONTENT_TYPE_JSON) || $response->isContentType(Constants::CONTENT_TYPE_JSON_NXENTITY))) {
         if (Blob::className !== $clazz) {
             throw new ClassCastException(sprintf('Cannot cast %s as %s', Blob::className, $clazz));
         }
         return Blob::fromHttpResponse($response);
     }
     $body = $response->getBody(true);
     return $this->nuxeoClient->getConverter()->read($body, $clazz);
 }
 public static function getBody(GuzzleResponse $response)
 {
     $body = $response->getBody(true);
     // Response body is in JSON
     if ($response->isContentType('json')) {
         $tmp = json_decode($body, true);
         if (JSON_ERROR_NONE === json_last_error()) {
             $body = $tmp;
         }
     }
     return $body;
 }
 /**
  * @AfterScenario
  */
 public function printLastResponseOnError(BaseScenarioEvent $scenarioEvent)
 {
     if ($scenarioEvent->getResult() == StepEvent::FAILED) {
         if ($this->response) {
             $body = $this->getResponse()->getBody(true);
             // could we even ask them if they want to print out the error?
             // or do it based on verbosity
             // print some debug details
             $this->printDebug('');
             $this->printDebug('<error>Failure!</error> when making the following request:');
             $this->printDebug(sprintf('<comment>%s</comment>: <info>%s</info>', $this->lastRequest->getMethod(), $this->lastRequest->getUrl()) . "\n");
             if ($this->response->isContentType('application/json') || $this->response->isContentType('+json')) {
                 $data = json_decode($body);
                 if ($data === null) {
                     // invalid JSON!
                     $this->printDebug($body);
                 } else {
                     // valid JSON, print it pretty
                     $this->printDebug(json_encode($data, JSON_PRETTY_PRINT));
                 }
             } else {
                 // the response is HTML - see if we should print all of it or some of it
                 $isValidHtml = strpos($body, '</body>') !== false;
                 if ($this->useFancyExceptionReporting && $isValidHtml) {
                     $this->printDebug('<error>Failure!</error> Below is a summary of the HTML response from the server.');
                     // finds the h1 and h2 tags and prints them only
                     $crawler = new Crawler($body);
                     foreach ($crawler->filter('h1, h2')->extract(array('_text')) as $header) {
                         $this->printDebug(sprintf('        ' . $header));
                     }
                 } else {
                     $this->printDebug($body);
                 }
             }
         }
     }
 }
Example #5
0
 /**
  * @covers Guzzle\Http\Message\Response::isContentType
  */
 public function testComparesContentType()
 {
     $response = new Response(200, array('Content-Type' => 'text/html; charset=ISO-8859-4'));
     $this->assertTrue($response->isContentType('text/html'));
     $this->assertTrue($response->isContentType('TExT/html'));
     $this->assertTrue($response->isContentType('charset=ISO-8859-4'));
     $this->assertFalse($response->isContentType('application/xml'));
 }
 function it_fails_with_invalid_http_authentication_credentials($client, $clientParameters, Request $request, Response $response, Collection $curlOptions)
 {
     $guzzleParams = ['connect_timeout' => UrlExplorer::CONNECT_TIMEOUT, 'timeout' => UrlExplorer::TIMEOUT, 'auth' => ['bad_login', 'passwd']];
     $clientParameters->getHash()->willReturn('some_hash');
     $clientParameters->getHttpLogin()->willReturn('bad_login');
     $clientParameters->getHttpPassword()->willReturn('passwd');
     $clientParameters->getSoapUrl()->willReturn('http://myvalidsoap.url/api/soap/?wsdl');
     $client->get('http://myvalidsoap.url/api/soap/?wsdl', [], $guzzleParams)->willReturn($request);
     $request->getCurlOptions()->willReturn($curlOptions);
     $curlOptions->set(CURLOPT_CONNECTTIMEOUT, UrlExplorer::CONNECT_TIMEOUT)->willReturn($request);
     $curlOptions->set(CURLOPT_TIMEOUT, UrlExplorer::TIMEOUT)->willReturn($request);
     $client->send($request)->shouldBeCalled()->willThrow('Guzzle\\Http\\Exception\\BadResponseException');
     $response->isContentType(Argument::any())->shouldNotBeCalled();
     $response->getBody(Argument::any())->shouldNotBeCalled();
     $this->shouldThrow('\\Pim\\Bundle\\MagentoConnectorBundle\\Validator\\Exception\\InvalidSoapUrlException')->duringGetUrlContent($clientParameters);
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function isContentType($type)
 {
     return $this->response->isContentType($type);
 }