Changeset 1493

Show
Ignore:
Timestamp:
04/04/08 17:08:47 (3 months ago)
Author:
mikey
Message:

added unit test for net::stubbles::peer::http::stubHTTPRequest
split send() method into different methods equaling http methods

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/peer/http/stubHTTPConnection.php

    r1492 r1493  
    155155     * returns response object for given URL after GET request 
    156156     * 
     157     * @param   string            $version  optional  HTTP version 
    157158     * @return  stubHTTPResponse 
    158159     */ 
    159     public function get(
    160     { 
    161         return $this->createRequest()->send(stubHTTPRequest::METHOD_GET); 
     160    public function get($version = null
     161    { 
     162        return $this->createRequest()->get($version); 
    162163    } 
    163164 
     
    165166     * returns response object for given URL after HEAD request 
    166167     * 
     168     * @param   string            $version  optional  HTTP version 
    167169     * @return  stubHTTPResponse 
    168170     */ 
    169     public function head(
    170     { 
    171         return $this->createRequest()->send(stubHTTPRequest::METHOD_HEAD); 
     171    public function head($version = null
     172    { 
     173        return $this->createRequest()->head($version); 
    172174    } 
    173175 
     
    176178     * 
    177179     * @param   array<string,string>  $postValues  post data to send with POST request 
     180     * @param   string                $version     optional  HTTP version 
    178181     * @return  stubHTTPResponse 
    179182     */ 
    180     public function post($postValues
    181     { 
    182         return $this->createRequest()->preparePost($postValues)->send(stubHTTPRequest::METHOD_POST); 
     183    public function post(array $postValues, $version = null
     184    { 
     185        return $this->createRequest()->preparePost($postValues)->post($version); 
    183186    } 
    184187 
  • trunk/src/main/php/net/stubbles/peer/http/stubHTTPRequest.php

    r1486 r1493  
    99stubClassLoader::load('net::stubbles::peer::stubHeaderList', 
    1010                      'net::stubbles::peer::stubConnectionException', 
    11                       'net::stubbles::peer::stubSocket' 
     11                      'net::stubbles::peer::stubSocket', 
     12                      'net::stubbles::peer::http::stubHTTPConnection', 
     13                      'net::stubbles::peer::http::stubHTTPResponse', 
     14                      'net::stubbles::peer::http::stubHTTPURL' 
    1215); 
    1316/** 
     
    2427     * @var  stubHTTPURL 
    2528     */ 
    26     protected $http    = null; 
     29    protected $httpURL = null; 
    2730    /** 
    2831     * contains request headers 
     
    6770     * constructor 
    6871     * 
    69      * @param  stubHTTPURL     $http     HTTP URL to perform a request to 
     72     * @param  stubHTTPURL     $httpURL  HTTP URL to perform a request to 
    7073     * @param  stubHeaderList  $header   list of request headers 
    7174     * @param  int             $timeout  timeout for connection in seconds 
    7275     */ 
    73     public function __construct(stubHTTPURL $http, stubHeaderList $header, $timeout) 
     76    public function __construct(stubHTTPURL $httpURL, stubHeaderList $header, $timeout) 
    7477    { 
    75         $this->http    = $http
     78        $this->httpURL = $httpURL
    7679        $this->headers = $header; 
    7780        $this->timeout = $timeout; 
     
    7982 
    8083    /** 
    81      * factory method for more fluent use 
     84     * initializes a get request 
    8285     * 
    83      * @param   stubHTTPURL      $http     HTTP URL to perform a request to 
    84      * @param   stubHeaderList   $header   list of request headers 
    85      * @param   int              $timeout  timeout for connection in seconds 
    86      * @return  stubHTTPRequest 
     86     * @param   string            $version  optional  HTTP version 
     87     * @return  stubHTTPResponse 
    8788     */ 
    88     public static function create(stubHTTPURL $http, stubHeaderList $header, $timeout
     89    public function get($version = null
    8990    { 
    90         return new self($http, $header, $timeout); 
     91        $socket = $this->createSocket(); 
     92        $this->processHeader($socket, self::METHOD_GET, $version); 
     93        $socket->write(stubHTTPConnection::END_OF_LINE); 
     94        return new stubHTTPResponse($socket); 
     95    } 
     96 
     97    /** 
     98     * initializes a head request 
     99     * 
     100     * @param   string            $version  optional  HTTP version 
     101     * @return  stubHTTPResponse 
     102     */ 
     103    public function head($version = null) 
     104    { 
     105        $socket = $this->createSocket(); 
     106        $this->processHeader($socket, self::METHOD_HEAD, $version); 
     107        $socket->write('Connection: close' . stubHTTPConnection::END_OF_LINE . stubHTTPConnection::END_OF_LINE); 
     108        return new stubHTTPResponse($socket); 
    91109    } 
    92110 
     
    94112     * creates required headers for post request and encodes post values 
    95113     * 
    96      * @param   array<string,string>  $postValue  post values to submit 
     114     * @param   array<string,string>  $postValues  post values to submit 
    97115     * @return  stubHTTPRequest 
    98116     */ 
    99     public function preparePost(array $postValue
     117    public function preparePost(array $postValues
    100118    { 
    101         foreach ($postValue as $key => $value) { 
     119        foreach ($postValues as $key => $value) { 
    102120            $this->body .= urlencode($key) . '=' . urlencode($value) . '&'; 
    103121        } 
     
    109127 
    110128    /** 
    111      * do a request on the given URL 
     129     * initializes a post request 
    112130     * 
    113      * @param   string            $method   request type, allowed: POST, GET, PUT, standard: GET 
    114      * @param   string            $version  optional  version of HTTP-protocol to use, standard: HTTP/1.0 
    115      * @return  stubHTTPResponse  response of given request 
     131     * @param   string            $version  optional  HTTP version 
     132     * @return  stubHTTPResponse 
    116133     */ 
    117     public function send($method, $version = null) 
     134    public function post($version = null) 
     135    { 
     136        $socket = $this->createSocket(); 
     137        $this->processHeader($socket, self::METHOD_POST, $version); 
     138        $socket->write(stubHTTPConnection::END_OF_LINE); 
     139        $socket->write($this->body); 
     140        return new stubHTTPResponse($socket); 
     141    } 
     142 
     143    /** 
     144     * creates the socket 
     145     * 
     146     * @return  stubSocket 
     147     */ 
     148    protected function createSocket() 
     149    { 
     150        return new stubSocket($this->httpURL->getHost(), $this->httpURL->getPort()); 
     151    } 
     152 
     153    /** 
     154     * helper method to send the headers 
     155     * 
     156     * @param  stubSocket  $socket   the socket to write headers to 
     157     * @param  string      $method   HTTP method 
     158     * @param  string      $version  HTTP version 
     159     */ 
     160    protected function processHeader(stubSocket $socket, $method, $version) 
    118161    { 
    119162        if (self::METHOD_POST != $method && self::METHOD_GET != $method && self::METHOD_HEAD != $method) { 
     
    125168        } 
    126169         
    127         $socket = $this->createSocket(); 
    128170        $socket->setTimeout($this->timeout); 
    129171        $socket->connect(); 
    130         $socket->write($method . ' ' . $this->http->getPath() . ' ' . $version . stubHTTPConnection::END_OF_LINE); 
    131         $socket->write('Host: ' . $this->http->getHost() . stubHTTPConnection::END_OF_LINE); 
     172        $socket->write($method . ' ' . $this->httpURL->getPath() . ' ' . $version . stubHTTPConnection::END_OF_LINE); 
     173        $socket->write('Host: ' . $this->httpURL->getHost() . stubHTTPConnection::END_OF_LINE); 
    132174         
    133175        // prepare last headers and write all headers to socket 
    134         $this->headers->putDate(); 
    135176        if ($this->headers->containsKey('User-Agent') === false) { 
    136177            $this->headers->putUserAgent('stubbles HTTP Client'); 
    137178        } 
    138179         
    139         $this->headers->putPower(); 
     180        $this->headers->enablePower(); 
     181        $this->headers->putDate(); 
    140182        foreach ($this->headers as $key => $value) { 
    141183            $socket->write($key . ': ' . $value . stubHTTPConnection::END_OF_LINE); 
    142184        } 
    143          
    144         switch ($method) { 
    145             // on GET: write one further end of line 
    146             case self::METHOD_GET: 
    147                 $socket->write(stubHTTPConnection::END_OF_LINE); 
    148                 break; 
    149              
    150             // on POST we have to write the body 
    151             case self::METHOD_POST: 
    152                 $socket->write(stubHTTPConnection::END_OF_LINE); 
    153                 $socket->write($this->body); 
    154                 break; 
    155              
    156             // on HEAD we write one additional header and close the connection 
    157             case self::METHOD_HEAD: 
    158                 $socket->write('Connection: close' . stubHTTPConnection::END_OF_LINE . stubHTTPConnection::END_OF_LINE); 
    159                 break; 
    160              
    161             default: 
    162                 // same as GET 
    163                 $socket->write(stubHTTPConnection::END_OF_LINE); 
    164         } 
    165          
    166         $response = new stubHTTPResponse($socket); 
    167         return $response; 
    168     } 
    169  
    170     /** 
    171      * creates the socket 
    172      * 
    173      * @return  stubSocket 
    174      */ 
    175     protected function createSocket() 
    176     { 
    177         return new stubSocket($this->http->getHost(), $this->http->getPort()); 
    178185    } 
    179186} 
  • trunk/src/main/php/net/stubbles/peer/http/stubHTTPResponse.php

    r1486 r1493  
    107107 
    108108    /** 
     109     * returns the used socket 
     110     * 
     111     * @return  stubSocket 
     112     */ 
     113    public function getSocket() 
     114    { 
     115        return $this->socket; 
     116    } 
     117 
     118    /** 
    109119     * read the response from socket and parse it 
    110120     */ 
  • trunk/src/test/php/net/stubbles/peer/PeerTestSuite.php

    r1492 r1493  
    3232        // http classes 
    3333        $suite->addTestFile($dir . '/http/stubHTTPConnectionTestCase.php'); 
     34        $suite->addTestFile($dir . '/http/stubHTTPRequestTestCase.php'); 
    3435        $suite->addTestFile($dir . '/http/stubHTTPURLTestCase.php'); 
    3536 
  • trunk/src/test/php/net/stubbles/peer/http/stubHTTPConnectionTestCase.php

    r1492 r1493  
    107107                            ); 
    108108        $mockHTTPRequest->expects($this->once()) 
    109                         ->method('send') 
    110                         ->with(stubHTTPRequest::METHOD_GET
     109                        ->method('get') 
     110                        ->with(stubHTTPRequest::VERSION_1_1
    111111                        ->will($this->returnValue($mockHTTPResponse)); 
    112         $this->assertSame($mockHTTPResponse, $this->httpConnection->get()); 
     112        $this->assertSame($mockHTTPResponse, $this->httpConnection->get(stubHTTPRequest::VERSION_1_1)); 
    113113    } 
    114114 
     
    134134                            ); 
    135135        $mockHTTPRequest->expects($this->once()) 
    136                         ->method('send') 
    137                         ->with(stubHTTPRequest::METHOD_HEAD
     136                        ->method('head') 
     137                        ->with(stubHTTPRequest::VERSION_1_1
    138138                        ->will($this->returnValue($mockHTTPResponse)); 
    139         $this->assertSame($mockHTTPResponse, $this->httpConnection->head()); 
     139        $this->assertSame($mockHTTPResponse, $this->httpConnection->head(stubHTTPRequest::VERSION_1_1)); 
    140140    } 
    141141 
    142142    /** 
    143      * initializing a head request should return a response object 
     143     * initializing a post request should return a response object 
    144144     * 
    145145     * @test 
     
    163163                            ); 
    164164        $mockHTTPRequest->expects($this->once()) 
    165                         ->method('send') 
    166                         ->with(stubHTTPRequest::METHOD_POST
     165                        ->method('post') 
     166                        ->with(stubHTTPRequest::VERSION_1_1
    167167                        ->will($this->returnValue($mockHTTPResponse)); 
    168         $this->assertSame($mockHTTPResponse, $this->httpConnection->post(array('foo' => 'bar'))); 
     168        $this->assertSame($mockHTTPResponse, $this->httpConnection->post(array('foo' => 'bar'), stubHTTPRequest::VERSION_1_1)); 
    169169    } 
    170170}