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: CommonNetworkTest.php 309921 2011-04-03 16:43:02Z avb $
41
 * @link       http://pear.php.net/package/HTTP_Request2
42
 */
43
 
44
/** Class representing a HTTP request */
45
require_once 'HTTP/Request2.php';
46
 
47
/** Helper for PHPUnit includes */
48
require_once dirname(dirname(dirname(__FILE__))) . '/TestHelper.php';
49
 
50
/**
51
 * Tests for HTTP_Request2 package that require a working webserver
52
 *
53
 * The class contains some common tests that should be run for all Adapters,
54
 * it is extended by their unit tests.
55
 *
56
 * You need to properly set up this test suite, refer to NetworkConfig.php.dist
57
 */
58
abstract class HTTP_Request2_Adapter_CommonNetworkTest extends PHPUnit_Framework_TestCase
59
{
60
   /**
61
    * HTTP Request object
62
    * @var HTTP_Request2
63
    */
64
    protected $request;
65
 
66
   /**
67
    * Base URL for remote test files
68
    * @var string
69
    */
70
    protected $baseUrl;
71
 
72
   /**
73
    * Configuration for HTTP Request object
74
    * @var array
75
    */
76
    protected $config = array();
77
 
78
    protected function setUp()
79
    {
80
        if (!defined('HTTP_REQUEST2_TESTS_BASE_URL') || !HTTP_REQUEST2_TESTS_BASE_URL) {
81
            $this->markTestSkipped('Base URL is not configured');
82
 
83
        } else {
84
            $this->baseUrl = rtrim(HTTP_REQUEST2_TESTS_BASE_URL, '/') . '/';
85
            $name = strtolower(preg_replace('/^test/i', '', $this->getName())) . '.php';
86
 
87
            $this->request = new HTTP_Request2(
88
                $this->baseUrl . $name, HTTP_Request2::METHOD_GET, $this->config
89
            );
90
        }
91
    }
92
 
93
   /**
94
    * Tests possibility to send GET parameters
95
    *
96
    * NB: Currently there are problems with Net_URL2::setQueryVariables(), thus
97
    * array structure is simple: http://pear.php.net/bugs/bug.php?id=18267
98
    */
99
    public function testGetParameters()
100
    {
101
        $data = array(
102
            'bar' => array(
103
                'key' => 'value'
104
            ),
105
            'foo' => 'some value',
106
            'numbered' => array('first', 'second')
107
        );
108
 
109
        $this->request->getUrl()->setQueryVariables($data);
110
        $response = $this->request->send();
111
        $this->assertEquals($response->getBody(), serialize($data));
112
    }
113
 
114
    public function testPostParameters()
115
    {
116
        $data = array(
117
            'bar' => array(
118
                'key' => 'some other value'
119
            ),
120
            'baz' => array(
121
                'key1' => array(
122
                    'key2' => 'yet another value'
123
                )
124
            ),
125
            'foo' => 'some value',
126
            'indexed' => array('first', 'second')
127
        );
128
 
129
        $this->request->setMethod(HTTP_Request2::METHOD_POST)
130
                      ->addPostParameter($data);
131
 
132
        $response = $this->request->send();
133
        $this->assertEquals($response->getBody(), serialize($data));
134
    }
135
 
136
    public function testUploads()
137
    {
138
        $this->request->setMethod(HTTP_Request2::METHOD_POST)
139
                      ->addUpload('foo', dirname(dirname(dirname(__FILE__))) . '/_files/empty.gif', 'picture.gif', 'image/gif')
140
                      ->addUpload('bar', array(
141
                                    array(dirname(dirname(dirname(__FILE__))) . '/_files/empty.gif', null, 'image/gif'),
142
                                    array(dirname(dirname(dirname(__FILE__))) . '/_files/plaintext.txt', 'secret.txt', 'text/x-whatever')
143
                                  ));
144
 
145
        $response = $this->request->send();
146
        $this->assertContains("foo picture.gif image/gif 43", $response->getBody());
147
        $this->assertContains("bar[0] empty.gif image/gif 43", $response->getBody());
148
        $this->assertContains("bar[1] secret.txt text/x-whatever 15", $response->getBody());
149
    }
150
 
151
    public function testRawPostData()
152
    {
153
        $data = 'Nothing to see here, move along';
154
 
155
        $this->request->setMethod(HTTP_Request2::METHOD_POST)
156
                      ->setBody($data);
157
        $response = $this->request->send();
158
        $this->assertEquals($response->getBody(), $data);
159
    }
160
 
161
    public function testCookies()
162
    {
163
        $cookies = array(
164
            'CUSTOMER'    => 'WILE_E_COYOTE',
165
            'PART_NUMBER' => 'ROCKET_LAUNCHER_0001'
166
        );
167
 
168
        foreach ($cookies as $k => $v) {
169
            $this->request->addCookie($k, $v);
170
        }
171
        $response = $this->request->send();
172
        $this->assertEquals($response->getBody(), serialize($cookies));
173
    }
174
 
175
    public function testTimeout()
176
    {
177
        $this->request->setConfig('timeout', 2);
178
        try {
179
            $this->request->send();
180
            $this->fail('Expected HTTP_Request2_Exception was not thrown');
181
        } catch (HTTP_Request2_MessageException $e) {
182
            $this->assertEquals(HTTP_Request2_Exception::TIMEOUT, $e->getCode());
183
        }
184
    }
185
 
186
    public function testBasicAuth()
187
    {
188
        $this->request->getUrl()->setQueryVariables(array(
189
            'user' => 'luser',
190
            'pass' => 'qwerty'
191
        ));
192
        $wrong = clone $this->request;
193
 
194
        $this->request->setAuth('luser', 'qwerty');
195
        $response = $this->request->send();
196
        $this->assertEquals(200, $response->getStatus());
197
 
198
        $wrong->setAuth('luser', 'password');
199
        $response = $wrong->send();
200
        $this->assertEquals(401, $response->getStatus());
201
    }
202
 
203
    public function testDigestAuth()
204
    {
205
        $this->request->getUrl()->setQueryVariables(array(
206
            'user' => 'luser',
207
            'pass' => 'qwerty'
208
        ));
209
        $wrong = clone $this->request;
210
 
211
        $this->request->setAuth('luser', 'qwerty', HTTP_Request2::AUTH_DIGEST);
212
        $response = $this->request->send();
213
        $this->assertEquals(200, $response->getStatus());
214
 
215
        $wrong->setAuth('luser', 'password', HTTP_Request2::AUTH_DIGEST);
216
        $response = $wrong->send();
217
        $this->assertEquals(401, $response->getStatus());
218
    }
219
 
220
    public function testRedirectsDefault()
221
    {
222
        $this->request->setUrl($this->baseUrl . 'redirects.php')
223
                      ->setConfig(array('follow_redirects' => true, 'strict_redirects' => false))
224
                      ->setMethod(HTTP_Request2::METHOD_POST)
225
                      ->addPostParameter('foo', 'foo value');
226
 
227
        $response = $this->request->send();
228
        $this->assertContains('Method=GET', $response->getBody());
229
        $this->assertNotContains('foo', $response->getBody());
230
        $this->assertEquals($this->baseUrl . 'redirects.php?redirects=0', $response->getEffectiveUrl());
231
    }
232
 
233
    public function testRedirectsStrict()
234
    {
235
        $this->request->setUrl($this->baseUrl . 'redirects.php')
236
                      ->setConfig(array('follow_redirects' => true, 'strict_redirects' => true))
237
                      ->setMethod(HTTP_Request2::METHOD_POST)
238
                      ->addPostParameter('foo', 'foo value');
239
 
240
        $response = $this->request->send();
241
        $this->assertContains('Method=POST', $response->getBody());
242
        $this->assertContains('foo', $response->getBody());
243
    }
244
 
245
    public function testRedirectsLimit()
246
    {
247
        $this->request->setUrl($this->baseUrl . 'redirects.php?redirects=4')
248
                      ->setConfig(array('follow_redirects' => true, 'max_redirects' => 2));
249
 
250
        try {
251
            $this->request->send();
252
            $this->fail('Expected HTTP_Request2_Exception was not thrown');
253
        } catch (HTTP_Request2_MessageException $e) {
254
            $this->assertEquals(HTTP_Request2_Exception::TOO_MANY_REDIRECTS, $e->getCode());
255
        }
256
    }
257
 
258
    public function testRedirectsRelative()
259
    {
260
        $this->request->setUrl($this->baseUrl . 'redirects.php?special=relative')
261
                      ->setConfig(array('follow_redirects' => true));
262
 
263
        $response = $this->request->send();
264
        $this->assertContains('did relative', $response->getBody());
265
    }
266
 
267
    public function testRedirectsNonHTTP()
268
    {
269
        $this->request->setUrl($this->baseUrl . 'redirects.php?special=ftp')
270
                      ->setConfig(array('follow_redirects' => true));
271
 
272
        try {
273
            $this->request->send();
274
            $this->fail('Expected HTTP_Request2_Exception was not thrown');
275
        } catch (HTTP_Request2_MessageException $e) {
276
            $this->assertEquals(HTTP_Request2_Exception::NON_HTTP_REDIRECT, $e->getCode());
277
        }
278
    }
279
 
280
    public function testCookieJar()
281
    {
282
        $this->request->setUrl($this->baseUrl . 'setcookie.php?name=cookie_name&value=cookie_value');
283
        $req2 = clone $this->request;
284
 
285
        $this->request->setCookieJar()->send();
286
        $jar = $this->request->getCookieJar();
287
        $jar->store(
288
            array('name' => 'foo', 'value' => 'bar'),
289
            $this->request->getUrl()
290
        );
291
 
292
        $response = $req2->setUrl($this->baseUrl . 'cookies.php')->setCookieJar($jar)->send();
293
        $this->assertEquals(
294
            serialize(array('cookie_name' => 'cookie_value', 'foo' => 'bar')),
295
            $response->getBody()
296
        );
297
    }
298
 
299
    public function testCookieJarAndRedirect()
300
    {
301
        $this->request->setUrl($this->baseUrl . 'redirects.php?special=cookie')
302
                      ->setConfig('follow_redirects', true)
303
                      ->setCookieJar();
304
 
305
        $response = $this->request->send();
306
        $this->assertEquals(serialize(array('cookie_on_redirect' => 'success')), $response->getBody());
307
    }
308
}
309
?>