/** * @covers Respect\Rest\Routes\AbstractRoute::match * @dataProvider extensions_provider */ public function testIgnoreFileExtensions($with, $without) { $_SERVER['HTTP_ACCEPT'] = '*'; $_SERVER['REQUEST_URI'] = '/'; $r = new Router(); $r->get('/route1/*', function ($match) { return $match; }); $r->get('/route2/*', function ($match) { return $match; })->accept(array('.json-home' => function ($data) { return factory::respond(E2M::mediaType('.json-home'), $data); }, "*" => function ($data) { return "{$data}.accepted"; })); $response = $r->dispatch('get', "/route1/{$with}")->response(); $this->assertEquals($with, $response); $response = $r->dispatch('get', "/route2/{$with}")->response(); $this->assertEquals("{$without}.accepted", $response); }
public function test_when_with_a_callable_class_within_a_route() { $router = new Router(); $routine = new WhenAlwaysTrue(); $router->get('/', function () { return 'route'; })->by($routine); // By does not affect the output of the route. $this->assertEquals($expected = 'route', (string) $router->dispatch('GET', '/')); $this->assertAttributeEquals($expected = true, 'invoked', $routine, 'Routine was not invoked!'); }
/** * @covers Respect\Rest\Routes\Exception::getReflection * @covers Respect\Rest\Routes\Exception::runTarget * @covers Respect\Rest\Router::exceptionRoute */ public function testMagicConstuctorCanCreateRoutesToExceptions() { $router = new Router(); $called = false; $phpUnit = $this; $router->exceptionRoute('RuntimeException', function ($e) use(&$called, $phpUnit) { $called = true; $phpUnit->assertEquals('Oops', $e->getMessage(), 'The exception message should be available in the exception route'); return 'There has been a runtime error.'; }); $router->get('/', function () { throw new \RuntimeException('Oops'); }); $response = (string) $router->dispatch('GET', '/')->response(); $this->assertTrue($called, 'The exception route must have been called'); $this->assertEquals('There has been a runtime error.', $response, 'An exception should be caught by the router and forwarded'); }
/** * @covers Respect\Rest\Routes\Error::getReflection * @covers Respect\Rest\Routes\Error::runTarget * @covers Respect\Rest\Router::errorRoute */ public function testMagicConstuctorCanCreateRoutesToErrors() { $router = new Router(); $called = false; $phpUnit = $this; $router->errorRoute(function ($err) use(&$called, $phpUnit) { $called = true; $phpUnit->assertContains('Oops', $err[0], 'The error message should be available in the error route'); return 'There has been an error.'; }); $router->get('/', function () { trigger_error('Oops', E_USER_WARNING); }); $response = (string) $router->dispatch('GET', '/')->response(); $this->assertTrue($called, 'The error route must have been called'); $this->assertEquals('There has been an error.', $response, 'An error should be caught by the router and forwarded'); }
/** * @covers Respect\Rest\Router::handleOptionsRequest * @runInSeparateProcess */ public function testOptionsRequestShouldBeDispatchedToCorrectOptionsHandler() { // arrange $router = new Router(); $router->get('/asian', 'GET: Asian Food!'); $router->options('/asian', 'OPTIONS: Asian Food!'); $router->post('/asian', 'POST: Asian Food!'); // act $response = (string) $router->dispatch('OPTIONS', '/asian')->response(); // assert $this->assertContains('Allow: GET, OPTIONS, POST', xdebug_get_headers(), 'There should be a sent Allow header with all methods for the handler that match this request.'); $this->assertEquals('OPTIONS: Asian Food!', $response, 'OPTIONS request should call the correct custom OPTIONS handler.'); }
/** * @covers Respect\Rest\Router::appendRoute */ public function testNamesRoutesUsingAttributes() { $router = new Router(); $router->allMembers = $router->any('/members', 'John, Carl'); $response = (string) $router->dispatch('GET', '/members')->response(); $this->assertTrue(isset($router->allMembers), 'There must be an attribute set for that key'); $this->assertEquals('John, Carl', $response, 'The route must be declared anyway'); }