Changeset 1471

Show
Ignore:
Timestamp:
03/29/08 21:35:39 (5 months ago)
Author:
mikey
Message:

added unit test for net::stubbles::ipo.response::stubBaseResponse
added possibility to set the status code for the response
added possibility to set the http version

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/ipo/response/stubBaseResponse.php

    r1223 r1471  
    2222{ 
    2323    /** 
     24     * current php sapi 
     25     * 
     26     * @var  string 
     27     */ 
     28    protected $sapi; 
     29    /** 
     30     * http version to be used 
     31     * 
     32     * @var  string 
     33     */ 
     34    protected $version; 
     35    /** 
     36     * the status code to be send 
     37     * 
     38     * @var  int 
     39     */ 
     40    protected $statusCode = null; 
     41    /** 
    2442     * list of headers for this response 
    2543     * 
    2644     * @var  array<string,string> 
    2745     */ 
    28     protected $headers = array(); 
     46    protected $headers    = array(); 
    2947    /** 
    3048     * list of cookies for this response 
     
    3250     * @var  array<string,stubCookie> 
    3351     */ 
    34     protected $cookies = array(); 
     52    protected $cookies    = array(); 
    3553    /** 
    3654     * data to send as body of response 
     
    3856     * @var  string 
    3957     */ 
    40     protected $data    = null; 
     58    protected $data       = null; 
     59 
     60    /** 
     61     * constructor 
     62     * 
     63     * @param  string  $sapi     optional  current php sapi 
     64     * @param  string  $version  optional  http version 
     65     */ 
     66    public function __construct($sapi = PHP_SAPI, $version = '1.1') 
     67    { 
     68        $this->sapi    = $sapi; 
     69        $this->version = $version; 
     70    } 
    4171 
    4272    /** 
     
    4575    public function clear() 
    4676    { 
    47         $this->header = array(); 
    48         $this->cookie = array(); 
    49         $this->data   = null; 
     77        $this->statusCode = null; 
     78        $this->headers    = array(); 
     79        $this->cookies    = array(); 
     80        $this->data       = null; 
     81    } 
     82 
     83    /** 
     84     * sets the http version 
     85     * 
     86     * The version should be a string like '1.0' or '1.1'. 
     87     * 
     88     * @param  string  $version 
     89     */ 
     90    public function setVersion($version) 
     91    { 
     92        $this->version = $version; 
     93    } 
     94 
     95    /** 
     96     * returns the http version 
     97     * 
     98     * @return  string 
     99     */ 
     100    public function getVersion() 
     101    { 
     102        return $this->version; 
     103    } 
     104 
     105    /** 
     106     * sets the status code to be send 
     107     * 
     108     * This needs only to be done if another status code then the default one 
     109     * should be send. 
     110     * 
     111     * @param  int  $statusCode 
     112     */ 
     113    public function setStatusCode($statusCode) 
     114    { 
     115        $this->statusCode = $statusCode; 
     116    } 
     117 
     118    /** 
     119     * returns status code to be send 
     120     * 
     121     * If return value is <null> the default one will be send. 
     122     * 
     123     * @return  int 
     124     */ 
     125    public function getStatusCode() 
     126    { 
     127        return $this->statusCode; 
    50128    } 
    51129 
     
    84162     * returns the list of cookies 
    85163     * 
    86      * @return  array<stubCookie> 
     164     * @return  array<string,stubCookie> 
    87165     */ 
    88166    public function getCookies() 
     
    126204    public function send() 
    127205    { 
     206        if (null !== $this->statusCode) { 
     207            if ('cgi' === $this->sapi) { 
     208                $this->header('Status: ' . $this->statusCode); 
     209            } else { 
     210                $this->header('HTTP/' . $this->version . ' ' . $this->statusCode); 
     211            } 
     212        } 
     213 
    128214        // send the headers 
    129215        foreach ($this->headers as $name => $value) { 
    130             header($name . ': ' . $value); 
     216            $this->header($name . ': ' . $value); 
    131217        } 
    132218 
    133219        // send the cookies 
    134220        foreach ($this->cookies as $cookie) { 
    135             setcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiration(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly()); 
     221            $cookie->send(); 
    136222        } 
    137223 
    138224        // and finally send the data 
    139         echo $this->data; 
     225        $this->sendData($this->data); 
     226    } 
     227 
     228    /** 
     229     * helper method to send the header 
     230     * 
     231     * @param  string  $header 
     232     */ 
     233    // @codeCoverageIgnoreStart 
     234    protected function header($header) 
     235    { 
     236        header($header); 
     237    } 
     238    // @codeCoverageIgnoreEnd 
     239 
     240    /** 
     241     * helper method to send the data 
     242     * 
     243     * @param  string  $data 
     244     */ 
     245    // @codeCoverageIgnoreStart 
     246    protected function sendData($data) 
     247    { 
     248        echo $data; 
    140249        flush(); 
    141250    } 
     251    // @codeCoverageIgnoreEnd 
    142252} 
    143253?> 
  • trunk/src/main/php/net/stubbles/ipo/response/stubCookie.php

    r713 r1471  
    7070     * @param  string  $value  value of the cookie 
    7171     */ 
    72     protected function __construct($name, $value) 
     72    public function __construct($name, $value) 
    7373    { 
    7474        $this->name  = $name; 
     
    218218        return $this->httpOnly; 
    219219    } 
     220 
     221    /** 
     222     * sends the cookie 
     223     */ 
     224    // @codeCoverageIgnoreStart 
     225    public function send() 
     226    { 
     227        setcookie($this->name, $this->value, $this->expires, $this->path, $this->domain, $this->secure, $this->httpOnly); 
     228    } 
     229    // @codeCoverageIgnoreEnd 
    220230} 
    221231?> 
  • trunk/src/main/php/net/stubbles/ipo/response/stubResponse.php

    r1223 r1471  
    2323     */ 
    2424    public function clear(); 
    25      
     25 
     26    /** 
     27     * sets the http version 
     28     * 
     29     * The version should be a string like '1.0' or '1.1'. 
     30     * 
     31     * @param  string  $version 
     32     */ 
     33    public function setVersion($version); 
     34 
     35    /** 
     36     * returns the http version 
     37     * 
     38     * @return  string 
     39     */ 
     40    public function getVersion(); 
     41 
     42    /** 
     43     * sets the status code to be send 
     44     * 
     45     * This needs only to be done if another status code then the default one 
     46     * should be send. 
     47     * 
     48     * @param  int  $statusCode 
     49     */ 
     50    public function setStatusCode($statusCode); 
     51 
     52    /** 
     53     * returns status code to be send 
     54     * 
     55     * If return value is <null> the default one will be send. 
     56     * 
     57     * @return  int 
     58     */ 
     59    public function getStatusCode(); 
     60 
    2661    /** 
    2762     * add a header to the response 
     
    3166     */ 
    3267    public function addHeader($name, $value); 
    33      
     68 
    3469    /** 
    3570     * returns the list of headers 
     
    3873     */ 
    3974    public function getHeaders(); 
    40      
     75 
    4176    /** 
    4277     * add a cookie to the response 
     
    4580     */ 
    4681    public function setCookie(stubCookie $cookie); 
    47      
     82 
    4883    /** 
    4984     * returns the list of cookies 
    5085     * 
    51      * @return  array<stubCookie> 
     86     * @return  array<string,stubCookie> 
    5287     */ 
    5388    public function getCookies(); 
    54      
     89 
    5590    /** 
    5691     * write data into the response 
     
    5994     */ 
    6095    public function write($data); 
    61      
     96 
    6297    /** 
    6398     * returns the data written so far 
     
    66101     */ 
    67102    public function getData(); 
    68      
     103 
    69104    /** 
    70105     * replaces the data written so far with the new data 
     
    73108     */ 
    74109    public function replaceData($data); 
    75      
     110 
    76111    /** 
    77112     * send the response out 
  • trunk/src/test/php/net/stubbles/ipo/IPOTestSuite.php

    r1434 r1471  
    6565        $suite->addTestFile($dir . '/request/filters/provider/stubSimpleFilterProviderTestCase.php'); 
    6666 
     67        $suite->addTestFile($dir . '/response/stubBaseResponseTestCase.php'); 
    6768        $suite->addTestFile($dir . '/response/stubCookieTestCase.php'); 
    6869