Skip to content

Commit

Permalink
Merge pull request #7 from teresko/master
Browse files Browse the repository at this point in the history
Adding functionality for reading Content-Type header for custom content parser in RequestBuilder
  • Loading branch information
teresko committed Feb 22, 2016
2 parents b5ed081 + 4ebe117 commit 526d1c0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/Fracture/Http/Headers/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,14 @@ public function isFinal()
{
return false;
}


public function getParameter($name)
{
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}

return null;
}
}
9 changes: 4 additions & 5 deletions src/Fracture/Http/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,24 @@ protected function applyContentParsers($instance)

foreach ($this->parsers as $value => $parser) {
if ($header->contains($value)) {
$parameters = $this->alterParameters($parameters, $parser, $value);
$parameters += $this->alterParameters($parser, $value, $header);
}
}

$instance->setParameters($parameters, true);
}


private function alterParameters($parameters, $parser, $value)
private function alterParameters($parser, $value, $header)
{
$result = call_user_func($parser);
$result = call_user_func($parser, $header);

if (false === is_array($result)) {
$message = "Parser for '$value' did not return a 'name => value' array of parameters";
trigger_error($message, \E_USER_WARNING);
}

$parameters += $result;
return $parameters;
return $result;
}


Expand Down
18 changes: 18 additions & 0 deletions tests/unit/Fracture/Http/Headers/CommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,22 @@ public function testParsedDataForPreparedInstance()
$instance->prepare();
$this->assertSame('beta', $instance->getParsedData());
}


/**
* @covers Fracture\Http\Headers\Common::__construct
* @covers Fracture\Http\Headers\Common::prepare
* @covers Fracture\Http\Headers\Common::getParsedData
* @covers Fracture\Http\Headers\Common::getParameter
*/
public function testValueRetrieval()
{
$instance = $this->getMockForAbstractClass('Fracture\Http\Headers\Common', ['type/subtype; name=4']);
$instance->expects($this->any())
->method('extractData')
->will($this->returnValue(['name' => 4]));

$instance->prepare();
$this->assertSame(4, $instance->getParameter('name'));
}
}
25 changes: 25 additions & 0 deletions tests/unit/Fracture/Http/RequestBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,31 @@ public function testAppliedContentParsersWithMissingHeader()

}


/**
* @covers Fracture\Http\RequestBuilder::create
* @covers Fracture\Http\RequestBuilder::applyContentParsers
* @covers Fracture\Http\RequestBuilder::addContentParser
*/
public function testAppliedContentParsersWithHeader()
{
$input = [
'server' => [
'CONTENT_TYPE' => 'text/html;version=2',
],
];

$builder = $this->getMock('Fracture\Http\RequestBuilder', ['isCLI']);

$builder->addContentParser('text/html', function ($header) {
return ['foo' => $header->getParameter('version')];
});

$instance = $builder->create($input);
$this->assertEquals(2, $instance->getParameter('foo'));
}


/**
* @covers Fracture\Http\RequestBuilder::create
* @covers Fracture\Http\RequestBuilder::applyContentParsers
Expand Down

0 comments on commit 526d1c0

Please sign in to comment.