Skip to content
Permalink
Browse files

Implement withHeader()

  • Loading branch information...
markstory committed Sep 20, 2016
1 parent 6501fd9 commit cea85d7b4f7d2f7899bddaf98b73774939b9dc8c
Showing with 32 additions and 0 deletions.
  1. +8 −0 src/Network/Request.php
  2. +24 −0 tests/TestCase/Network/RequestTest.php
@@ -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;
}
/**
@@ -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
*

0 comments on commit cea85d7

Please sign in to comment.
You can’t perform that action at this time.