function test_rest_api() { $c = new Controller(); $c->page(new Page()); $test_api = new RestTestApi(); // GET bap/bop/boop should match GET bap/bop/boop ob_start(); $_SERVER['REQUEST_METHOD'] = 'GET'; $c->params = array('bap', 'bop', 'boop'); $c->restful($test_api); $res = ob_get_clean(); ob_start(); $test_api->wrap('it works'); $exp = ob_get_clean(); $this->assertEquals($exp, $res); // POST foo/123 should match ALL foo/%d ob_start(); $_SERVER['REQUEST_METHOD'] = 'POST'; $c->params = array('foo', '123'); $c->restful($test_api); $res = ob_get_clean(); ob_start(); $test_api->wrap('123'); $exp = ob_get_clean(); $this->assertEquals($exp, $res); // GET bar/hello should match GET bar/%s ob_start(); $_SERVER['REQUEST_METHOD'] = 'GET'; $c->params = array('bar', 'hello'); $c->restful($test_api); $res = ob_get_clean(); ob_start(); $test_api->wrap('hello'); $exp = ob_get_clean(); $this->assertEquals($exp, $res); // POST bar/hello should fail to match GET bar/%s ob_start(); $_SERVER['REQUEST_METHOD'] = 'POST'; $c->params = array('bar', 'hello'); $c->restful($test_api); $res = ob_get_clean(); ob_start(); $test_api->error('Invalid action name'); $exp = ob_get_clean(); $this->assertEquals($exp, $res); // GET one/and/two/and should match GET one/%s/two/%s ob_start(); $_SERVER['REQUEST_METHOD'] = 'GET'; $c->params = array('one', 'and', 'two', 'and'); $c->restful($test_api); $res = ob_get_clean(); ob_start(); $test_api->wrap(array('and', 'and')); $exp = ob_get_clean(); $this->assertEquals($exp, $res); // GET standard should match get_standard() ob_start(); $_SERVER['REQUEST_METHOD'] = 'GET'; $c->params = array('standard'); $c->restful($test_api); $res = ob_get_clean(); ob_start(); $test_api->wrap('it works'); $exp = ob_get_clean(); $this->assertEquals($exp, $res); // POST standard should fail to match get_standard() ob_start(); $_SERVER['REQUEST_METHOD'] = 'POST'; $c->params = array('standard'); $c->restful($test_api); $res = ob_get_clean(); ob_start(); $test_api->error('Invalid action name'); $exp = ob_get_clean(); $this->assertEquals($exp, $res); // GET asdf/foo should fail to match all ob_start(); $_SERVER['REQUEST_METHOD'] = 'GET'; $c->params = array('asdf', 'foo'); $c->restful($test_api); $res = ob_get_clean(); ob_start(); $test_api->error('Invalid action name'); $err = ob_get_clean(); $this->assertEquals($err, $res); }