Created
January 15, 2013 14:48
-
-
Save gms8994/4539172 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## core.php | |
| /* Load Slim */ | |
| $app = new \Slim\Slim($app_config); | |
| \Slim\Slim::registerAutoloader(); | |
| /* Validate API key */ | |
| $app->hook('slim.before', function() use ($app){ | |
| $authenticated = $app->request()->headers('X-API-KEY') == API_KEY; | |
| if(! $authenticated) { | |
| throw new ApiException('Authentication failed'); | |
| } | |
| }); | |
| ## SchoolsTest.php | |
| public function testAPIKeyInvalid() { | |
| 21 $this->setExpectedException('\ApiException');## | |
| $this->get('/1', array('X_API_KEY' => 'XXXXXXXXXX')); | |
| } | |
| public function request($method, $path, $options=array()) { | |
| \Slim\Environment::mock(array_merge(array( | |
| 'REQUEST_METHOD' => $method, | |
| 'PATH_INFO' => $path, | |
| 'SERVER_NAME' => 'api.local.learninghouse.com', | |
| ), $options)); | |
| // Run the application | |
| require __DIR__ . '/../../public/index.php'; | |
| $this->app = $app; | |
| $this->request = $app->request(); | |
| $this->response = $app->response(); | |
| } | |
| public function get($path, $options=array()) { | |
| $this->request('GET', $path, $options); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment