Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed Mar 28, 2015
1 parent 49510de commit 0c79977
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/MessageTrait.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace GuzzleHttp\Psr7;

use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamableInterface;

/**
Expand All @@ -27,6 +28,10 @@ public function getProtocolVersion()

public function withProtocolVersion($version)
{
if ($this->protocol === $version) {
return $this;
}

$new = clone $this;
$new->protocol = $version;
return $new;
Expand Down Expand Up @@ -86,14 +91,14 @@ public function withHeader($header, $value)

public function withAddedHeader($header, $value)
{
if (is_array($value)) {
$current = array_merge($this->getHeaderLines($header), $value);
} else {
$current = $this->getHeaderLines($header);
$current[] = (string) $value;
if (!$this->hasHeader($header)) {
return $this->withHeader($header, $value);
}

return $this->withHeader($header, $current);
$new = clone $this;
$new->headers[strtolower($header)][] = $value;
$new->headerLines[$header][] = $value;
return $new;
}

public function withoutHeader($header)
Expand Down Expand Up @@ -126,6 +131,10 @@ public function getBody()

public function withBody(StreamableInterface $body)
{
if ($body === $this->stream) {
return $this;
}

$new = clone $this;
$new->stream = $body;
return $new;
Expand Down
10 changes: 10 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,14 @@ public function testAddsPortToHeaderAndReplacePreviousPort()
$r = $r->withUri(new Uri('http://foo.com:8125/bar'));
$this->assertEquals('foo.com:8125', $r->getHeader('host'));
}

public function testUpdatesHeaderAndRemovesSoft()
{
$r = new Request('GET', 'http://foo.com');
$this->assertEquals('foo.com', $r->getHeader('host'));
$r2 = $r->withHeader('Host', 'baz.com');
$this->assertEquals('baz.com', $r2->getHeader('host'));
$r3 = $r2->withUri(new Uri('http://bam.com'));
$this->assertEquals('baz.com', $r3->getHeader('host'));
}
}
60 changes: 60 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace GuzzleHttp\Tests\Psr7;

use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7;

/**
* @covers GuzzleHttp\Psr7\MessageTrait
Expand Down Expand Up @@ -76,4 +77,63 @@ public function testCanSetHeaderAsArray()
$this->assertEquals('baz, bar', $r->getHeader('foo'));
$this->assertEquals(['baz', 'bar'], $r->getHeaderLines('foo'));
}

public function testSameInstanceWhenSameBody()
{
$r = new Response(200, [], 'foo');
$b = $r->getBody();
$this->assertSame($r, $r->withBody($b));
}

public function testNewInstanceWhenNewBody()
{
$r = new Response(200, [], 'foo');
$b2 = Psr7\stream_for('abc');
$this->assertNotSame($r, $r->withBody($b2));
}

public function testSameInstanceWhenSameProtocol()
{
$r = new Response(200);
$this->assertSame($r, $r->withProtocolVersion('1.1'));
}

public function testNewInstanceWhenNewProtocol()
{
$r = new Response(200);
$this->assertNotSame($r, $r->withProtocolVersion('1.0'));
}

public function testNewInstanceWhenRemovingHeader()
{
$r = new Response(200, ['Foo' => 'Bar']);
$r2 = $r->withoutHeader('Foo');
$this->assertNotSame($r, $r2);
$this->assertFalse($r2->hasHeader('foo'));
}

public function testNewInstanceWhenAddingHeader()
{
$r = new Response(200, ['Foo' => 'Bar']);
$r2 = $r->withAddedHeader('Foo', 'Baz');
$this->assertNotSame($r, $r2);
$this->assertEquals('Bar, Baz', $r2->getHeader('foo'));
}

public function testNewInstanceWhenAddingHeaderThatWasNotThereBefore()
{
$r = new Response(200, ['Foo' => 'Bar']);
$r2 = $r->withAddedHeader('Baz', 'Bam');
$this->assertNotSame($r, $r2);
$this->assertEquals('Bam', $r2->getHeader('Baz'));
$this->assertEquals('Bar', $r2->getHeader('Foo'));
}

public function testRemovesPreviouslyAddedHeaderOfDifferentCase()
{
$r = new Response(200, ['Foo' => 'Bar']);
$r2 = $r->withHeader('foo', 'Bam');
$this->assertNotSame($r, $r2);
$this->assertEquals('Bam', $r2->getHeader('Foo'));
}
}

0 comments on commit 0c79977

Please sign in to comment.