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: Request2Test.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
/** Helper for PHPUnit includes */
50
require_once dirname(__FILE__) . '/TestHelper.php';
51
 
52
/**
53
 * Unit test for HTTP_Request2 class
54
 */
55
class HTTP_Request2Test extends PHPUnit_Framework_TestCase
56
{
57
    public function testConstructorSetsDefaults()
58
    {
59
        $url = new Net_URL2('http://www.example.com/foo');
60
        $req = new HTTP_Request2($url, HTTP_Request2::METHOD_POST, array('connect_timeout' => 666));
61
 
62
        $this->assertSame($url, $req->getUrl());
63
        $this->assertEquals(HTTP_Request2::METHOD_POST, $req->getMethod());
64
        $this->assertEquals(666, $req->getConfig('connect_timeout'));
65
    }
66
 
67
   /**
68
    *
69
    * @expectedException HTTP_Request2_LogicException
70
    */
71
    public function testSetUrl()
72
    {
73
        $urlString = 'http://www.example.com/foo/bar.php';
74
        $url       = new Net_URL2($urlString);
75
 
76
        $req1 = new HTTP_Request2();
77
        $req1->setUrl($url);
78
        $this->assertSame($url, $req1->getUrl());
79
 
80
        $req2 = new HTTP_Request2();
81
        $req2->setUrl($urlString);
82
        $this->assertType('Net_URL2', $req2->getUrl());
83
        $this->assertEquals($urlString, $req2->getUrl()->getUrl());
84
 
85
        $req3 = new HTTP_Request2();
86
        $req3->setUrl(array('This will cause an error'));
87
    }
88
 
89
    public function testConvertUserinfoToAuth()
90
    {
91
        $req = new HTTP_Request2();
92
        $req->setUrl('http://foo:b%40r@www.example.com/');
93
 
94
        $this->assertEquals('', (string)$req->getUrl()->getUserinfo());
95
        $this->assertEquals(
96
            array('user' => 'foo', 'password' => 'b@r', 'scheme' => HTTP_Request2::AUTH_BASIC),
97
            $req->getAuth()
98
        );
99
    }
100
 
101
   /**
102
    *
103
    * @expectedException HTTP_Request2_LogicException
104
    */
105
    public function testSetMethod()
106
    {
107
        $req = new HTTP_Request2();
108
        $req->setMethod(HTTP_Request2::METHOD_PUT);
109
        $this->assertEquals(HTTP_Request2::METHOD_PUT, $req->getMethod());
110
 
111
        $req->setMethod('Invalid method');
112
    }
113
 
114
    public function testSetAndGetConfig()
115
    {
116
        $req = new HTTP_Request2();
117
        $this->assertArrayHasKey('connect_timeout', $req->getConfig());
118
 
119
        $req->setConfig(array('connect_timeout' => 123));
120
        $this->assertEquals(123, $req->getConfig('connect_timeout'));
121
        try {
122
            $req->setConfig(array('foo' => 'unknown parameter'));
123
            $this->fail('Expected HTTP_Request2_LogicException was not thrown');
124
        } catch (HTTP_Request2_LogicException $e) {}
125
 
126
        try {
127
            $req->getConfig('bar');
128
            $this->fail('Expected HTTP_Request2_LogicException was not thrown');
129
        } catch (HTTP_Request2_LogicException $e) {}
130
    }
131
 
132
   /**
133
    *
134
    * @expectedException HTTP_Request2_LogicException
135
    */
136
    public function testHeaders()
137
    {
138
        $req = new HTTP_Request2();
139
        $autoHeaders = $req->getHeaders();
140
 
141
        $req->setHeader('Foo', 'Bar');
142
        $req->setHeader('Foo-Bar: value');
143
        $req->setHeader(array('Another-Header' => 'another value', 'Yet-Another: other_value'));
144
        $this->assertEquals(
145
            array('foo-bar' => 'value', 'another-header' => 'another value',
146
            'yet-another' => 'other_value', 'foo' => 'Bar') + $autoHeaders,
147
            $req->getHeaders()
148
        );
149
 
150
        $req->setHeader('FOO-BAR');
151
        $req->setHeader(array('aNOTHER-hEADER'));
152
        $this->assertEquals(
153
            array('yet-another' => 'other_value', 'foo' => 'Bar') + $autoHeaders,
154
            $req->getHeaders()
155
        );
156
 
157
        $req->setHeader('Invalid header', 'value');
158
    }
159
 
160
    public function testBug15937()
161
    {
162
        $req = new HTTP_Request2();
163
        $autoHeaders = $req->getHeaders();
164
 
165
        $req->setHeader('Expect: ');
166
        $req->setHeader('Foo', '');
167
        $this->assertEquals(
168
            array('expect' => '', 'foo' => '') + $autoHeaders,
169
            $req->getHeaders()
170
        );
171
    }
172
 
173
    public function testRequest17507()
174
    {
175
        $req = new HTTP_Request2();
176
 
177
        $req->setHeader('accept-charset', 'iso-8859-1');
178
        $req->setHeader('accept-charset', array('windows-1251', 'utf-8'), false);
179
 
180
        $req->setHeader(array('accept' => 'text/html'));
181
        $req->setHeader(array('accept' => 'image/gif'), null, false);
182
 
183
        $headers = $req->getHeaders();
184
 
185
        $this->assertEquals('iso-8859-1, windows-1251, utf-8', $headers['accept-charset']);
186
        $this->assertEquals('text/html, image/gif', $headers['accept']);
187
    }
188
 
189
   /**
190
    *
191
    * @expectedException HTTP_Request2_LogicException
192
    */
193
    public function testCookies()
194
    {
195
        $req = new HTTP_Request2();
196
        $req->addCookie('name', 'value');
197
        $req->addCookie('foo', 'bar');
198
        $headers = $req->getHeaders();
199
        $this->assertEquals('name=value; foo=bar', $headers['cookie']);
200
 
201
        $req->addCookie('invalid cookie', 'value');
202
    }
203
 
204
   /**
205
    *
206
    * @expectedException HTTP_Request2_LogicException
207
    */
208
    public function testPlainBody()
209
    {
210
        $req = new HTTP_Request2();
211
        $req->setBody('A string');
212
        $this->assertEquals('A string', $req->getBody());
213
 
214
        $req->setBody(dirname(__FILE__) . '/_files/plaintext.txt', true);
215
        $headers = $req->getHeaders();
216
        $this->assertRegexp(
217
            '!^(text/plain|application/octet-stream)!',
218
            $headers['content-type']
219
        );
220
        $this->assertEquals('This is a test.', fread($req->getBody(), 1024));
221
 
222
        $req->setBody('missing file', true);
223
    }
224
 
225
   /**
226
    *
227
    * @expectedException HTTP_Request2_LogicException
228
    */
229
    public function testRequest16863()
230
    {
231
        $req = new HTTP_Request2();
232
        $req->setBody(fopen(dirname(__FILE__) . '/_files/plaintext.txt', 'rb'));
233
        $headers = $req->getHeaders();
234
        $this->assertEquals('application/octet-stream', $headers['content-type']);
235
 
236
        $req->setBody(fopen('php://input', 'rb'));
237
    }
238
 
239
    public function testUrlencodedBody()
240
    {
241
        $req = new HTTP_Request2(null, HTTP_Request2::METHOD_POST);
242
        $req->addPostParameter('foo', 'bar');
243
        $req->addPostParameter(array('baz' => 'quux'));
244
        $req->addPostParameter('foobar', array('one', 'two'));
245
        $this->assertEquals(
246
            'foo=bar&baz=quux&foobar%5B0%5D=one&foobar%5B1%5D=two',
247
            $req->getBody()
248
        );
249
 
250
        $req->setConfig(array('use_brackets' => false));
251
        $this->assertEquals(
252
            'foo=bar&baz=quux&foobar=one&foobar=two',
253
            $req->getBody()
254
        );
255
    }
256
 
257
    public function testRequest15368()
258
    {
259
        $req = new HTTP_Request2(null, HTTP_Request2::METHOD_POST);
260
        $req->addPostParameter('foo', 'te~st');
261
        $this->assertContains('~', $req->getBody());
262
    }
263
 
264
   /**
265
    *
266
    * @expectedException HTTP_Request2_LogicException
267
    */
268
    public function testUpload()
269
    {
270
        $req = new HTTP_Request2(null, HTTP_Request2::METHOD_POST);
271
        $req->addUpload('upload', dirname(__FILE__) . '/_files/plaintext.txt');
272
 
273
        $headers = $req->getHeaders();
274
        $this->assertEquals('multipart/form-data', $headers['content-type']);
275
 
276
        $req->addUpload('upload_2', 'missing file');
277
    }
278
 
279
    public function testPropagateUseBracketsToNetURL2()
280
    {
281
        $req = new HTTP_Request2('http://www.example.com/', HTTP_Request2::METHOD_GET,
282
                                 array('use_brackets' => false));
283
        $req->getUrl()->setQueryVariable('foo', array('bar', 'baz'));
284
        $this->assertEquals('http://www.example.com/?foo=bar&foo=baz', $req->getUrl()->__toString());
285
 
286
        $req->setConfig('use_brackets', true)->setUrl('http://php.example.com/');
287
        $req->getUrl()->setQueryVariable('foo', array('bar', 'baz'));
288
        $this->assertEquals('http://php.example.com/?foo[0]=bar&foo[1]=baz', $req->getUrl()->__toString());
289
    }
290
 
291
    public function testSetBodyRemovesPostParameters()
292
    {
293
        $req = new HTTP_Request2('http://www.example.com/', HTTP_Request2::METHOD_POST);
294
        $req->addPostParameter('foo', 'bar');
295
        $req->setBody('');
296
        $this->assertEquals('', $req->getBody());
297
    }
298
 
299
    public function testPostParametersPrecedeSetBodyForPost()
300
    {
301
        $req = new HTTP_Request2('http://www.example.com/', HTTP_Request2::METHOD_POST);
302
        $req->setBody('Request body');
303
        $req->addPostParameter('foo', 'bar');
304
 
305
        $this->assertEquals('foo=bar', $req->getBody());
306
 
307
        $req->setMethod(HTTP_Request2::METHOD_PUT);
308
        $this->assertEquals('Request body', $req->getBody());
309
    }
310
 
311
    public function testSetMultipartBody()
312
    {
313
        require_once 'HTTP/Request2/MultipartBody.php';
314
 
315
        $req = new HTTP_Request2('http://www.example.com/', HTTP_Request2::METHOD_POST);
316
        $body = new HTTP_Request2_MultipartBody(array('foo' => 'bar'), array());
317
        $req->setBody($body);
318
        $this->assertSame($body, $req->getBody());
319
    }
320
 
321
    public function testBug17460()
322
    {
323
        $req = new HTTP_Request2('http://www.example.com/', HTTP_Request2::METHOD_POST);
324
        $req->addPostParameter('foo', 'bar')
325
            ->setHeader('content-type', 'application/x-www-form-urlencoded; charset=UTF-8');
326
 
327
        $this->assertEquals('foo=bar', $req->getBody());
328
    }
329
 
330
   /**
331
    *
332
    * @expectedException HTTP_Request2_LogicException
333
    */
334
    public function testCookieJar()
335
    {
336
        $req = new HTTP_Request2();
337
        $this->assertNull($req->getCookieJar());
338
 
339
        $req->setCookieJar();
340
        $jar = $req->getCookieJar();
341
        $this->assertType('HTTP_Request2_CookieJar', $jar);
342
 
343
        $req2 = new HTTP_Request2();
344
        $req2->setCookieJar($jar);
345
        $this->assertSame($jar, $req2->getCookieJar());
346
 
347
        $req2->setCookieJar(null);
348
        $this->assertNull($req2->getCookieJar());
349
 
350
        $req2->setCookieJar('foo');
351
    }
352
 
353
    public function testAddCookieToJar()
354
    {
355
        $req = new HTTP_Request2();
356
        $req->setCookieJar();
357
 
358
        try {
359
            $req->addCookie('foo', 'bar');
360
            $this->fail('Expected HTTP_Request2_Exception was not thrown');
361
        } catch (HTTP_Request2_LogicException $e) { }
362
 
363
        $req->setUrl('http://example.com/path/file.php');
364
        $req->addCookie('foo', 'bar');
365
 
366
        $this->assertArrayNotHasKey('cookie', $req->getHeaders());
367
        $cookies = $req->getCookieJar()->getAll();
368
        $this->assertEquals(
369
            array(
370
                'name'    => 'foo',
371
                'value'   => 'bar',
372
                'domain'  => 'example.com',
373
                'path'    => '/path/',
374
                'expires' => null,
375
                'secure'  => false
376
            ),
377
            $cookies[0]
378
        );
379
    }
380
}
381
?>