Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial set of tests for headers and routes

  • Loading branch information...
commit c7f3968fe728307eeb082fe0ccdc957a7015a2ec 1 parent 4d32165
@abackstrom abackstrom authored
View
4 Makefile
@@ -0,0 +1,4 @@
+all:
+
+test:
+ phpunit tests
View
12 tests/HeadersTest.php
@@ -0,0 +1,12 @@
+<?php
+
+require_once dirname(__FILE__) . '/setup.php';
+
+class HeadersTest extends PHPUnit_Framework_TestCase {
+ public function testHeaderKey() {
+ $this->expectOutputString("Foo: Bar\n");
+
+ $headers = new HeadersEcho;
+ $headers->header( 'Foo', 'Bar' );
+ }
+}
View
79 tests/RoutesTest.php
@@ -0,0 +1,79 @@
+<?php
+
+require_once dirname(__FILE__) . '/setup.php';
+
+class RoutesTest extends PHPUnit_Framework_TestCase {
+ protected function setUp() {
+ global $__routes;
+ $__routes = array();
+
+ global $__namespace;
+ $__namespace = null;
+ }
+
+ public function testBasic() {
+ respond( '/', function(){ echo 'x'; });
+ respond( '/something', function(){ echo 'y'; });
+
+ $out = dispatch( '/', 'GET', null, true );
+ $this->assertSame( 'x', $out );
+ }
+
+ public function testCatchall() {
+ respond( function(){ echo 'a'; });
+ respond( '/not', function(){ echo 'b'; });
+ respond( '*', function(){ echo 'c'; });
+ respond( '/something', function(){ echo 'd'; });
+ respond( '*', function(){ echo 'e'; });
+ respond( '/not', function(){ echo 'f'; });
+ respond( function(){ echo 'g'; });
+
+ $out = dispatch( '/something', 'GET', null, true );
+ $this->assertSame( 'acdeg', $out );
+ }
+
+ public function testRegex() {
+ respond( '@/bar', function(){ echo 'z'; });
+
+ $out = dispatch( '/bar', 'GET', null, true );
+ $this->assertSame( 'z', $out );
+ }
+
+ public function testRegexNegate() {
+ respond( '!@/foo', function(){ echo 'y'; });
+
+ $out = dispatch( '/bar', 'GET', null, true );
+ $this->assertSame( 'y', $out );
+ }
+
+ public function test404() {
+ $this->expectOutputString("HTTP/1.1 404\n");
+
+ $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
+ respond( '/', function(){ echo 'a'; } );
+
+ dispatch( '/foo' );
+ }
+
+ public function testParamsBasic() {
+ respond( '/[:color]', function($request){ echo $request->param('color'); });
+
+ $out = dispatch( '/blue', 'GET', null, true );
+ $this->assertSame( 'blue', $out );
+ }
+
+ public function testParamsIntegerSuccess() {
+ respond( '/[i:age]', function($request){ var_dump( $request->param('age') ); });
+
+ $out = dispatch( '/987', 'GET', null, true );
+ $this->assertSame( 'string(3) "987"', trim($out) );
+ }
+
+ public function testParamsIntegerFail() {
+ respond( '/[i:age]', function($request){ var_dump( $request->param('age') ); });
+ respond( '404', function(){ echo '404'; } );
+
+ $out = dispatch( '/blue', 'GET', null, true );
+ $this->assertSame( '404', trim($out) );
+ }
+}
View
11 tests/setup.php
@@ -0,0 +1,11 @@
+<?php
+
+include dirname(dirname(__FILE__)) . '/klein.php';
+
+class HeadersEcho extends _Headers {
+ public function header($key, $value = '') {
+ echo $this->_header($key, $value) . "\n";
+ }
+}
+
+_Request::$_headers = _Response::$_headers = new HeadersEcho;
Please sign in to comment.
Something went wrong with that request. Please try again.