Example #1
0
 /**
  * Prepares the response based on the Request object
  * This method is not essential and can be cut out of the chain.
  * However it cleans up the headers and does some other stuff under the hood.
  * @param  Request $req The request object
  * 
  * @return \Modulework\Modules\Http\Response THIS
  */
 public function prepare(Request $request)
 {
     if ($this->isInvalid()) {
         throw new RunTimeException('Response is invalid (' . $this->statusCode . ')');
     }
     if ($this->isInformational()) {
         $this->content = null;
     }
     $this->charset = $this->charset ?: 'UTF-8';
     if (!$this->headers->has('Content-Type')) {
         $this->headers->set('Content-Type', 'text/html; charset=' . $this->charset);
     }
     // This method tries may cause some issues, if 200 is REQUIRED even when it' s
     // redirect response. If you want to change the status code just call it AFTER
     // this method:
     // e. g. [...]->prepare()->setStatusCode(301)[...]
     if ($this->statusCode === 200 && $this->headers->has('Location')) {
         $this->setStatusCode('302');
     }
     if ($request->isMethod('HEAD')) {
         $this->content = null;
     }
     if ('1.0' == $this->getProtocolVersion() && 'no-cache' == $this->headers->get('Cache-Control')) {
         $this->headers->set('pragma', 'no-cache');
         $this->headers->set('expires', -1);
     }
     return $this;
 }
Example #2
0
 public function testGetAcceptedEncoding()
 {
     $request = new Request();
     $request->init(array(), array(), array(), array(), array('HTTP_ACCEPT_ENCODING' => 'gzip,deflate,sdch'));
     $this->assertEquals(array('gzip', 'deflate', 'sdch'), $request->getAcceptedEncodings());
 }
Example #3
0
<?php

/*
 * (c) Christian Gärtner <*****@*****.**>
 * This file is part of the Modulework Framework Tests
 * License: View distributed LICENSE file
 *
 * 
 * This file is meant to be run in a browser and with a Webserver (php -S is just fine)
 */
use Modulework\Modules\Http\Request;
echo "General Testing<br />", PHP_EOL;
require '../../../../vendor/autoload.php';
$req = Request::makeFromGlobals();
echo $req;
echo "<hr>", PHP_EOL;
var_dump($_SERVER);
echo "<hr>", PHP_EOL;
$request = new Request();
$request->init(array('FOO'), array('FS'), array(), array('F'), array('F'));
var_dump($request);
Example #4
0
 public function testPrepare()
 {
     $req = new Request();
     $response = Response::make();
     $response->addHeader('Location', 'foo.bar');
     $response->prepare($req);
     $this->assertEquals(302, $response->getStatusCode());
     $req = new Request();
     $req->setMethod('HEAD');
     $response = Response::make();
     $response->prepare($req);
     $this->assertNull($response->getContent());
     $req = new Request();
     $req->setMethod('HEAD');
     $response = Response::make();
     $response->addHeader('Cache-Control', 'no-cache');
     $ret = $response->prepare($req);
     $this->assertEquals('no-cache', $response->headers->get('pragma'));
     $this->assertEquals(-1, $response->headers->get('expires'));
     $this->assertInstanceOf('Modulework\\Modules\\Http\\Response', $ret);
     $response = Response::make('', 100);
     $response->prepare(new Request());
     $this->assertEquals(null, $response->getContent());
     $this->expectOutputString('');
     // Just for extra saftey
     $response->send();
 }