public function testAddsBody()
 {
     $this->getServer()->flush();
     $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi");
     $request = $this->client->put('/', array('Foo' => 'Bar'), 'Testing...123');
     $stream = $this->factory->fromRequest($request);
     $this->assertEquals('hi', (string) $stream);
     $headers = $this->factory->getLastResponseHeaders();
     $this->assertContains('HTTP/1.1 200 OK', $headers);
     $this->assertContains('Content-Length: 2', $headers);
     $this->assertSame($headers, $stream->getCustomData('response_headers'));
     $received = $this->getServer()->getReceivedRequests();
     $this->assertEquals(1, count($received));
     $this->assertContains('PUT / HTTP/1.1', $received[0]);
     $this->assertContains('host: ', $received[0]);
     $this->assertContains('user-agent: Guzzle/', $received[0]);
     $this->assertContains('foo: Bar', $received[0]);
     $this->assertContains('content-length: 13', $received[0]);
     $this->assertContains('Testing...123', $received[0]);
 }
Exemple #2
0
 /**
  * Get the streaming contents of the specified resource.
  *
  * @param string $uri
  *            Resource URI
  *            
  * @return EntityBody Returns the stream resource on success or false on failure
  * @throws \RuntimeException If the stream cannot be opened or an error occurs
  */
 public function getStream($uri)
 {
     $request = $this->createRequest('GET', $uri);
     $factory = new PhpStreamRequestFactory();
     $stream = $factory->fromRequest($request, array(), array('stream_class' => 'Guzzle\\Http\\EntityBody'));
     // The implementation of streaming download proposed by Guzzle's EntityBody class does not care about
     // HTTP errors. As a workaround, let's rebuild the HTTP response from the response headers sent in the
     // $http_response_header variable (http://www.php.net/manual/en/reserved.variables.httpresponseheader.php)
     $response = HttpResponse::fromMessage(implode("\r\n", $factory->getLastResponseHeaders()));
     // Creates History
     $this->lastRequest = $request;
     $this->lastResponse = $response;
     if (!$response->isSuccessful()) {
         $stream = false;
     }
     if (!$stream && $this->throwExceptions) {
         switch ($response->getStatusCode()) {
             case 404:
                 throw new NoSuchResourceException('No such file or directory');
             default:
                 throw new \RuntimeException($response->getReasonPhrase(), $response->getStatusCode());
         }
     }
     return $stream;
 }
 /**
  * Process request
  *
  * @return mixed
  */
 public function sendData($data)
 {
     if (true == $this->getParameter('testMode')) {
         $data['username'] = '******';
         $data['password'] = '******';
     } else {
         $data['username'] = $this->getUsername();
         $data['password'] = $this->getPassword();
     }
     // don't throw exceptions for 4xx errors
     $this->httpClient->getEventDispatcher()->addListener('request.error', function ($event) {
         if ($event['response']->isClientError()) {
             $event->stopPropagation();
         }
     });
     $httpRequest = $this->httpClient->createRequest($this->getHttpMethod(), $this->getEndpoint(), null, $data);
     // Might be useful to have some debug code here.  Perhaps hook to whatever
     // logging engine is being used.
     // echo "Data == " . json_encode($data) . "\n";
     //$httpResponse = $httpRequest->send();
     $factory = new PhpStreamRequestFactory();
     $stream = $factory->fromRequest($httpRequest);
     // Read until the stream is closed
     while (!$stream->feof()) {
         // Read a line from the stream
         $line = $stream->readLine();
         // JSON decode the line of data
     }
     parse_str($line, $output);
     return $this->response = new Response($this, $output, $factory->getLastResponseHeaders());
 }