/** * Test Slim::halt outside route callback * * Pre-conditions: * Slim::halt is invoked outside of a route callback * * Post-conditions: * The new response should be returned with the expected * status code and body, regardless of the current route * callback's expected output. */ public function testSlimHaltOutsideCallback() { $this->setExpectedException('Slim_Exception_Stop'); Slim::init(); Slim::halt(500, 'External error'); Slim::get('/', function () { echo "foo"; }); Slim::run(); $this->assertEquals(Slim::response()->status(), 500); $this->assertEquals(Slim::response()->body(), 'External error'); }
require 'Slim/Slim.php'; //With custom settings $app = new Slim(); //Mailchimp help route $app->get('/mailchimp', function () use($app) { $app->render('mailchimp.php'); }); //Mailchimp webhook $app->post('/mailchimp', function () use($app) { require_once 'MCAPI.class.php'; $emailField = $app->request()->get('email'); $listId = $app->request()->get('listid'); $apiKey = $app->request()->get('apikey'); //Make sure we have required data. if (empty($emailField) || empty($listId) || empty($apiKey)) { $app->halt(500, 'Your hook is missing required GET parameters.'); } $email = $app->request()->post($emailField); $forename = $app->request()->post($app->request()->get('forename')); $surname = $app->request()->post($app->request()->get('surname')); $rid = $app->request()->post('id'); //Make sure we have required data. if (empty($email)) { $app->halt(500, 'Your hook is missing email address.'); } //If double opt in parameter is present, subscribe with double opt-in. $doubleOptIn = false; if (!is_null($app->request()->get('doubleoptin'))) { $doubleOptIn = true; } $api = new MCAPI($apiKey);
/** * Test halt does not leave output buffers open */ public function testHaltDoesNotLeaveOutputBuffersOpen() { $level_start = ob_get_level(); $s = new Slim(); $s->get('/bar', function () use($s) { $s->halt(500, ''); }); $s->run(); $this->assertEquals($level_start, ob_get_level()); }
/** * Test halt */ public function testHalt() { $s = new Slim(); $s->get('/bar', function () use($s) { echo "Foo!"; //<-- Should not be in response body! $s->halt(500, 'Something broke'); }); $s->call(); list($status, $header, $body) = $s->response()->finalize(); $this->assertEquals(500, $status); $this->assertEquals('Something broke', $body); }
/** * Test Slim Halt outside route callback * * Pre-conditions: * Slim app instantiated; * Slim::halt is invoked outside of a route callback; * * Post-conditions: * The new response should be returned with the expected * status code and body, regardless of the current route * callback's expected output. */ public function testSlimHaltOutsideCallback() { $this->setExpectedException('Slim_Exception_Stop'); $app = new Slim(); $app->halt(500, 'External error'); $app->get('/', function () { echo "foo"; }); $app->run(); $this->assertEquals(500, $app->response()->status()); $this->assertEquals('External error', $app->response()->body()); }
/** * Set ETag HTTP Response Header * * Set the etag header and stop if the conditional GET request matches. * The `value` argument is a unique identifier for the current resource. * The `type` argument indicates whether the etag should be used as a strong or * weak cache validator. * * When the current request includes an 'If-None-Match' header with * a matching etag, execution is immediately stopped. If the request * method is GET or HEAD, a '304 Not Modified' response is sent. * * @param string $value The etag value * @param string $type The type of etag to create; either "strong" or "weak" * @throws InvalidArgumentException If provided type is invalid * @return void */ public static function etag($value, $type = 'strong') { //Ensure type is correct if (!in_array($type, array('strong', 'weak'))) { throw new InvalidArgumentException('Invalid Slim::etag type. Expected "strong" or "weak".'); } //Set etag value $value = '"' . $value . '"'; if ($type === 'weak') { $value = 'W/' . $value; } Slim::response()->header('ETag', $value); //Check conditional GET if ($etagsHeader = Slim::request()->header('IF_NONE_MATCH')) { $etags = preg_split('@\\s*,\\s*@', $etagsHeader); if (in_array($value, $etags) || in_array('*', $etags)) { Slim::halt(304); } } }