8
8
9
9
class ResponseParserTest extends TestCase
10
10
{
11
- public function testParsesXmlString ()
11
+ public function testParsesJsonString ()
12
12
{
13
- $ data = ResponseParser::xml ( ' <Foo>< Baz> Bar</Baz></Foo> ' );
13
+ $ data = ResponseParser::json ( ' {" Baz":" Bar"} ' );
14
14
15
- $ this ->assertInstanceOf ('SimpleXMLElement ' , $ data );
16
- $ this ->assertEquals ('Bar ' , (string ) $ data ->Baz );
15
+ $ this ->assertEquals (array ('Baz ' => 'Bar ' ), $ data );
17
16
}
18
17
19
- public function testParsesXmlResponse ()
18
+ public function testParsesJsonResponse ()
20
19
{
21
- $ response = new Response (200 , [], '<Foo>< Baz> Bar</Baz></Foo> ' );
20
+ $ response = new Response (200 , [], '{" Baz":" Bar"} ' );
22
21
23
- $ data = ResponseParser::xml ($ response );
22
+ $ data = ResponseParser::json ($ response );
24
23
25
- $ this ->assertInstanceOf ('SimpleXMLElement ' , $ data );
26
- $ this ->assertEquals ('Bar ' , (string ) $ data ->Baz );
24
+ $ this ->assertEquals (array ('Baz ' => 'Bar ' ), $ data );
27
25
}
28
26
29
- public function testParsesXmlResponseException ()
27
+ /**
28
+ * @expectedException RuntimeException
29
+ * @expectedExceptionMessage Unable to parse response body into JSON: 4
30
+ */
31
+ public function testParsesJsonResponseException ()
30
32
{
31
33
$ this ->expectException (RuntimeException::class);
32
34
33
35
$ response = new Response (200 , [], 'FooBar ' );
34
36
35
- ResponseParser::xml ($ response );
37
+ ResponseParser::json ($ response );
36
38
}
37
39
38
- public function testParsesJsonString ()
40
+ public function testParsesXmlString ()
39
41
{
40
- $ data = ResponseParser::json ( ' {" Baz":" Bar"} ' );
42
+ $ data = ResponseParser::xml ( ' <Foo>< Baz> Bar</Baz></Foo> ' );
41
43
42
- $ this ->assertEquals (array ('Baz ' => 'Bar ' ), $ data );
44
+ $ this ->assertInstanceOf ('SimpleXMLElement ' , $ data );
45
+ $ this ->assertEquals ('Bar ' , (string ) $ data ->Baz );
43
46
}
44
47
45
- public function testParsesJsonResponse ()
48
+ public function testParsesXmlResponse ()
46
49
{
47
- $ response = new Response (200 , [], '{" Baz":" Bar"} ' );
50
+ $ response = new Response (200 , [], '<Foo>< Baz> Bar</Baz></Foo> ' );
48
51
49
- $ data = ResponseParser::json ($ response );
52
+ $ data = ResponseParser::xml ($ response );
50
53
51
- $ this ->assertEquals (array ('Baz ' => 'Bar ' ), $ data );
54
+ $ this ->assertInstanceOf ('SimpleXMLElement ' , $ data );
55
+ $ this ->assertEquals ('Bar ' , (string ) $ data ->Baz );
52
56
}
53
57
54
- public function testParsesJsonResponseException ()
58
+ /**
59
+ * @expectedException RuntimeException
60
+ * @expectedExceptionMessage Unable to parse response body into XML: String could not be parsed as XML
61
+ */
62
+ public function testParsesXmlResponseException ()
55
63
{
56
- $ this -> expectException (RuntimeException::class );
64
+ $ response = new Response ( 200 , [], ' <abc ' );
57
65
58
- $ response = new Response (200 , [], 'FooBar ' );
66
+ ResponseParser::xml ($ response );
67
+ }
59
68
60
- ResponseParser::json ($ response );
69
+ /**
70
+ * Based on https://github.com/guzzle/guzzle3/blob/v3.9.3/tests/Guzzle/Tests/Http/Message/ResponseTest.php#L662-L676
71
+ */
72
+ public function testPreventsComplexExternalEntities ()
73
+ {
74
+ $ xml = '<?xml version="1.0"?><!DOCTYPE scan[<!ENTITY test SYSTEM "php://filter/read=convert.base64-encode/resource=ResponseTest.php">]><scan>&test;</scan> ' ;
75
+ $ response = new Response (200 , [], $ xml );
76
+ $ oldCwd = getcwd ();
77
+ chdir (__DIR__ );
78
+ try {
79
+ $ xml = ResponseParser::xml ($ response );
80
+ chdir ($ oldCwd );
81
+ $ this ->markTestIncomplete ('Did not throw the expected exception! XML resolved as: ' . $ xml ->asXML ());
82
+ } catch (\Exception $ e ) {
83
+ chdir ($ oldCwd );
84
+ }
61
85
}
62
86
63
87
}
0 commit comments