| 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: MultipartBodyTest.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(dirname(__FILE__)) . '/TestHelper.php';
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Unit test for HTTP_Request2_MultipartBody class
|
|
|
54 |
*/
|
|
|
55 |
class HTTP_Request2_MultipartBodyTest extends PHPUnit_Framework_TestCase
|
|
|
56 |
{
|
|
|
57 |
public function testUploadSimple()
|
|
|
58 |
{
|
|
|
59 |
$req = new HTTP_Request2(null, HTTP_Request2::METHOD_POST);
|
|
|
60 |
$body = $req->addPostParameter('foo', 'I am a parameter')
|
|
|
61 |
->addUpload('upload', dirname(dirname(__FILE__)) . '/_files/plaintext.txt')
|
|
|
62 |
->getBody();
|
|
|
63 |
|
|
|
64 |
$this->assertTrue($body instanceof HTTP_Request2_MultipartBody);
|
|
|
65 |
$asString = $body->__toString();
|
|
|
66 |
$boundary = $body->getBoundary();
|
|
|
67 |
$this->assertEquals($body->getLength(), strlen($asString));
|
|
|
68 |
$this->assertContains('This is a test.', $asString);
|
|
|
69 |
$this->assertContains('I am a parameter', $asString);
|
|
|
70 |
$this->assertRegexp("!--{$boundary}--\r\n$!", $asString);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
*
|
|
|
75 |
* @expectedException HTTP_Request2_LogicException
|
|
|
76 |
*/
|
|
|
77 |
public function testRequest16863()
|
|
|
78 |
{
|
|
|
79 |
$req = new HTTP_Request2(null, HTTP_Request2::METHOD_POST);
|
|
|
80 |
$fp = fopen(dirname(dirname(__FILE__)) . '/_files/plaintext.txt', 'rb');
|
|
|
81 |
$body = $req->addUpload('upload', $fp)
|
|
|
82 |
->getBody();
|
|
|
83 |
|
|
|
84 |
$asString = $body->__toString();
|
|
|
85 |
$this->assertContains('name="upload"; filename="anonymous.blob"', $asString);
|
|
|
86 |
$this->assertContains('This is a test.', $asString);
|
|
|
87 |
|
|
|
88 |
$req->addUpload('bad_upload', fopen('php://input', 'rb'));
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public function testStreaming()
|
|
|
92 |
{
|
|
|
93 |
$req = new HTTP_Request2(null, HTTP_Request2::METHOD_POST);
|
|
|
94 |
$body = $req->addPostParameter('foo', 'I am a parameter')
|
|
|
95 |
->addUpload('upload', dirname(dirname(__FILE__)) . '/_files/plaintext.txt')
|
|
|
96 |
->getBody();
|
|
|
97 |
$asString = '';
|
|
|
98 |
while ($part = $body->read(10)) {
|
|
|
99 |
$asString .= $part;
|
|
|
100 |
}
|
|
|
101 |
$this->assertEquals($body->getLength(), strlen($asString));
|
|
|
102 |
$this->assertContains('This is a test.', $asString);
|
|
|
103 |
$this->assertContains('I am a parameter', $asString);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
public function testUploadArray()
|
|
|
107 |
{
|
|
|
108 |
$req = new HTTP_Request2(null, HTTP_Request2::METHOD_POST);
|
|
|
109 |
$body = $req->addUpload('upload', array(
|
|
|
110 |
array(dirname(dirname(__FILE__)) . '/_files/plaintext.txt', 'bio.txt', 'text/plain'),
|
|
|
111 |
array(fopen(dirname(dirname(__FILE__)) . '/_files/empty.gif', 'rb'), 'photo.gif', 'image/gif')
|
|
|
112 |
))
|
|
|
113 |
->getBody();
|
|
|
114 |
$asString = $body->__toString();
|
|
|
115 |
$this->assertContains(file_get_contents(dirname(dirname(__FILE__)) . '/_files/empty.gif'), $asString);
|
|
|
116 |
$this->assertContains('name="upload[0]"; filename="bio.txt"', $asString);
|
|
|
117 |
$this->assertContains('name="upload[1]"; filename="photo.gif"', $asString);
|
|
|
118 |
|
|
|
119 |
$body2 = $req->setConfig(array('use_brackets' => false))->getBody();
|
|
|
120 |
$asString = $body2->__toString();
|
|
|
121 |
$this->assertContains('name="upload"; filename="bio.txt"', $asString);
|
|
|
122 |
$this->assertContains('name="upload"; filename="photo.gif"', $asString);
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
?>
|