Skip to content

Instantly share code, notes, and snippets.

@gms8994
Created January 15, 2013 14:48
Show Gist options
  • Save gms8994/4539172 to your computer and use it in GitHub Desktop.
Save gms8994/4539172 to your computer and use it in GitHub Desktop.
## 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