public function testEmitsEvents() { $request = new EntityEnclosingRequest('PUT', 'http://www.example.com'); $request->setBody('foo'); $request->setResponse(new Response(200)); // Ensure that IO events are emitted $request->getCurlOptions()->set('emit_io', true); // Attach listeners for each event type $request->getEventDispatcher()->addListener('curl.callback.progress', array($this, 'event')); $request->getEventDispatcher()->addListener('curl.callback.read', array($this, 'event')); $request->getEventDispatcher()->addListener('curl.callback.write', array($this, 'event')); $mediator = new RequestMediator($request, true); $mediator->progress('a', 'b', 'c', 'd'); $this->assertEquals(1, count($this->events)); $this->assertEquals('curl.callback.progress', $this->events[0]->getName()); $this->assertEquals(3, $mediator->writeResponseBody('foo', 'bar')); $this->assertEquals(2, count($this->events)); $this->assertEquals('curl.callback.write', $this->events[1]->getName()); $this->assertEquals('bar', $this->events[1]['write']); $this->assertSame($request, $this->events[1]['request']); $this->assertEquals('foo', $mediator->readRequestBody('a', 'b', 3)); $this->assertEquals(3, count($this->events)); $this->assertEquals('curl.callback.read', $this->events[2]->getName()); $this->assertEquals('foo', $this->events[2]['read']); $this->assertSame($request, $this->events[2]['request']); }
public function testGetBody() { $request1 = $this->getRequest(); $this->assertEquals('', $request1->getBody()); $guzzleRequest = new EntityEnclosingRequest('GET', 'http://example.com'); $guzzleRequest->setBody('test content'); $request2 = new Guzzle3($guzzleRequest); $this->assertEquals('test content', $request2->getBody()); }
public function testAddsContentSha256WhenBodyIsPresent() { $request = new EntityEnclosingRequest('PUT', 'http://foo.com'); $request->setBody('foo'); $credentials = new Credentials('foo', 'bar'); $signature = new S3SignatureV4('service', 'region'); $signature->signRequest($request, $credentials); $this->assertEquals(hash('sha256', 'foo'), $request->getHeader('x-amz-content-sha256')); }
public function testAuthorizationHeader() { $plugin = $this->getPlugin(); $uri = 'http://example.com/resource/1?key=value'; $request = new EntityEnclosingRequest('GET', $uri, array('Content-Type' => 'text/plain', 'Date' => 'Fri, 19 Mar 1982 00:00:04 GMT', 'Custom1' => 'Value1')); $request->setBody('test content'); $plugin->signRequest($request); $expected = 'Acquia 1:' . DigestVersion1Test::EXPECTED_HASH; $this->assertEquals($expected, (string) $request->getHeader('Authorization')); }
/** * @return EntityBody|\Guzzle\Http\EntityBodyInterface|null */ public function getBody() { if (!$this->body) { $body = ''; $contentType = $this->originalContentType ?: $this->getHeader('Content-Type'); if ($this->relatedParts) { $parts = array_merge(array(new RelatedString($this->originalBody, $contentType)), $this->relatedParts); foreach (new MultipartRelatedIterator($parts, '--' . $this->boundary) as $part) { $body .= $part; } $contentType = sprintf('%s;boundary=%s', self::MULTIPART_RELATED, $this->boundary); } else { $body = $this->originalBody; } parent::setBody($body, $contentType); } return parent::getBody(); }
/** * Test valid signature for entity-body style requests (POST, PUT, PATCH, DELETE). */ public function testValidSignatureWithEntityBody() { $method = 'POST'; $body = '{hello: "world!"}'; $path = '/say/hello'; $secret = '456'; $queryParams = array('fiz' => 'buzz', 'foo' => 'bar'); $request = new EntityEnclosingRequest($method, $path); $request->setBody($body); $signature = new Signature($secret, $request); $expected = $secret . $method . $path; ksort($queryParams); foreach ($queryParams as $key => $param) { $expected .= "{$key}={$param}"; $request->getQuery()->set($key, $param); } $expected .= $body; $this->assertEquals($expected, $signature->getRawSignature(), 'Raw signature should include entity-body string.'); $this->assertEquals(43, strlen($signature), 'Hash signature should be 43 characters in length.'); }
/** * @return string */ public function send($xml, Port $port, BindingOperation $bindingOperation) { $url = $port->getDomElement()->evaluate("string(soap:address/@location)", array("soap" => SoapClient::NS)); $soapAction = $bindingOperation->getDomElement()->evaluate("string(soap:operation/@soapAction)", array("soap" => SoapClient::NS)); $request = new EntityEnclosingRequest("POST", $url); $request->setBody($xml, "text/xml; charset=utf-8"); $request->addHeader("SOAPAction", '"' . $soapAction . '"'); if ($this->debugUri) { $request->setUrl($this->debugUri); } try { $response = $this->client->send($request); } catch (BadResponseException $e) { $response = $e->getResponse(); } if (!$response->isSuccessful() && strpos($response->getContentType(), '/xml') === false) { throw new TransportException($response->getReasonPhrase(), $response->getStatusCode()); } return $response->getBody(true); }
/** * @return string */ public function send($xml, Port $port, BindingOperation $bindingOperation) { $soapAction = $bindingOperation->getDomElement()->evaluate("string(soap:operation/@soapAction)", array("soap" => SoapClient::NS)); if (!$soapAction) { $soapActionRequired = $bindingOperation->getDomElement()->evaluate("string(soap:operation/@soapActionRequired)", array("soap" => SoapClient::NS)); if ($soapActionRequired == "true") { throw new TransportException("SoapAction required for operation '" . $bindingOperation->getName() . "'", 100); } } $url = $port->getDomElement()->evaluate("string(soap:address/@location)", array("soap" => SoapClient::NS)); $request = new EntityEnclosingRequest("POST", $url); $request->setBody($xml, 'application/soap+xml; charset=utf-8; action="' . $soapAction . '"'); if ($this->debugUri) { $request->setUrl($this->debugUri); } $response = $this->client->send($request); if (!$response->isSuccessful()) { throw new TransportException($response->getReasonPhrase(), $response->getStatusCode()); } return $response->getBody(true); }
/** * This function is used to send any kind of request to Nuxeo EM * @return NuxeoDocuments|string */ public function sendRequest() { if (!$this->blobList) { $content = str_replace('\\/', '/', json_encode($this->finalRequest, JSON_FORCE_OBJECT)); $this->request->setBody($content); $answer = ''; try { $response = $this->request->send(); $answer = $response->getBody(true); } catch (RequestException $ex) { throw new NuxeoClientException("error", NuxeoClientException::INTERNAL_ERROR_STATUS, $ex); } if (null == json_decode($answer, true)) { $documents = $answer; file_put_contents("tempstream", $answer); } else { $answer = json_decode($answer, true); $documents = new NuxeoDocuments($answer); } return $documents; } else { return $this->multiPart(); } }
/** * This function is used to send any kind of request to Nuxeo EM * @return NuxeoDocuments|string */ public function sendRequest() { if (!$this->blobList) { $content = str_replace('\\/', '/', json_encode($this->finalRequest)); $this->request->setBody($content); $answer = ''; try { $response = $this->request->send(); $answer = $response->getBody(true); } catch (RequestException $ex) { echo 'Error Server'; } if (null == json_decode($answer, true)) { $documents = $answer; file_put_contents("tempstream", $answer); } else { $answer = json_decode($answer, true); $documents = new NuxeoDocuments($answer); } return $documents; } else { return $this->multiPart(); } }
/** * @covers Guzzle\Http\Message\EntityEnclosingRequest::setExpectHeaderCutoff * @covers Guzzle\Http\Message\EntityEnclosingRequest::setBody */ public function testSettingExpectHeaderCutoffChangesRequest() { $request = new EntityEnclosingRequest('PUT', 'http://test.com/'); $request->setHeader('Expect', '100-Continue'); $request->setExpectHeaderCutoff(false); $this->assertNull($request->getHeader('Expect')); // There is not body, so remove the expect header $request->setHeader('Expect', '100-Continue'); $request->setExpectHeaderCutoff(10); $this->assertNull($request->getHeader('Expect')); // The size is less than the cutoff $request->setBody('foo'); $this->assertNull($request->getHeader('Expect')); // The size is greater than the cutoff $request->setBody('foobazbarbamboo'); $this->assertNotNull($request->getHeader('Expect')); }
public function testSeeksToBeginningOfRequestBodyWhenRetrying() { // Create a request with a body $request = new EntityEnclosingRequest('PUT', 'http://www.example.com'); $request->setBody('abc'); // Set the retry time to be something that will be retried always $request->getParams()->set(BackoffPlugin::DELAY_PARAM, 2); // Seek to the end of the stream $request->getBody()->seek(3); $this->assertEquals('', $request->getBody()->read(1)); // Create a plugin that does not delay when retrying $plugin = new BackoffPlugin(new ConstantBackoffStrategy(0)); $plugin->onRequestPoll($this->getMockEvent($request)); // Ensure that the stream was seeked to 0 $this->assertEquals('a', $request->getBody()->read(1)); }
public function testDoesNotCloneBody() { $request = new EntityEnclosingRequest('PUT', 'http://test.com/foo'); $request->setBody('test'); $newRequest = clone $request; $newRequest->setBody('foo'); $this->assertInternalType('string', (string) $request->getBody()); }
/** * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::onRequestPoll */ public function testSeeksToBeginningOfRequestBodyWhenRetrying() { // Create a request with a body $request = new EntityEnclosingRequest('PUT', 'http://www.example.com'); $request->setBody('abc'); // Set the retry time to be something that will be retried always $request->getParams()->set('plugins.exponential_backoff.retry_time', 2); // Seek to the end of the stream $request->getBody()->seek(3); $this->assertEquals('', $request->getBody()->read(1)); // Create a plugin that does not delay when retrying $plugin = new ExponentialBackoffPlugin(2, null, array($this, 'delayClosure')); $plugin->onRequestPoll($this->getMockEvent($request)); // Ensure that the stream was seeked to 0 $this->assertEquals('a', $request->getBody()->read(1)); }
/** * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::onRequestPoll */ public function testSeeksToBeginningOfRequestBodyWhenRetrying() { // Create a mock curl multi object $multi = $this->getMockBuilder('Guzzle\\Http\\Curl\\CurlMulti')->setMethods(array('remove', 'add'))->getMock(); // Create a request with a body $request = new EntityEnclosingRequest('PUT', 'http://www.example.com'); $request->setBody('abc'); // Set the retry time to be something that will be retried always $request->getParams()->set('plugins.exponential_backoff.retry_time', 2); // Seek to the end of the stream $request->getBody()->seek(3); $this->assertEquals('', $request->getBody()->read(1)); // Create a plugin that does not delay when retrying $plugin = new ExponentialBackoffPlugin(2, null, array($this, 'delayClosure')); // Create an event that is expected for the Poll event $event = new Event(array('request' => $request, 'curl_multi' => $multi)); $event->setName(CurlMultiInterface::POLLING_REQUEST); $plugin->onRequestPoll($event); // Ensure that the stream was seeked to 0 $this->assertEquals('a', $request->getBody()->read(1)); }
/** * @covers Guzzle\Http\Message\EntityEnclosingRequest::setBody * @expectedException Guzzle\Http\Exception\RequestException */ public function testThrowsExceptionWhenContentLengthCannotBeDeterminedAndUsingHttp1() { $request = new EntityEnclosingRequest('PUT', 'http://test.com/'); $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); $request->setProtocolVersion('1.0'); $request->setBody(fopen($this->getServer()->getUrl(), 'r')); }
/** * @covers Guzzle\Http\Message\EntityEnclosingRequest::setBody */ public function testSetsContentTypeWhenSettingBodyByGuessingFromEntityBody() { $request = new EntityEnclosingRequest('PUT', 'http://test.com/foo'); $request->setBody(EntityBody::factory(fopen(__FILE__, 'r'))); $this->assertEquals('text/x-php', (string) $request->getHeader('Content-Type')); }