ResponseTest.php
1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
use anlutro\cURL\Response;
class ResponseTest extends PHPUnit_Framework_TestCase
{
private function makeResponse($body, $headers, $info = array())
{
return new Response($body, $headers, $info);
}
/** @test */
public function parsesHttpResponseCodeCorrectly()
{
$r = $this->makeResponse('', 'HTTP/1.1 200 OK');
$this->assertEquals(200, $r->statusCode);
$this->assertEquals('200 OK', $r->statusText);
$r = $this->makeResponse('', 'HTTP/1.1 302 Found');
$this->assertEquals(302, $r->statusCode);
$this->assertEquals('302 Found', $r->statusText);
}
/** @test */
public function parsesHttp2ResponseCorrectly()
{
$r = $this->makeResponse('', 'HTTP/2 200 OK');
$this->assertEquals(200, $r->statusCode);
$this->assertEquals('200 OK', $r->statusText);
}
/** @test */
public function parsesHeaderStringCorrectly()
{
$header = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 0";
$r = $this->makeResponse('', $header);
$this->assertEquals('text/plain', $r->getHeader('content-type'));
$this->assertEquals('0', $r->getHeader('content-length'));
$this->assertEquals(null, $r->getHeader('x-nonexistant'));
}
/** @test */
public function duplicateHeadersAreHandled()
{
$header = "HTTP/1.1 200 OK\r\nX-Var: A\r\nX-Var: B\r\nX-Var: C";
$r = $this->makeResponse('', $header);
$this->assertEquals(array('A', 'B', 'C'), $r->getHeader('X-Var'));
}
/** @test */
public function httpContinueResponsesAreHandled()
{
$header = "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 OK\r\nx-var: foo";
$r = $this->makeResponse('', $header);
$this->assertEquals(200, $r->statusCode);
$this->assertEquals('foo', $r->getHeader('x-var'));
}
/** @test */
public function throwsExceptionIfHeaderDoesntStartWithHttpStatus()
{
$this->setExpectedException('InvalidArgumentException', 'Invalid response header');
$this->makeResponse('', 'x-var: foo');
}
}