summaryrefslogtreecommitdiff
path: root/test/HTTP_POST_multipartTest.php
blob: 5d7c32ed21ef8221e12433a3d1d0c14d2c1f3832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

#
# forum-list bridge 
# Copyright (C) 2010 Joel Uckelman
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

require_once('src/HTTP_POST_multipart.php');

class HTTP_POST_multipartTest extends PHPUnit_Framework_TestCase {

  protected static function getMethod($name) {
    $class = new ReflectionClass('HTTP_POST_multipart');
    $method = $class->getMethod($name);
    $method->setAccessible(true);
    return $method;
  }

  /**
   * @dataProvider providerBuildDataPart
   */
  public function testBuildDataPart($part, $expected, $ex) {
    if ($ex) $this->setExpectedException($ex);

    $this->assertEquals(
      $expected,
      self::getMethod('buildDataPart')->invokeArgs(null, array($part))
    );
  }

  public function providerBuildDataPart() {
    return array(
      array(null, null, 'Exception'),
      array(
        array('name' => 'foo', 'data' => 1),
        "Content-Disposition: form-data; name=\"foo\"\r\n\r\n1\r\n",
        null
      )
    );
  }

  /**
   * @dataProvider providerBuildFilePart
   */
  public function testBuildFilePart($part, $expected, $ex) {
    if ($ex) $this->setExpectedException($ex);

    $this->assertEquals(
      $expected,
      self::getMethod('buildFilePart')->invokeArgs(null, array($part))
    );
  }

  public function providerBuildFilePart() {
    return array(
      array(null, null, 'Exception'),
      array(
        array(
          'name'     => 'foo',
          'filename' => 'somename.txt',
          'mimetype' => 'text/plain',
          'charset'  => 'utf-8',
          'encoding' => null,
          'data'     => "blah blah blah\nblah blah blah"
        ),
        "Content-Disposition: form-data; name=\"foo\"; filename=\"somename.txt\"\r\nContent-Type: text/plain; charset=\"utf-8\"\r\n\r\nblah blah blah\nblah blah blah\r\n",
        null
      ),
      array(
        array(
          'name'     => 'foo',
          'filename' => 'somename.png',
          'mimetype' => 'image/png',
          'charset'  => null,
          'encoding' => 'binary',
          'data'     => "blah blah blah\nblah blah blah"
        ),
        "Content-Disposition: form-data; name=\"foo\"; filename=\"somename.png\"\r\nContent-Type: image/png\r\nContent-Transfer-Encoding: binary\r\n\r\nblah blah blah\nblah blah blah\r\n",
        null
      ),
      array(
        array(
          'name'     => 'foo',
          'filename' => 'somename.png',
          'mimetype' => 'image/png',
          'charset'  => null,
          'encoding' => 'base64',
          'data'     => "blah blah blah\nblah blah blah"
        ),
        "Content-Disposition: form-data; name=\"foo\"; filename=\"somename.png\"\r\nContent-Type: image/png\r\nContent-Transfer-Encoding: base64\r\n\r\nYmxhaCBibGFoIGJsYWgKYmxhaCBibGFoIGJsYWg=\r\n\r\n",
        null
      )
    );
  }

  /**
   * @dataProvider providerBuildPost
   */
  public function testBuildPost($parts, $expected, $ex) {
    if ($ex) $this->setExpectedException($ex);

    list($boundary, $content) =
      self::getMethod('buildPost')->invokeArgs(null, array($parts));

    $expected = str_replace('boundary', $boundary, $expected); 
    $this->assertEquals($expected, $content);
  }

  public function providerBuildPost() {
    return array(
      array(null, null, 'Exception'),
      array(
        array(
          array(
            'name' => 'foo',
            'data' => 1
          ),
          array(
            'name'     => 'foo',
            'filename' => 'somename.txt',
            'mimetype' => 'text/plain',
            'charset'  => 'utf-8',
            'encoding' => null,
            'data'     => "blah blah blah\nblah blah blah"
          )
        ),
        "--boundary\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\n1\r\n--boundary\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"somename.txt\"\r\nContent-Type: text/plain; charset=\"utf-8\"\r\n\r\nblah blah blah\nblah blah blah\r\n--boundary--\r\n",
        null
      )
    );
  }
}

?>