views:

102

answers:

2

I am trying to write a test case for a class that is managing headers for my application. Among the headers it sends are http status headers. I am using headers_list() to see which headers would be send, were I to send headers now. The problem with headers_list() is that it does not include the http status header (although this seems to be undocumented on php.net). So, I cannot find a way to determine what http status would be sent. Even if I do send the headers (which I'm trying not to do, so I can test a bunch of different things all in one go), the status does not show up in headers_list(). Any ideas?

P.S. I realize I could do this by requesting the page and examining the response, but that makes it very difficult to keep tests at a unit level, so I'm trying to avoid it.

A: 

This is the code you're looking for:

header("HTTP/1.0 404 Not Found");

As for determining it, you'll need to use a 3rd party script. Google "header status analyser" or something like that, there's tons of tools for it.

Citizen
Why the downvote? If his current method isn't working, my answer is the next best thing.Alternate answer: you can do it in php, just use http://php.net/manual/en/function.get-headers.php
Citizen
I'm not trying to send it, I'm trying to determing what would be sent. The class I'm testing is making that call to header, I'm trying to find out what status it called it with.
magicrobotmonkey
get_headers() sounds like your best bet then, unless I'm still missing something. You might want to be more descriptive of what you're trying to do and include some of your attempted code.
Citizen
nope, get_headers() gives you the headers that were sent to you. I'm trying to get the staus header my script sends. I'm not sure how I can be more clear.
magicrobotmonkey
I'm saying you can test it recursively or via external.But hey, thanks for the downvote, welcome to StackOverflow.
Citizen
The OP was pretty clear - you only told him how to perform a subset of the situation he's currently in. Your answer was neither relevant nor helpful, and exactly what I've been getting back from Google to boot.
michaelc
A: 

Either use a Mock that returns the header but doesnt send it or write a Response object, e.g.

class HttpResponse 
{
    protected $_status = '200 OK';
    protected $_headers = array();
    protected $_body = null;

    public function setStatus($status)
    {
        $this->_status = $status;
    }

    public function getStatus()
    {
        return $this->_status;
    }

    public function addHeader($name, $value)
    {
        $this->_headers[$name] = $value;
        return $this;
    }

    public function getHeaders()
    {
        return $this->_headers;
    }    

    public function write($data)
    {
        $this->_body .= $data;
        return $this;
    }

    public function send()
    {
        header('HTTP/1.0 ' . $this->_status);
        foreach ($this->headers as $name => $value) {
            header("$name : $value");
        }
        echo $this->_body;
        $this->resetRequest();
    }

   public function resetRequest()
   {
        $this->_status  = '200 OK';
        $this->_headers = array();
        $this->_body    = null;
   }
}

So as long as you dont send() you can inspect the state through the getters. You could also add a __toString() method, that returns the Entire Response as a string and regex it to see if it looks like you expect it to look.

Gordon