37930

determine the http status that will be sent in php

Question:

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 <em>unit</em> level, so I'm trying to avoid it.

Answer1:

Either use a <a href="http://www.phpunit.de/manual/3.0/en/mock-objects.html" rel="nofollow">Mock</a> 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.

Recommend

  • Is it possible to detect unreachable code or other built in compile warnings using Roslyn
  • Question about POP3 message termination octet
  • TensorFlow Custom Allocator and Accessing Data from Tensor
  • android 2.2 WebView and WebViewClient RTSP problem
  • SecurityError for same-origin image texImage2D
  • How can I install and use latest Git on Mac OS X 10.9?
  • How to check Eclipse plugin performance
  • link library to all targets in cmake project
  • How to move axis labels automatically in rgl R
  • How to determine the CCSID used in CPYFRMIMPF command?
  • How to discover Font Type?
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • Looking for good analogy/examples for monitor verses semaphore
  • how to avoid repetitive constructor in children
  • What and where is mdimport
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • How to get Eclipse Oxygen to run on Java 9
  • How to match http request and response using Jersey ContainerRequestFilter and ContainerResponseFilt
  • Handling un-mapped Rest path
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • How to rebase a series of branches?
  • Control modification in presentation layer
  • Retrieving value from sql ExecuteScalar()
  • Get object from AWS S3 as a stream
  • Sending data from AppleScript to FileMaker records
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • Web-crawler for facebook in python
  • How do I rollback to a specific git commit
  • A cron job substitute?
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • How to get Windows thread pool to call class member function?
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?
  • playing video using jmf
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize