/** * Test SlimException sets Response * * Pre-conditions: * You have initialized a Slim app with an accessible route * and raise a SlimException in that route. * * Post-conditions: * The response status will match the code and message of the SlimException */ public function testSlimRaiseSetsResponse() { Slim::init(); Slim::get('/', function () { Slim::raise(501, 'Error!'); }); Slim::run(); $this->assertEquals(Slim::response()->status(), 501); $this->assertEquals(Slim::response()->body(), 'Error!'); }
/** * 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 */ 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::raise(304); } } }