summaryrefslogtreecommitdiff
path: root/_test
diff options
context:
space:
mode:
authorChristopher Smith <chris@jalakai.co.uk>2013-03-22 18:01:45 +0000
committerChristopher Smith <chris@jalakai.co.uk>2013-03-22 18:01:45 +0000
commitfb0b3fbf03223c8c304608cdb32ee5c9d755eca1 (patch)
tree4a417efffa52bcb536dbc9846c9623c3e560ed54 /_test
parent9894e7afaae16cc0699afbe839681e023afee65a (diff)
downloadrpg-fb0b3fbf03223c8c304608cdb32ee5c9d755eca1.tar.gz
rpg-fb0b3fbf03223c8c304608cdb32ee5c9d755eca1.tar.bz2
update TestResponse class to return specific headers & status codes (with tests)
Diffstat (limited to '_test')
-rw-r--r--_test/core/TestResponse.php38
-rw-r--r--_test/tests/test/basic.test.php41
2 files changed, 79 insertions, 0 deletions
diff --git a/_test/core/TestResponse.php b/_test/core/TestResponse.php
index 6d20afb28..7cc50ee4f 100644
--- a/_test/core/TestResponse.php
+++ b/_test/core/TestResponse.php
@@ -42,6 +42,44 @@ class TestResponse {
}
/**
+ * @param $name string, the name of the header without the ':', e.g. 'Content-Type', 'Pragma'
+ * @return mixed if exactly one header, the header (string); otherwise an array of headers, empty when no headers
+ */
+ public function getHeader($name) {
+ $result = array();
+ foreach ($this->headers as $header) {
+ if (substr($header,0,strlen($name)+1) == $name.':') {
+ $result[] = $header;
+ }
+ }
+
+ return count($result) == 1 ? $result[0] : $result;
+ }
+
+ /**
+ * @return int http status code
+ *
+ * in the test environment, only status codes explicitly set by dokuwiki are likely to be returned
+ * this means succcessful status codes (e.g. 200 OK) will not be present, but error codes will be
+ */
+ public function getStatusCode() {
+ $headers = $this->getHeader('Status');
+ $code = null;
+
+ if ($headers) {
+ // if there is more than one status header, use the last one
+ $status = is_array($headers) ? array_pop($headers) : $headers;
+ $matches = array();
+ preg_match('/^Status: ?(\d+)/',$status,$matches);
+ if ($matches){
+ $code = $matches[1];
+ }
+ }
+
+ return $code;
+ }
+
+ /**
* Query the response for a JQuery compatible CSS selector
*
* @link https://code.google.com/p/phpquery/wiki/Selectors
diff --git a/_test/tests/test/basic.test.php b/_test/tests/test/basic.test.php
index c40f2d574..1c9d6d516 100644
--- a/_test/tests/test/basic.test.php
+++ b/_test/tests/test/basic.test.php
@@ -4,6 +4,24 @@
* @group integration
*/
class InttestsBasicTest extends DokuWikiTest {
+
+ private $some_headers = array(
+ 'Content-Type: image/png',
+ 'Date: Fri, 22 Mar 2013 16:10:01 GMT',
+ 'X-Powered-By: PHP/5.3.15',
+ 'Expires: Sat, 23 Mar 2013 17:03:46 GMT',
+ 'Cache-Control: public, proxy-revalidate, no-transform, max-age=86400',
+ 'Pragma: public',
+ 'Last-Modified: Fri, 22 Mar 2013 01:48:28 GMT',
+ 'ETag: "63daab733b38c30c337229b2e587f8fb"',
+ 'Content-Disposition: inline; filename="fe389b0db8c1088c336abb502d2f9ae7.media.200x200.png',
+ 'Accept-Ranges: bytes',
+ 'Content-Type: image/png',
+ 'Content-Length: 62315',
+ 'Status: 200 OK',
+ 'Status: 404 Not Found',
+ );
+
/**
* Execute the simplest possible request and expect
* a dokuwiki page which obviously has the word "DokuWiki"
@@ -120,4 +138,27 @@ class InttestsBasicTest extends DokuWikiTest {
$this->assertEquals('lib/exe/detail.php',$request->getScript());
}
+ function testHeaders(){
+ $request = new TestRequest();
+ $response = $request->get(array(),'/lib/exe/fetch.php?media=wiki:dokuwiki-128.png');
+ $headers = $response->getHeaders();
+ $this->assertTrue(!empty($headers));
+ }
+
+ function testGetHeader(){
+ $response = new TestResponse('',$this->some_headers);
+
+ $this->assertEquals('Pragma: public', $response->getHeader('Pragma'));
+ $this->assertEmpty($response->getHeader('Junk'));
+ $this->assertEquals(array('Content-Type: image/png','Content-Type: image/png'), $response->getHeader('Content-Type'));
+ }
+
+ function testGetStatus(){
+ $response = new TestResponse('',$this->some_headers);
+ $this->assertEquals(404, $response->getStatusCode());
+
+ $response = new TestResponse('',array_slice($this->some_headers,0,-2)); // slide off the last two headers to leave no status header
+ $this->assertNull($response->getStatusCode());
+ }
+
}