Exemplo n.º 1
0
    /**
     * testing _httpRequest
     *
     */
    public function testHttpRequest()
    {
        $consumer = new Zend_OpenId_ConsumerHelper(new Zend_OpenId_Consumer_Storage_File(dirname(__FILE__)."/_files/consumer"));
        $http = new Zend_Http_Client(null,
            array(
                'maxredirects' => 4,
                'timeout'      => 15,
                'useragent'    => 'Zend_OpenId'
            ));
        $test = new Zend_Http_Client_Adapter_Test();
        $http->setAdapter($test);
        $consumer->SetHttpClient($http);
        $this->assertSame( $http, $consumer->GetHttpClient() );
        $this->assertFalse( $consumer->httpRequest(self::SERVER) );

        $test->setResponse("HTTP/1.1 200 OK\r\n\r\nok\n");

        // Test GET request without parameters
        $this->assertSame( "ok\n", $consumer->httpRequest(self::SERVER) );
        $this->assertSame( "GET / HTTP/1.1\r\n" .
                           "Host: www.myopenid.com\r\n" .
                           "Connection: close\r\n" .
                           "Accept-encoding: gzip, deflate\r\n" .
                           "User-agent: Zend_OpenId\r\n\r\n",
                           $http->getLastRequest() );

        // Test POST request without parameters
        $this->assertSame( "ok\n", $consumer->httpRequest(self::SERVER, 'POST') );
        $this->assertSame( "POST / HTTP/1.1\r\n" .
                           "Host: www.myopenid.com\r\n" .
                           "Connection: close\r\n" .
                           "Accept-encoding: gzip, deflate\r\n" .
                           "Content-type: application/x-www-form-urlencoded\r\n" .
                           "User-agent: Zend_OpenId\r\n\r\n",
                           $http->getLastRequest() );

        // Test GET request with parameters
        $this->assertSame( "ok\n", $consumer->httpRequest(self::SERVER . 'test.php', 'GET', array('a'=>'b','c'=>'d')) );
        $this->assertSame( "GET /test.php?a=b&c=d HTTP/1.1\r\n" .
                           "Host: www.myopenid.com\r\n" .
                           "Connection: close\r\n" .
                           "Accept-encoding: gzip, deflate\r\n" .
                           "User-agent: Zend_OpenId\r\n\r\n",
                           $http->getLastRequest() );

        // Test POST request with parameters
        $this->assertSame( "ok\n", $consumer->httpRequest(self::SERVER . 'test.php', 'POST', array('a'=>'b','c'=>'d')) );
        $this->assertSame( "POST /test.php HTTP/1.1\r\n" .
                           "Host: www.myopenid.com\r\n" .
                           "Connection: close\r\n" .
                           "Accept-encoding: gzip, deflate\r\n" .
                           "User-agent: Zend_OpenId\r\n" .
                           "Content-type: application/x-www-form-urlencoded\r\n" .
                           "Content-length: 7\r\n\r\n" .
                           "a=b&c=d",
                           $http->getLastRequest() );

        // Test GET parameters combination
        $this->assertSame( "ok\n", $consumer->httpRequest(self::SERVER . 'test.php?a=b', 'GET', array('c'=>'x y')) );
        $this->assertSame( "GET /test.php?a=b&c=x+y HTTP/1.1\r\n" .
                           "Host: www.myopenid.com\r\n" .
                           "Connection: close\r\n" .
                           "Accept-encoding: gzip, deflate\r\n" .
                           "User-agent: Zend_OpenId\r\n\r\n",
                           $http->getLastRequest() );

        // Test GET and POST parameters combination
        $this->assertSame( "ok\n", $consumer->httpRequest(self::SERVER . 'test.php?a=b', 'POST', array('c'=>'x y')) );
        $this->assertSame( "POST /test.php?a=b HTTP/1.1\r\n" .
                           "Host: www.myopenid.com\r\n" .
                           "Connection: close\r\n" .
                           "Accept-encoding: gzip, deflate\r\n" .
                           "User-agent: Zend_OpenId\r\n" .
                           "Content-type: application/x-www-form-urlencoded\r\n" .
                           "Content-length: 5\r\n\r\n" .
                           "c=x+y",
                           $http->getLastRequest() );
    }