Skip to content

Commit

Permalink
Implement withHeader()
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Sep 20, 2016
1 parent 6501fd9 commit cea85d7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Network/Request.php
Expand Up @@ -1019,6 +1019,14 @@ public function getHeaderLine($name)
*/
public function withHeader($name, $value)
{
$new = clone $this;
$name = strtoupper(str_replace('-', '_', $name));
if (!in_array($name, ['CONTENT_LENGTH', 'CONTENT_TYPE'])) {
$name = 'HTTP_' . $name;
}
$new->_environment[$name] = $value;

return $new;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/TestCase/Network/RequestTest.php
Expand Up @@ -1328,6 +1328,30 @@ public function testGetHeaderLine()
$this->assertEquals('a, b', $request->getHeaderLine('Double'));
}

/**
* Test setting a header.
*
* @return void
*/
public function testWithHeader()
{
$request = new Request(['environment' => [
'HTTP_HOST' => 'localhost',
'CONTENT_TYPE' => 'application/json',
'CONTENT_LENGTH' => 1337,
'HTTP_CONTENT_MD5' => 'abc123',
'HTTP_DOUBLE' => ['a', 'b']
]]);
$new = $request->withHeader('Content-Length', 999);
$this->assertNotSame($new, $request);

$this->assertEquals(1337, $request->getHeaderLine('Content-length'), 'old request is unchanged');
$this->assertEquals(999, $new->getHeaderLine('Content-length'), 'new request is correct');

$new = $request->withHeader('Double', ['a']);
$this->assertEquals(['a'], $new->getHeader('Double'), 'List values are overwritten');
}

/**
* Test accepts() with and without parameters
*
Expand Down

0 comments on commit cea85d7

Please sign in to comment.