Пример #1
0
 /**
  * @covers Guzzle\Http\Message\EntityEnclosingRequest::setState
  */
 public function testSetStateToTransferWithEmptyBodySetsContentLengthToZero()
 {
     $request = new EntityEnclosingRequest('POST', 'http://test.com/');
     $request->setState($request::STATE_TRANSFER);
     $this->assertEquals('0', (string) $request->getHeader('Content-Length'));
 }
Пример #2
0
 /**
  * @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'));
 }
    /**
     * 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;
    }
 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'));
 }
Пример #5
0
 /**
  * @covers Guzzle\Http\Message\EntityEnclosingRequest::setBody
  */
 public function testUsesChunkedTransferWhenBodyLengthCannotBeDetermined()
 {
     $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
     $request = new EntityEnclosingRequest('PUT', 'http://test.com/');
     $request->setBody(fopen($this->getServer()->getUrl(), 'r'));
     $this->assertEquals('chunked', $request->getHeader('Transfer-Encoding'));
     $this->assertFalse($request->hasHeader('Content-Length'));
 }