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: MockTest.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 request
46
 */
47
require_once 'HTTP/Request2.php';
48
 
49
/**
50
 * Mock adapter intended for testing
51
 */
52
require_once 'HTTP/Request2/Adapter/Mock.php';
53
 
54
/** Helper for PHPUnit includes */
55
require_once dirname(dirname(dirname(__FILE__))) . '/TestHelper.php';
56
 
57
/**
58
 * Unit test for HTTP_Request2_Response class
59
 */
60
class HTTP_Request2_Adapter_MockTest extends PHPUnit_Framework_TestCase
61
{
62
    public function testDefaultResponse()
63
    {
64
        $req = new HTTP_Request2('http://www.example.com/', HTTP_Request2::METHOD_GET,
65
                                 array('adapter' => 'mock'));
66
        $response = $req->send();
67
        $this->assertEquals(400, $response->getStatus());
68
        $this->assertEquals(0, count($response->getHeader()));
69
        $this->assertEquals('', $response->getBody());
70
    }
71
 
72
    public function testResponseFromString()
73
    {
74
        $mock = new HTTP_Request2_Adapter_Mock();
75
        $mock->addResponse(
76
            "HTTP/1.1 200 OK\r\n" .
77
            "Content-Type: text/plain; charset=iso-8859-1\r\n" .
78
            "\r\n" .
79
            "This is a string"
80
        );
81
        $req = new HTTP_Request2('http://www.example.com/');
82
        $req->setAdapter($mock);
83
 
84
        $response = $req->send();
85
        $this->assertEquals(200, $response->getStatus());
86
        $this->assertEquals(1, count($response->getHeader()));
87
        $this->assertEquals('This is a string', $response->getBody());
88
    }
89
 
90
    public function testResponseFromFile()
91
    {
92
        $mock = new HTTP_Request2_Adapter_Mock();
93
        $mock->addResponse(fopen(dirname(dirname(dirname(__FILE__))) .
94
                           '/_files/response_headers', 'rb'));
95
 
96
        $req = new HTTP_Request2('http://www.example.com/');
97
        $req->setAdapter($mock);
98
 
99
        $response = $req->send();
100
        $this->assertEquals(200, $response->getStatus());
101
        $this->assertEquals(7, count($response->getHeader()));
102
        $this->assertEquals('Nothing to see here, move along.', $response->getBody());
103
    }
104
 
105
    public function testResponsesQueue()
106
    {
107
        $mock = new HTTP_Request2_Adapter_Mock();
108
        $mock->addResponse(
109
            "HTTP/1.1 301 Over there\r\n" .
110
            "Location: http://www.example.com/newpage.html\r\n" .
111
            "\r\n" .
112
            "The document is over there"
113
        );
114
        $mock->addResponse(
115
            "HTTP/1.1 200 OK\r\n" .
116
            "Content-Type: text/plain; charset=iso-8859-1\r\n" .
117
            "\r\n" .
118
            "This is a string"
119
        );
120
 
121
        $req = new HTTP_Request2('http://www.example.com/');
122
        $req->setAdapter($mock);
123
        $this->assertEquals(301, $req->send()->getStatus());
124
        $this->assertEquals(200, $req->send()->getStatus());
125
        $this->assertEquals(400, $req->send()->getStatus());
126
    }
127
 
128
    public function testResponseException()
129
    {
130
        $mock = new HTTP_Request2_Adapter_Mock();
131
        $mock->addResponse(
132
            new HTTP_Request2_Exception('Shit happens')
133
        );
134
        $req = new HTTP_Request2('http://www.example.com/');
135
        $req->setAdapter($mock);
136
        try {
137
            $req->send();
138
        } catch (Exception $e) {
139
            $this->assertEquals('Shit happens', $e->getMessage());
140
            return;
141
        }
142
        $this->fail('Expected HTTP_Request2_Exception was not thrown');
143
    }
144
}
145
?>