Skip to content

Commit

Permalink
Introduced new behavior for override: it will completely ignore the a…
Browse files Browse the repository at this point in the history
…ccept header if format has been overridden.
  • Loading branch information
KrisJordan committed Jun 9, 2009
1 parent 2e8a05d commit 50cf9d8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
28 changes: 19 additions & 9 deletions recess/recess/http/Accepts.class.php
Expand Up @@ -7,6 +7,7 @@ class Accepts {
protected $headers;

protected $types = false;
protected $typeOverride = null;
protected $typesTried = array();
protected $typesCurrent = array();

Expand All @@ -19,15 +20,28 @@ class Accepts {
const ENCODINGS = 'ACCEPT_ENCODING';
const CHARSETS = 'ACCEPT_CHARSETS';

public function __construct($headers, $overrides = array()) {
public function __construct($headers) {
$this->headers = $headers;
foreach($overrides as $key => $value) {
$this->headers[$key] = $value;
}
}

protected function initFormats() {
$this->types = new AcceptsList($this->headers[self::TYPES]);
}

public function overrideFormat($format) {
$this->typeOverride = $format;
$this->headers[self::TYPES] = array();
}

public function nextFormat() {
if($this->types === false) { $this->initFormats(); }
if($this->types === false) {
if($this->typeOverride !== null) {
$format = $this->typeOverride;
$this->typeOverride = null;
return $format;
}
$this->initFormats();
}

while(current($this->typesCurrent) === false) {
$key = key($this->typesCurrent);
Expand All @@ -50,10 +64,6 @@ public function nextFormat() {
return $result[1]; // Each returns an array of (key, value)
}

protected function initFormats() {
$this->types = new AcceptsList($this->headers[self::TYPES]);
}

public function resetFormats() {
if($this->types !== false)
$this->types->reset();
Expand Down
3 changes: 1 addition & 2 deletions recess/recess/http/Environment.class.php
@@ -1,6 +1,5 @@
<?php
Library::import('recess.http.Request');
Library::import('recess.http.Formats');
Library::import('recess.http.Methods');

/**
Expand All @@ -19,7 +18,7 @@ public static function getRawRequest() {

$request->method = $_SERVER['REQUEST_METHOD'];

$request->format = Formats::XHTML;
$request->format = 'html';

$request->setResource(self::stripQueryString($_SERVER['REQUEST_URI']));

Expand Down
2 changes: 1 addition & 1 deletion recess/recess/http/Request.class.php
Expand Up @@ -45,7 +45,7 @@ public function setResource($resource) {

public static function splitResourceString($resourceString) {
$parts = array_filter(split(Library::pathSeparator, $resourceString), array('Request','resourceFilter'));
if(!empty($parts)) {
if(!empty($parts)) {
return array_combine(range(0, count($parts)-1), $parts);
} else {
return $parts;
Expand Down
17 changes: 16 additions & 1 deletion recess/test/recess/http/AcceptsTest.php
Expand Up @@ -2,7 +2,7 @@
Library::import('recess.http.Accepts');

class AcceptsTest extends PHPUnit_Framework_TestCase {
protected $acceptChrome, $acceptFirefox, $acceptIE;
protected $acceptChrome, $acceptFirefox, $acceptIE, $acceptOverride;

function setup() {
$this->acceptChrome
Expand All @@ -28,6 +28,15 @@ function setup() {
'ACCEPT_ENCODING' => 'gzip, deflate',
)
);

$this->acceptOverride
= new Accepts(
array( 'ACCEPT' => 'image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-shockwave-flash, application/x-silverlight-2-b2, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*',
'ACCEPT_LANGUAGE' => 'en-us',
'ACCEPT_ENCODING' => 'gzip, deflate',
)
);
$this->acceptOverride->overrideFormat('xml');
}

function testChromeContentTypes() {
Expand Down Expand Up @@ -90,6 +99,12 @@ function testIEContentTypes() {
$this->assertEquals('form', $this->acceptIE->nextFormat());
$this->assertEquals('url-form', $this->acceptIE->nextFormat());
$this->assertEquals('csv', $this->acceptIE->nextFormat());
$this->assertEquals(false, $this->acceptIE->nextFormat());
}

function testOverrideContentTypes() {
$this->assertEquals('xml', $this->acceptOverride->nextFormat());
$this->assertEquals(false, $this->acceptOverride->nextFormat());
}
}
?>

0 comments on commit 50cf9d8

Please sign in to comment.