English 中文(简体)
determine the http status that will be sent in php
原标题:

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.

问题回答

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.





相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签