aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/lib/classes/RequestTest.php
blob: dd1adc9bc2897392ba12728849a7f89999eaea64 (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
<?php
/*
 * request_test.php - unit tests for the Request class
 *
 * Copyright (c) 2009  Elmar Ludwig
 *
 * 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 2 of
 * the License, or (at your option) any later version.
 */

/**
 * @backupGlobals enabled
 */
class RequestTest extends \Codeception\Test\Unit
{
    public function setUp(): void
    {
        unset($_SERVER['HTTPS']);
        unset($_SERVER['HTTP_X_FORWARDED_PROTO']);
        unset($_SERVER['REQUEST_URI']);
        unset($_SERVER['SCRIPT_NAME']);
        unset($_SERVER['SERVER_NAME']);
        unset($_SERVER['SERVER_PORT']);
    }

    protected function setScriptName(string $script_name): void
    {
        $_SERVER['SCRIPT_NAME'] = $script_name;
    }

    protected function setRequestUri(string $request_uri): void
    {
        $_SERVER['REQUEST_URI'] = $request_uri;
    }

    protected function setServerNameAndPort(string $name, int $port, bool $ssl = false): void
    {
        $_SERVER['SERVER_NAME'] = $name;
        $_SERVER['SERVER_PORT'] = $port;
        $_SERVER['HTTPS'] = $ssl ? 'on' : 'off';
    }

    /**
     * @covers Request::protocol
     */
    public function testProtocol(): void
    {
        $this->assertEquals('http', Request::protocol());

        $_SERVER['HTTPS'] = 'on';
        $this->assertEquals('https', Request::protocol());

        $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'foo';
        $this->assertEquals('foo', Request::protocol());
    }

    /**
     * @covers Request::server
     */
    public function testServer(): void
    {
        // Usual ports for http and https
        $this->setServerNameAndPort('www.studip.de', 80);
        $this->assertEquals('www.studip.de', Request::server());

        $this->setServerNameAndPort('www.studip.de', 443, true);
        $this->assertEquals('www.studip.de', Request::server());

        // Unusual ports for http and https
        $this->setServerNameAndPort('www.studip.de', 80, true);
        $this->assertEquals('www.studip.de:80', Request::server());

        $this->setServerNameAndPort('www.studip.de', 443, false);
        $this->assertEquals('www.studip.de:443', Request::server());

        // Other tests
        $this->setServerNameAndPort('www.studip.de', 8088);
        $this->assertEquals('www.studip.de:8088', Request::server());

        $this->setServerNameAndPort('www.studip.de', 8088, true);
        $this->assertEquals('www.studip.de:8088', Request::server());
    }

    /**
     * @covers Request::path()
     */
    public function testPath(): void
    {
        $this->setRequestUri('/foo');
        $this->assertEquals('/foo', Request::path());
    }

    /**
     * @depends testProtocol
     * @depends testServer
     * @depends testPath
     * @covers Request::url
     */
    public function testURL(): void
    {
        $this->setServerNameAndPort('www.example.com', 443, true);
        $this->setRequestUri('/do/it?now=1');
        $this->assertEquals('https://www.example.com/do/it?now=1', Request::url());

        $this->setServerNameAndPort('www.example.com', 8080);
        $this->setRequestUri('/index.php');
        $this->assertEquals('http://www.example.com:8080/index.php', Request::url());
    }

    public function testScriptName(): void
    {
        $this->setScriptName('/index.php');
        $this->assertEquals('/index.php', Request::scriptName());
    }

    /**
     * @depends testPath
     * @depends testScriptName
     * @covers       Request::pathInfo
     * @dataProvider PathProvider
     */
    public function testPathInfo(string $request_uri, string $script_name, string $expected): void
    {
        $this->setScriptName($script_name);
        $this->setRequestUri($request_uri);
        $this->assertEquals($expected, Request::pathInfo());
    }

    /**
     * Data provider for testGetCompletePathInfo
     *
     * @return array[]
     * @see RequestTest::testPathInfo
     */
    public function PathProvider(): array
    {
        return [
            'Regular'                => ['/studip/dispatch.php/start', '/studip/dispatch.php', '/start'],
            'With duplicate slash'   => ['/plugins.php/foo/bar//42', '/plugins.php', '/foo/bar//42'],
            'With duplicate slashes' => ['/bogus.php/1/2//4///7///', '/bogus.php', '/1/2//4///7///'],
            'Encoded'                => ['/dispatch.php/%62lu%62%62er', '/dispatch.php', '/blubber'],
        ];
    }
}