~ballot/wordpress/openstack-objectstorage-breaking-insight

« back to all changes in this revision

Viewing changes to vendor/guzzlehttp/guzzle/tests/Message/ResponseTest.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
namespace GuzzleHttp\Tests\Message;
 
4
 
 
5
use GuzzleHttp\Exception\XmlParseException;
 
6
use GuzzleHttp\Message\Response;
 
7
use GuzzleHttp\Stream\Stream;
 
8
 
 
9
/**
 
10
 * @covers GuzzleHttp\Message\Response
 
11
 */
 
12
class ResponseTest extends \PHPUnit_Framework_TestCase
 
13
{
 
14
    public function testCanProvideCustomStatusCodeAndReasonPhrase()
 
15
    {
 
16
        $response = new Response(999, [], null, ['reason_phrase' => 'hi!']);
 
17
        $this->assertEquals(999, $response->getStatusCode());
 
18
        $this->assertEquals('hi!', $response->getReasonPhrase());
 
19
    }
 
20
 
 
21
    public function testConvertsToString()
 
22
    {
 
23
        $response = new Response(200);
 
24
        $this->assertEquals("HTTP/1.1 200 OK\r\n\r\n", (string) $response);
 
25
        // Add another header
 
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);
 
30
    }
 
31
 
 
32
    public function testConvertsToStringAndSeeksToByteZero()
 
33
    {
 
34
        $response = new Response(200);
 
35
        $s = Stream::factory('foo');
 
36
        $s->read(1);
 
37
        $response->setBody($s);
 
38
        $this->assertEquals("HTTP/1.1 200 OK\r\n\r\nfoo", (string) $response);
 
39
    }
 
40
 
 
41
    public function testParsesJsonResponses()
 
42
    {
 
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]));
 
47
 
 
48
        $response = new Response(200);
 
49
        $this->assertEquals(null, $response->json());
 
50
    }
 
51
 
 
52
    /**
 
53
     * @expectedException \GuzzleHttp\Exception\ParseException
 
54
     * @expectedExceptionMessage Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON
 
55
     */
 
56
    public function testThrowsExceptionWhenFailsToParseJsonResponse()
 
57
    {
 
58
        $response = new Response(200, [], Stream::factory('{"foo": "'));
 
59
        $response->json();
 
60
    }
 
61
 
 
62
    public function testParsesXmlResponses()
 
63
    {
 
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);
 
69
    }
 
70
 
 
71
    /**
 
72
     * @expectedException \GuzzleHttp\Exception\XmlParseException
 
73
     * @expectedExceptionMessage Unable to parse response body into XML: String could not be parsed as XML
 
74
     */
 
75
    public function testThrowsExceptionWhenFailsToParseXmlResponse()
 
76
    {
 
77
        $response = new Response(200, [], Stream::factory('<abc'));
 
78
        try {
 
79
            $response->xml();
 
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);
 
84
            throw $e;
 
85
        }
 
86
    }
 
87
 
 
88
    public function testHasEffectiveUrl()
 
89
    {
 
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());
 
94
    }
 
95
 
 
96
    public function testPreventsComplexExternalEntities()
 
97
    {
 
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));
 
100
 
 
101
        $oldCwd = getcwd();
 
102
        chdir(__DIR__);
 
103
        try {
 
104
            $xml = $response->xml();
 
105
            chdir($oldCwd);
 
106
            $this->markTestIncomplete('Did not throw the expected exception! XML resolved as: ' . $xml->asXML());
 
107
        } catch (\Exception $e) {
 
108
            chdir($oldCwd);
 
109
        }
 
110
    }
 
111
}