Completed
Push — master ( a2233b...d3bba2 )
by Chad
10s
created

HttpTest::buildQueryStringComplexValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace TraderInteractive\Util;
4
5
use PHPUnit\Framework\TestCase;
6
7
/**
8
 * @coversDefaultClass \TraderInteractive\Util\Http
9
 */
10
final class HttpTest extends TestCase
11
{
12
    /**
13
     * @test
14
     * @group unit
15
     * @covers ::parseHeaders
16
     *
17
     * @return void
18
     */
19
    public function parseHeadersBasicUsage()
20
    {
21
        $headers = 'Content-Type: text/json';
22
        $result = Http::parseHeaders($headers);
23
        $this->assertSame(['Content-Type' => 'text/json'], $result);
24
    }
25
26
    /**
27
     * @test
28
     * @group unit
29
     * @covers ::parseHeaders
30
     *
31
     * @return void
32
     */
33
    public function parseHeadersMalformed()
34
    {
35
        try {
36
            $headers = "&some\r\nbad+headers";
37
            Http::parseHeaders($headers);
38
            $this->fail('No exception thrown');
39
        } catch (\Exception $e) {
40
            $this->assertSame('Unsupported header format: &some', $e->getMessage());
41
        }
42
    }
43
44
    /**
45
     * Verifies parseHeaders retains the functionality of http_parse_headers()
46
     *
47
     * @test
48
     * @group unit
49
     * @covers ::parseHeaders
50
     *
51
     * @return void
52
     */
53
    public function parseHeadersPeclHttpFunctionality()
54
    {
55
        $headers = <<<EOT
56
HTTP/1.1 200 OK\r\n
57
content-type: text/html; charset=UTF-8\r\n
58
Server: Funky/1.0\r\n
59
Set-Cookie: foo=bar\r\n
60
Set-Cookie: baz=quux\r\n
61
Set-Cookie: key=value\r\n
62
EOT;
63
        $expected = [
64
            'Response Code' => 200,
65
            'Response Status' => 'OK',
66
            'Content-Type' => 'text/html; charset=UTF-8',
67
            'Server' => 'Funky/1.0',
68
            'Set-Cookie' => ['foo=bar', 'baz=quux', 'key=value'],
69
        ];
70
        $result = Http::parseHeaders($headers);
71
        $this->assertSame($expected, $result);
72
    }
73
74
    /**
75
     * Verifies Request Method and Request Url are set properly
76
     *
77
     * @test
78
     * @group unit
79
     * @covers ::parseHeaders
80
     *
81
     * @return void
82
     */
83
    public function parseHeadersMethodAndUrlSet()
84
    {
85
        $headers = <<<EOT
86
GET /file.xml HTTP/1.1\r\n
87
Host: www.example.com\r\n
88
Accept: */*\r\n
89
EOT;
90
        $expected = [
91
            'Request Method' => 'GET',
92
            'Request Url' => '/file.xml',
93
            'Host' => 'www.example.com',
94
            'Accept' => '*/*'
95
        ];
96
        $result = Http::parseHeaders($headers);
97
        $this->assertSame($expected, $result);
98
    }
99
100
    /**
101
     * Verifies that the rawHeaders string cannot be whitespace.
102
     *
103
     * @test
104
     * @group unit
105
     * @covers ::parseHeaders
106
     *
107
     * @expectedException \InvalidArgumentException
108
     * @expectedExceptionMessage $rawHeaders cannot be whitespace
109
     *
110
     * @return void
111
     */
112
    public function parseHeadersWhitespace()
113
    {
114
        Http::parseHeaders('');
115
    }
116
117
    /**
118
     * @test
119
     * @covers ::buildQueryString
120
     *
121
     * @return void
122
     */
123
    public function buildQueryStringBasicUse()
124
    {
125
        $data = [
126
            'foo' => 'bar',
127
            'baz' => 'boom',
128
            'cow' => 'milk',
129
            'php' => 'hypertext processor',
130
            'theFalse' => false,
131
            'theTrue' => true
132
        ];
133
134
        $this->assertSame(
135
            'foo=bar&baz=boom&cow=milk&php=hypertext%20processor&theFalse=false&theTrue=true',
136
            Http::buildQueryString($data)
137
        );
138
    }
139
140
    /**
141
     * @test
142
     * @covers ::buildQueryString
143
     *
144
     * @return void
145
     */
146
    public function buildQueryStringMultiValue()
147
    {
148
        $data = ['param1' => ['value', 'another value'], 'param2' => 'a value'];
149
150
        $this->assertSame('param1=value&param1=another%20value&param2=a%20value', Http::buildQueryString($data));
151
    }
152
153
    /**
154
     * @test
155
     * @covers ::buildQueryString
156
     *
157
     * @return void
158
     */
159
    public function buildQueryStringComplexValues()
160
    {
161
        $this->assertSame(
162
            'a%20b%20c=1%242%283&a%20b%20c=4%295%2A6',
163
            Http::buildQueryString(['a b c' => ['1$2(3', '4)5*6']])
164
        );
165
    }
166
167
    /**
168
     * Verifies Multi Parameter Method can handle a normal url
169
     *
170
     * @test
171
     * @group unit
172
     * @covers ::getQueryParams
173
     *
174
     * @return void
175
     */
176
    public function getQueryParamsNormal()
177
    {
178
        $url = 'http://foo.com/bar/?otherStuff=green&stuff=yeah&moreStuff=rock&moreStuff=jazz&otherStuff=blue&'
179
             . 'otherStuff=black';
180
        $expected = [
181
            'otherStuff' => ['green', 'blue', 'black'],
182
            'stuff' => ['yeah'],
183
            'moreStuff' => ['rock', 'jazz'],
184
        ];
185
        $result = Http::getQueryParams($url);
186
        $this->assertSame($expected, $result);
187
    }
188
189
    /**
190
     * Verifies Multi Parameter Method can handle a url with an empty parameter
191
     *
192
     * @test
193
     * @group unit
194
     * @covers ::getQueryParams
195
     *
196
     * @return void
197
     */
198
    public function getQueryParamsEmptyParameter()
199
    {
200
        $url = 'http://foo.com/bar/?stuff=yeah&moreStuff=&moreStuff=jazz&otherStuff';
201
        $expected = [
202
            'stuff' => ['yeah'],
203
            'moreStuff' => ['', 'jazz'],
204
            'otherStuff' => [''],
205
        ];
206
        $result = Http::getQueryParams($url);
207
        $this->assertSame($expected, $result);
208
    }
209
210
    /**
211
     * Verifies multi parameter method with a garbage query string
212
     *
213
     * @test
214
     * @group unit
215
     * @covers ::getQueryParams
216
     *
217
     * @return void
218
     */
219
    public function getQueryParamsGarbage()
220
    {
221
        $this->assertSame([], Http::getQueryParams('GARBAGE'));
222
    }
223
224
    /**
225
     * @test
226
     * @covers ::getQueryParams
227
     *
228
     * @return void
229
     */
230
    public function getQueryParamsWithCollapsed()
231
    {
232
        $result = Http::getQueryParams('http://foo.com/bar/?stuff=yeah&moreStuff=mhmm', ['stuff', 'notThere']);
233
        $this->assertSame(['stuff' => 'yeah', 'moreStuff' => ['mhmm']], $result);
234
    }
235
236
    /**
237
     * @test
238
     * @covers ::getQueryParams
239
     * @expectedException \Exception
240
     * @expectedExceptionMessage Parameter 'stuff' had more than one value but in $collapsedParams
241
     *
242
     * @return void
243
     */
244
    public function getQueryParamsCollapsedMoreThanOneValue()
245
    {
246
        Http::getQueryParams('http://foo.com/bar/?stuff=yeah&stuff=boy&moreStuff=mhmm', ['stuff']);
247
    }
248
249
    /**
250
     * @test
251
     * @covers ::getQueryParamsCollapsed
252
     *
253
     * @return void
254
     */
255
    public function getQueryParamsCollapsed()
256
    {
257
        $url = 'http://foo.com/bar/?boo=1&foo=bar&boo=2';
258
        $actual = Http::getQueryParamsCollapsed($url, ['boo']);
259
        $this->assertSame(['boo' => ['1', '2'], 'foo' => 'bar'], $actual);
260
    }
261
262
    /**
263
     * @test
264
     * @covers ::getQueryParamsCollapsed
265
     * @expectedException \Exception
266
     * @expectedExceptionMessage Parameter 'boo' is not expected to be an array, but array given
267
     *
268
     * @return void
269
     */
270
    public function getQueryParamsCollapsedUnexpectedArray()
271
    {
272
        $url = 'http://foo.com/bar/?boo=1&foo=bar&boo=2';
273
        Http::getQueryParamsCollapsed($url);
274
    }
275
276
    /**
277
     * Verifies multi parameter method with a garbage query string
278
     *
279
     * @test
280
     * @covers ::getQueryParamsCollapsed
281
     *
282
     * @return void
283
     */
284
    public function getQueryParamsCollapsedGarbage()
285
    {
286
        $this->assertSame([], Http::getQueryParamsCollapsed('GARBAGE'));
287
    }
288
289
    /**
290
     * Verifies Multi Parameter Method can handle a url with an empty parameter
291
     *
292
     * @test
293
     * @covers ::getQueryParamsCollapsed
294
     *
295
     * @return void
296
     */
297
    public function getQueryParamsCollapsedEmptyParameter()
298
    {
299
        $url = 'http://foo.com/bar/?stuff=yeah&moreStuff=&moreStuff=jazz&otherStuff';
300
        $expected = ['stuff' => 'yeah', 'moreStuff' => ['', 'jazz'], 'otherStuff' => ''];
301
        $this->assertSame($expected, Http::getQueryParamsCollapsed($url, ['moreStuff']));
302
    }
303
}
304