3
namespace GuzzleHttp\Tests\Message;
5
use GuzzleHttp\Exception\XmlParseException;
6
use GuzzleHttp\Message\Response;
7
use GuzzleHttp\Stream\Stream;
10
* @covers GuzzleHttp\Message\Response
12
class ResponseTest extends \PHPUnit_Framework_TestCase
14
public function testCanProvideCustomStatusCodeAndReasonPhrase()
16
$response = new Response(999, [], null, ['reason_phrase' => 'hi!']);
17
$this->assertEquals(999, $response->getStatusCode());
18
$this->assertEquals('hi!', $response->getReasonPhrase());
21
public function testConvertsToString()
23
$response = new Response(200);
24
$this->assertEquals("HTTP/1.1 200 OK\r\n\r\n", (string) $response);
26
$response = new Response(200, ['X-Test' => 'Guzzle']);
27
$this->assertEquals("HTTP/1.1 200 OK\r\nX-Test: Guzzle\r\n\r\n", (string) $response);
28
$response = new Response(200, ['Content-Length' => 4], Stream::factory('test'));
29
$this->assertEquals("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ntest", (string) $response);
32
public function testConvertsToStringAndSeeksToByteZero()
34
$response = new Response(200);
35
$s = Stream::factory('foo');
37
$response->setBody($s);
38
$this->assertEquals("HTTP/1.1 200 OK\r\n\r\nfoo", (string) $response);
41
public function testParsesJsonResponses()
43
$json = '{"foo": "bar"}';
44
$response = new Response(200, [], Stream::factory($json));
45
$this->assertEquals(['foo' => 'bar'], $response->json());
46
$this->assertEquals(json_decode($json), $response->json(['object' => true]));
48
$response = new Response(200);
49
$this->assertEquals(null, $response->json());
53
* @expectedException \GuzzleHttp\Exception\ParseException
54
* @expectedExceptionMessage Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON
56
public function testThrowsExceptionWhenFailsToParseJsonResponse()
58
$response = new Response(200, [], Stream::factory('{"foo": "'));
62
public function testParsesXmlResponses()
64
$response = new Response(200, [], Stream::factory('<abc><foo>bar</foo></abc>'));
65
$this->assertEquals('bar', (string) $response->xml()->foo);
66
// Always return a SimpleXMLElement from the xml method
67
$response = new Response(200);
68
$this->assertEmpty((string) $response->xml()->foo);
72
* @expectedException \GuzzleHttp\Exception\XmlParseException
73
* @expectedExceptionMessage Unable to parse response body into XML: String could not be parsed as XML
75
public function testThrowsExceptionWhenFailsToParseXmlResponse()
77
$response = new Response(200, [], Stream::factory('<abc'));
80
} catch (XmlParseException $e) {
81
$xmlParseError = $e->getError();
82
$this->assertInstanceOf('\LibXMLError', $xmlParseError);
83
$this->assertContains("Couldn't find end of Start Tag abc line 1", $xmlParseError->message);
88
public function testHasEffectiveUrl()
90
$r = new Response(200);
91
$this->assertNull($r->getEffectiveUrl());
92
$r->setEffectiveUrl('http://www.test.com');
93
$this->assertEquals('http://www.test.com', $r->getEffectiveUrl());
96
public function testPreventsComplexExternalEntities()
98
$xml = '<?xml version="1.0"?><!DOCTYPE scan[<!ENTITY test SYSTEM "php://filter/read=convert.base64-encode/resource=ResponseTest.php">]><scan>&test;</scan>';
99
$response = new Response(200, [], Stream::factory($xml));
104
$xml = $response->xml();
106
$this->markTestIncomplete('Did not throw the expected exception! XML resolved as: ' . $xml->asXML());
107
} catch (\Exception $e) {