Completed
Pull Request — master (#2)
by
unknown
01:16
created

HttpTest::parseHeadersMalformed()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 3
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
     * @test
102
     * @covers ::buildQueryString
103
     *
104
     * @return void
105
     */
106
    public function buildQueryStringBasicUse()
107
    {
108
        $data = [
109
            'foo' => 'bar',
110
            'baz' => 'boom',
111
            'cow' => 'milk',
112
            'php' => 'hypertext processor',
113
            'theFalse' => false,
114
            'theTrue' => true
115
        ];
116
117
        $this->assertSame(
118
            'foo=bar&baz=boom&cow=milk&php=hypertext%20processor&theFalse=false&theTrue=true',
119
            Http::buildQueryString($data)
120
        );
121
    }
122
123
    /**
124
     * @test
125
     * @covers ::buildQueryString
126
     *
127
     * @return void
128
     */
129
    public function buildQueryStringMultiValue()
130
    {
131
        $data = ['param1' => ['value', 'another value'], 'param2' => 'a value'];
132
133
        $this->assertSame('param1=value&param1=another%20value&param2=a%20value', Http::buildQueryString($data));
134
    }
135
136
    /**
137
     * @test
138
     * @covers ::buildQueryString
139
     *
140
     * @return void
141
     */
142
    public function buildQueryStringComplexValues()
143
    {
144
        $this->assertSame(
145
            'a%20b%20c=1%242%283&a%20b%20c=4%295%2A6',
146
            Http::buildQueryString(['a b c' => ['1$2(3', '4)5*6']])
147
        );
148
    }
149
150
    /**
151
     * Verifies Multi Parameter Method can handle a normal url
152
     *
153
     * @test
154
     * @group unit
155
     * @covers ::getQueryParams
156
     *
157
     * @return void
158
     */
159
    public function getQueryParamsNormal()
160
    {
161
        $url = 'http://foo.com/bar/?otherStuff=green&stuff=yeah&moreStuff=rock&moreStuff=jazz&otherStuff=blue&'
162
             . 'otherStuff=black';
163
        $expected = [
164
            'otherStuff' => ['green', 'blue', 'black'],
165
            'stuff' => ['yeah'],
166
            'moreStuff' => ['rock', 'jazz'],
167
        ];
168
        $result = Http::getQueryParams($url);
169
        $this->assertSame($expected, $result);
170
    }
171
172
    /**
173
     * Verifies Multi Parameter Method can handle a url with an empty parameter
174
     *
175
     * @test
176
     * @group unit
177
     * @covers ::getQueryParams
178
     *
179
     * @return void
180
     */
181
    public function getQueryParamsEmptyParameter()
182
    {
183
        $url = 'http://foo.com/bar/?stuff=yeah&moreStuff=&moreStuff=jazz&otherStuff';
184
        $expected = [
185
            'stuff' => ['yeah'],
186
            'moreStuff' => ['', 'jazz'],
187
            'otherStuff' => [''],
188
        ];
189
        $result = Http::getQueryParams($url);
190
        $this->assertSame($expected, $result);
191
    }
192
193
    /**
194
     * Verifies multi parameter method with a garbage query string
195
     *
196
     * @test
197
     * @group unit
198
     * @covers ::getQueryParams
199
     *
200
     * @return void
201
     */
202
    public function getQueryParamsGarbage()
203
    {
204
        $this->assertSame([], Http::getQueryParams('GARBAGE'));
205
    }
206
207
    /**
208
     * @test
209
     * @covers ::getQueryParams
210
     *
211
     * @return void
212
     */
213
    public function getQueryParamsWithCollapsed()
214
    {
215
        $result = Http::getQueryParams('http://foo.com/bar/?stuff=yeah&moreStuff=mhmm', ['stuff', 'notThere']);
216
        $this->assertSame(['stuff' => 'yeah', 'moreStuff' => ['mhmm']], $result);
217
    }
218
219
    /**
220
     * @test
221
     * @covers ::getQueryParams
222
     * @expectedException \Exception
223
     * @expectedExceptionMessage Parameter 'stuff' had more than one value but in $collapsedParams
224
     *
225
     * @return void
226
     */
227
    public function getQueryParamsCollapsedMoreThanOneValue()
228
    {
229
        Http::getQueryParams('http://foo.com/bar/?stuff=yeah&stuff=boy&moreStuff=mhmm', ['stuff']);
230
    }
231
232
    /**
233
     * @test
234
     * @covers ::getQueryParamsCollapsed
235
     *
236
     * @return void
237
     */
238
    public function getQueryParamsCollapsed()
239
    {
240
        $url = 'http://foo.com/bar/?boo=1&foo=bar&boo=2';
241
        $actual = Http::getQueryParamsCollapsed($url, ['boo']);
242
        $this->assertSame(['boo' => ['1', '2'], 'foo' => 'bar'], $actual);
243
    }
244
245
    /**
246
     * @test
247
     * @covers ::getQueryParamsCollapsed
248
     * @expectedException \Exception
249
     * @expectedExceptionMessage Parameter 'boo' is not expected to be an array, but array given
250
     *
251
     * @return void
252
     */
253
    public function getQueryParamsCollapsedUnexpectedArray()
254
    {
255
        $url = 'http://foo.com/bar/?boo=1&foo=bar&boo=2';
256
        Http::getQueryParamsCollapsed($url);
257
    }
258
259
    /**
260
     * Verifies multi parameter method with a garbage query string
261
     *
262
     * @test
263
     * @covers ::getQueryParamsCollapsed
264
     *
265
     * @return void
266
     */
267
    public function getQueryParamsCollapsedGarbage()
268
    {
269
        $this->assertSame([], Http::getQueryParamsCollapsed('GARBAGE'));
270
    }
271
272
    /**
273
     * Verifies Multi Parameter Method can handle a url with an empty parameter
274
     *
275
     * @test
276
     * @covers ::getQueryParamsCollapsed
277
     *
278
     * @return void
279
     */
280
    public function getQueryParamsCollapsedEmptyParameter()
281
    {
282
        $url = 'http://foo.com/bar/?stuff=yeah&moreStuff=&moreStuff=jazz&otherStuff';
283
        $expected = ['stuff' => 'yeah', 'moreStuff' => ['', 'jazz'], 'otherStuff' => ''];
284
        $this->assertSame($expected, Http::getQueryParamsCollapsed($url, ['moreStuff']));
285
    }
286
}
287