Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Unit tests for HTTP_Request2 package
4
 *
5
 * PHP version 5
6
 *
7
 * LICENSE:
8
 *
9
 * Copyright (c) 2008-2011, Alexey Borzov <avb@php.net>
10
 * All rights reserved.
11
 *
12
 * Redistribution and use in source and binary forms, with or without
13
 * modification, are permitted provided that the following conditions
14
 * are met:
15
 *
16
 *    * Redistributions of source code must retain the above copyright
17
 *      notice, this list of conditions and the following disclaimer.
18
 *    * Redistributions in binary form must reproduce the above copyright
19
 *      notice, this list of conditions and the following disclaimer in the
20
 *      documentation and/or other materials provided with the distribution.
21
 *    * The names of the authors may not be used to endorse or promote products
22
 *      derived from this software without specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
32
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category   HTTP
37
 * @package    HTTP_Request2
38
 * @author     Alexey Borzov <avb@php.net>
39
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
40
 * @version    SVN: $Id: ResponseTest.php 309665 2011-03-24 21:03:48Z avb $
41
 * @link       http://pear.php.net/package/HTTP_Request2
42
 */
43
 
44
/**
45
 * Class representing a HTTP response
46
 */
47
require_once 'HTTP/Request2/Response.php';
48
 
49
/** Helper for PHPUnit includes */
50
require_once dirname(dirname(__FILE__)) . '/TestHelper.php';
51
 
52
/**
53
 * Unit test for HTTP_Request2_Response class
54
 */
55
class HTTP_Request2_ResponseTest extends PHPUnit_Framework_TestCase
56
{
57
   /**
58
    *
59
    * @expectedException HTTP_Request2_MessageException
60
    */
61
    public function testParseStatusLine()
62
    {
63
        $response = new HTTP_Request2_Response('HTTP/1.1 200 OK');
64
        $this->assertEquals('1.1', $response->getVersion());
65
        $this->assertEquals(200, $response->getStatus());
66
        $this->assertEquals('OK', $response->getReasonPhrase());
67
 
68
        $response2 = new HTTP_Request2_Response('HTTP/1.2 222 Nishtyak!');
69
        $this->assertEquals('1.2', $response2->getVersion());
70
        $this->assertEquals(222, $response2->getStatus());
71
        $this->assertEquals('Nishtyak!', $response2->getReasonPhrase());
72
 
73
        $response3 = new HTTP_Request2_Response('Invalid status line');
74
    }
75
 
76
    public function testParseHeaders()
77
    {
78
        $response = $this->readResponseFromFile('response_headers');
79
        $this->assertEquals(7, count($response->getHeader()));
80
        $this->assertEquals('PHP/6.2.2', $response->getHeader('X-POWERED-BY'));
81
        $this->assertEquals('text/html; charset=windows-1251', $response->getHeader('cOnTeNt-TyPe'));
82
        $this->assertEquals('accept-charset, user-agent', $response->getHeader('vary'));
83
    }
84
 
85
    public function testParseCookies()
86
    {
87
        $response = $this->readResponseFromFile('response_cookies');
88
        $cookies  = $response->getCookies();
89
        $this->assertEquals(4, count($cookies));
90
        $expected = array(
91
            array('name' => 'foo', 'value' => 'bar', 'expires' => null,
92
                  'domain' => null, 'path' => null, 'secure' => false),
93
            array('name' => 'PHPSESSID', 'value' => '1234567890abcdef1234567890abcdef',
94
                  'expires' => null, 'domain' => null, 'path' => '/', 'secure' => true),
95
            array('name' => 'A', 'value' => 'B=C', 'expires' => null,
96
                  'domain' => null, 'path' => null, 'secure' => false),
97
            array('name' => 'baz', 'value' => '%20a%20value', 'expires' => 'Sun, 03 Jan 2010 03:04:05 GMT',
98
                  'domain' => 'pear.php.net', 'path' => null, 'secure' => false),
99
        );
100
        foreach ($cookies as $k => $cookie) {
101
            $this->assertEquals($expected[$k], $cookie);
102
        }
103
    }
104
 
105
   /**
106
    *
107
    * @expectedException HTTP_Request2_MessageException
108
    */
109
    public function testGzipEncoding()
110
    {
111
        $response = $this->readResponseFromFile('response_gzip');
112
        $this->assertEquals('0e964e9273c606c46afbd311b5ad4d77', md5($response->getBody()));
113
 
114
        $response = $this->readResponseFromFile('response_gzip_broken');
115
        $body = $response->getBody();
116
    }
117
 
118
    public function testDeflateEncoding()
119
    {
120
        $response = $this->readResponseFromFile('response_deflate');
121
        $this->assertEquals('0e964e9273c606c46afbd311b5ad4d77', md5($response->getBody()));
122
    }
123
 
124
    public function testBug15305()
125
    {
126
        $response = $this->readResponseFromFile('bug_15305');
127
        $this->assertEquals('c8c5088fc8a7652afef380f086c010a6', md5($response->getBody()));
128
    }
129
 
130
    public function testBug18169()
131
    {
132
        $response = $this->readResponseFromFile('bug_18169');
133
        $this->assertEquals('', $response->getBody());
134
    }
135
 
136
    protected function readResponseFromFile($filename)
137
    {
138
        $fp       = fopen(dirname(dirname(__FILE__)) . '/_files/' . $filename, 'rb');
139
        $response = new HTTP_Request2_Response(fgets($fp));
140
        do {
141
            $headerLine = fgets($fp);
142
            $response->parseHeaderLine($headerLine);
143
        } while ('' != trim($headerLine));
144
 
145
        while (!feof($fp)) {
146
            $response->appendBody(fread($fp, 1024));
147
        }
148
        return $response;
149
    }
150
}
151
?>