Code Duplication    Length = 21-22 lines in 5 locations

tests/WhipTest.php 5 locations

@@ 175-195 (lines=21) @@
172
     * Tests that we accept proxy method based on a whitelisted IP using the
173
     * dashed range notation.
174
     */
175
    public function testValidWhitelistedProxyMethodWithDashNotation()
176
    {
177
        $lookup = new Whip(
178
            Whip::PROXY_HEADERS,
179
            array(
180
                Whip::PROXY_HEADERS => array(
181
                    IpWhitelist::IPV4 => array(
182
                        '127.0.0.0-127.0.255.255',
183
                    ),
184
                    IpWhitelist::IPV6 => array(
185
                        '::1'
186
                    )
187
                )
188
            ),
189
            array(
190
                'REMOTE_ADDR' => '127.0.0.1',
191
                'HTTP_X_FORWARDED_FOR' => '32.32.32.32'
192
            )
193
        );
194
        $this->assertEquals('32.32.32.32', $lookup->getIpAddress());
195
    }
196
197
    /**
198
     * Tests that we accept proxy method based on a whitelisted IP using the
@@ 201-221 (lines=21) @@
198
     * Tests that we accept proxy method based on a whitelisted IP using the
199
     * wildcard asterix notation.
200
     */
201
    public function testValidWhitelistedProxyMethodWithWildcardNotation()
202
    {
203
        $lookup = new Whip(
204
            Whip::PROXY_HEADERS,
205
            array(
206
                Whip::PROXY_HEADERS => array(
207
                    IpWhitelist::IPV4 => array(
208
                        '127.0.*'
209
                    ),
210
                    IpWhitelist::IPV6 => array(
211
                        '::1'
212
                    )
213
                )
214
            ),
215
            array(
216
                'REMOTE_ADDR' => '127.0.0.1',
217
                'HTTP_X_FORWARDED_FOR' => '32.32.32.32'
218
            )
219
        );
220
        $this->assertEquals('32.32.32.32', $lookup->getIpAddress());
221
    }
222
223
    /**
224
     * Tests that we accept proxy method based on a whitelisted IP using the
@@ 227-247 (lines=21) @@
224
     * Tests that we accept proxy method based on a whitelisted IP using the
225
     * CIDR address notation.
226
     */
227
    public function testValidWhitelistedProxyMethodWithCIDRdNotation()
228
    {
229
        $lookup = new Whip(
230
            Whip::PROXY_HEADERS,
231
            array(
232
                Whip::PROXY_HEADERS => array(
233
                    IpWhitelist::IPV4 => array(
234
                        '127.0.0.0/24'
235
                    ),
236
                    IpWhitelist::IPV6 => array(
237
                        '::1'
238
                    )
239
                )
240
            ),
241
            array(
242
                'REMOTE_ADDR' => '127.0.0.1',
243
                'HTTP_X_FORWARDED_FOR' => '32.32.32.32'
244
            )
245
        );
246
        $this->assertEquals('32.32.32.32', $lookup->getIpAddress());
247
    }
248
249
    /**
250
     * Tests that we get false if there is a valid IP in a proxy header but
@@ 253-273 (lines=21) @@
250
     * Tests that we get false if there is a valid IP in a proxy header but
251
     * we reject it due to REMOTE_ADDR not being in the whitelist.
252
     */
253
    public function testValidIpRejectedDueToWhitelist()
254
    {
255
        $lookup = new Whip(
256
            Whip::PROXY_HEADERS,
257
            array(
258
                Whip::PROXY_HEADERS => array(
259
                    IpWhitelist::IPV4 => array(
260
                        '127.0.0.1/24'
261
                    ),
262
                    IpWhitelist::IPV6 => array(
263
                        '::1'
264
                    )
265
                )
266
            ),
267
            array(
268
                'REMOTE_ADDR' => '24.24.24.24',
269
                'HTTP_X_FORWARDED_FOR' => '32.32.32.32'
270
            )
271
        );
272
        $this->assertFalse($lookup->getIpAddress());
273
    }
274
275
    /**
276
     * Tests that we reject a proxy listed IPv6 address that does not fall within
@@ 370-391 (lines=22) @@
367
    /**
368
     * Test a custom header with a whitelisted IP.
369
     */
370
    public function testCustomHeader()
371
    {
372
        $lookup = new Whip(
373
            Whip::CUSTOM_HEADERS | Whip::REMOTE_ADDR,
374
            array(
375
                Whip::CUSTOM_HEADERS => array(
376
                    IpWhitelist::IPV4 => array(
377
                        '127.0.0.1',
378
                        '::1'
379
                    )
380
                )
381
            ),
382
            array(
383
                'REMOTE_ADDR' => '127.0.0.1',
384
                'HTTP_CUSTOM_SECRET_HEADER' => '32.32.32.32'
385
            )
386
        );
387
        $this->assertEquals(
388
            '32.32.32.32',
389
            $lookup->addCustomHeader('HTTP_CUSTOM_SECRET_HEADER')->getIpAddress()
390
        );
391
    }
392
393
    /**
394
     * Test HTTP_X_REAL_IP header.