/**
  * Redirect
  *
  * This method immediately redirects to a new URL. By default,
  * this issues a 302 Found response; this is considered the default
  * generic redirect response. You may also specify another valid
  * 3xx status code if you want. This method will automatically set the
  * HTTP Location header for you using the URL parameter and place the
  * destination URL into the response body.
  *
  * @param   string                      $url        The destination URL
  * @param   int                         $status     The HTTP redirect status code (Optional)
  * @throws  InvalidArgumentException                If status parameter is not a valid 3xx status code
  * @return  void
  */
 public function redirect($url, $status = 302)
 {
     if ($status >= 300 && $status <= 307) {
         $this->response->header('Location', (string) $url);
         $this->halt($status, (string) $url);
     } else {
         throw new InvalidArgumentException('Slim::redirect only accepts HTTP 300-307 status codes.');
     }
 }
Ejemplo n.º 2
0
 /**
  * Test finalize
  *
  * Pre-conditions:
  * Case A: Response status is 200
  * Case B: Response status is 204
  * Case C: Response status is 304
  *
  * Post-conditions:
  * Case A: Response has body and content-length
  * Case B: Response does not have body and content-length
  * Case C: Response does not have body and content-length
  */
 public function testFinalize()
 {
     //Case A
     $r1 = new Slim_Http_Response(new Slim_Http_Request());
     $r1->body('body1');
     $r1->finalize();
     $this->assertEquals('body1', $r1->body());
     $this->assertEquals(5, $r1->header('Content-Length'));
     //Case B
     $r2 = new Slim_Http_Response(new Slim_Http_Request());
     $r2->body('body2');
     $r2->status(204);
     $r2->finalize();
     $this->assertEquals('', $r2->body());
     $this->assertNull($r2->header('Content-Type'));
     //Case C
     $r3 = new Slim_Http_Response(new Slim_Http_Request());
     $r3->body('body3');
     $r3->status(304);
     $r3->finalize();
     $this->assertEquals('', $r3->body());
     $this->assertNull($r3->header('Content-Type'));
 }
Ejemplo n.º 3
0
 /**
  * Test get and set header (without Array Access)
  */
 public function testGetAndSetHeader()
 {
     $r = new Slim_Http_Response();
     $r->header('X-Foo', 'Bar');
     $this->assertEquals('Bar', $r->header('X-Foo'));
 }
Ejemplo n.º 4
0
 /**
  * Test response body if HEAD request
  *
  * Pre-conditions:
  * HTTP method is HEAD
  *
  * Post-conditions:
  * Response body is NOT set;
  * Response headers are set;
  */
 function testResponseBodyIfHeadRequest() {
     $this->expectOutputString('');
     $_SERVER['REQUEST_METHOD'] = 'HEAD';
     $req = new Slim_Http_Request();
     $res = new Slim_Http_Response($req);
     $res->body('This is a test body');
     $res->send();
     $this->assertEquals('text/html', $res->header('Content-Type'));
     $this->assertEquals(19, $res->header('Content-Length'));
 }