Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added AcceptsString utility class for basic ACCEPT header string pars…
…ing.
  • Loading branch information
KrisJordan committed May 7, 2009
1 parent b032776 commit 81571f1
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 2 deletions.
85 changes: 85 additions & 0 deletions recess/recess/http/AcceptsString.class.php
@@ -0,0 +1,85 @@
<?php

class AcceptsString {

protected $range;

protected $types;

protected $qs = false;

protected $key = 0;

public function __construct($range) {
$this->range = $range;
}

public function next() {
if($this->qs === false) $this->init();

if(isset($this->qs[$this->key])) {
return $this->qs[$this->key++];
} else {
return false;
}
}

public function reset() {
$this->key = 0;
}

protected function init() {
// Break apart each type
$this->types = explode(',', $this->range);

$qs = array();

$count = count($this->types);

// Iterate through each to clean-up and extract precedence
for($t = 0; $t < $count; $t++) {
$this->types[$t] = trim($this->types[$t]);

$q = 1.0;

// Break apart type parameters
$params = explode(';', $this->types[$t]);

$paramsCount = count($params);
if(count($paramsCount > 1)) {
for($p = 1; $p < $paramsCount; $p++) {
$qPos = strpos($params[$p], 'q=');
if($qPos !== false) {
$qValue = trim(substr($params[$p], $qPos + 2));
if(is_numeric($qValue)) {
$q = $qValue;
}
}
}
}

if($params[0][strlen($params[0])-1] === '*') {
$q -= 0.01;

if($params[0][0] === '*') {
$q -= 0.01;
}
}

if(!isset($qs[$q])) {
$qs[(string)$q] = array();
}

$qs[(string)$q][] = $params[0];
}

// Sort keys of q-value in descending order
krsort($qs);

// Re-key qs 0,1,..N for simple iteration
$this->qs = array_combine(range(0,count($qs)-1), $qs);
}

}

?>
52 changes: 52 additions & 0 deletions recess/test/recess/http/AcceptsStringTest.php
@@ -0,0 +1,52 @@
<?php
Library::import('recess.http.AcceptsString');

class AcceptsStringTest extends PHPUnit_Framework_TestCase {
protected $mediaString = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
protected $media2String = '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, */*';
protected $languageString = 'en-us,en;q=0.5';
protected $charsetString = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7';
protected $specificityString = 'text/*,text/html,text/plain;q=0.9,text/*;q=0.9,*/*;q=0.9';

function testMediaString() {
$acceptsString = new AcceptsString($this->mediaString);
$this->assertEquals($acceptsString->next(), array('text/xml','application/xml','application/xhtml+xml','image/png'));
$this->assertEquals($acceptsString->next(), array('text/html'));
$this->assertEquals($acceptsString->next(), array('text/plain'));
$this->assertEquals($acceptsString->next(), array('*/*'));
$this->assertEquals($acceptsString->next(), false);
}

function testMedia2String() {
$acceptsString = new AcceptsString($this->media2String);
$this->assertEquals($acceptsString->next(), array('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'));
$this->assertEquals($acceptsString->next(), array('*/*'));
$this->assertEquals($acceptsString->next(), false);
}

function testLanguageString() {
$acceptsString = new AcceptsString($this->languageString);
$this->assertEquals($acceptsString->next(), array('en-us'));
$this->assertEquals($acceptsString->next(), array('en'));
$this->assertEquals($acceptsString->next(), false);
}

function testCharsetString() {
$acceptsString = new AcceptsString($this->charsetString);
$this->assertEquals($acceptsString->next(), array('ISO-8859-1'));
$this->assertEquals($acceptsString->next(), array('utf-8'));
$this->assertEquals($acceptsString->next(), array('*'));
$this->assertEquals($acceptsString->next(), false);
}

function testSpecificity() {
$acceptsString = new AcceptsString($this->specificityString);
$this->assertEquals($acceptsString->next(), array('text/html'));
$this->assertEquals($acceptsString->next(), array('text/*'));
$this->assertEquals($acceptsString->next(), array('text/plain'));
$this->assertEquals($acceptsString->next(), array('text/*'));
$this->assertEquals($acceptsString->next(), array('*/*'));
$this->assertEquals($acceptsString->next(), false);
}
}
?>
6 changes: 4 additions & 2 deletions recess/test/recess/http/RecessHttpTests.php
@@ -1,14 +1,16 @@
<?php
require_once 'PHPUnit/Framework.php';
require_once 'recess/http/ContentNegotiationTest.php';
require_once 'recess/http/AcceptsTest.php';
require_once 'recess/http/AcceptsStringTest.php';

class RecessHttpAllTests
{
public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('recess.http');

$suite->addTestSuite('ContentNegotiationTest');
$suite->addTestSuite('AcceptsTest');
$suite->addTestSuite('AcceptsStringTest');

return $suite;
}
Expand Down

0 comments on commit 81571f1

Please sign in to comment.