-
Notifications
You must be signed in to change notification settings - Fork 701
Expand file tree
/
Copy pathIndexTest.php
More file actions
176 lines (152 loc) · 5.03 KB
/
IndexTest.php
File metadata and controls
176 lines (152 loc) · 5.03 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\Web;
use PHPUnit\Framework\TestCase;
use SimpleSAML\TestUtils\BuiltInServer;
/**
* Simple test for the public/index.php script.
*
* This test is intended mostly as a demonstration of how to test the public web interface in SimpleSAMLphp.
*
* @package SimpleSAMLphp
*/
class IndexTest extends TestCase
{
/**
* @var \SimpleSAML\TestUtils\BuiltInServer
*/
protected static BuiltInServer $server;
/**
* @var string
*/
protected static string $server_addr;
/**
* @var int
*/
protected static int $server_pid;
/**
* @var string
*/
protected static string $shared_file;
/**
* The setup method that is run before any tests in this class.
*/
public static function setupBeforeClass(): void
{
self::$server = new BuiltInServer('configLoader');
self::$server_addr = self::$server->start();
self::$server_pid = self::$server->getPid();
self::$shared_file = sys_get_temp_dir() . '/' . self::$server_pid . '.lock';
@unlink(self::$shared_file); // remove it if it exists
}
/**
* @param array $config
*/
protected function updateConfig(array $config): void
{
@unlink(self::$shared_file);
$config = "<?php\n\$config = " . var_export($config, true) . ";\n";
file_put_contents(self::$shared_file, $config);
}
/**
* A simple test to make sure the index.php file redirects appropriately to the right URL.
*/
public function testRedirection(): void
{
// test most basic redirection
$this->updateConfig([
'baseurlpath' => 'http://example.org/simplesaml/',
'cachedir' => sys_get_temp_dir(),
'tempdir' => sys_get_temp_dir(),
'logging.handler' => 'errorlog',
'secretsalt' => 'test-secret',
]);
$resp = self::$server->get('/index.php', [], [
CURLOPT_FOLLOWLOCATION => 0,
]);
$this->assertEquals('303', $resp['code']);
$this->assertEquals(
'http://example.org/simplesaml/module/core/welcome',
$resp['headers']['Location'],
);
// test non-default path and https
$this->updateConfig([
'baseurlpath' => 'https://example.org/',
'cachedir' => sys_get_temp_dir(),
'tempdir' => sys_get_temp_dir(),
'logging.handler' => 'errorlog',
'secretsalt' => 'test-secret',
]);
$resp = self::$server->get('/index.php', [], [
CURLOPT_FOLLOWLOCATION => 0,
]);
$this->assertEquals('303', $resp['code']);
$this->assertEquals(
'https://example.org/module/core/welcome',
$resp['headers']['Location'],
);
// test URL guessing
$this->updateConfig([
'baseurlpath' => '/simplesaml/',
'cachedir' => sys_get_temp_dir(),
'tempdir' => sys_get_temp_dir(),
'logging.handler' => 'errorlog',
'secretsalt' => 'test-secret',
]);
$resp = self::$server->get('/index.php', [], [
CURLOPT_FOLLOWLOCATION => 0,
]);
$this->assertEquals('303', $resp['code']);
$this->assertEquals(
'http://' . self::$server_addr . '/simplesaml/module/core/welcome',
$resp['headers']['Location'],
);
}
/**
* Test the frontpage.redirect config option
*/
public function testRedirectionFrontpageRedirectOption(): void
{
$this->updateConfig([
'frontpage.redirect' => 'https://www.example.edu/',
'cachedir' => sys_get_temp_dir(),
'tempdir' => sys_get_temp_dir(),
'logging.handler' => 'errorlog',
'secretsalt' => 'test-secret',
]);
$resp = self::$server->get('/index.php', [], [
CURLOPT_FOLLOWLOCATION => 0,
]);
$this->assertEquals('303', $resp['code']);
$this->assertEquals(
'https://www.example.edu/',
$resp['headers']['Location'],
);
}
/**
* Test that module URLs are handled by the front controller branch instead of being redirected.
*/
public function testModulePathIsHandledByFrontController(): void
{
$this->updateConfig([
'baseurlpath' => 'http://example.org/simplesaml/',
'cachedir' => sys_get_temp_dir(),
'tempdir' => sys_get_temp_dir(),
'logging.handler' => 'errorlog',
'secretsalt' => 'test-secret',
]);
$resp = self::$server->get('/index.php/module/doesnotexist', [], [
CURLOPT_FOLLOWLOCATION => 0,
]);
$this->assertEquals('404', $resp['code']);
$this->assertArrayNotHasKey('Location', $resp['headers']);
}
/**
* The tear down method that is executed after all tests in this class.
*/
public static function tearDownAfterClass(): void
{
unlink(self::$shared_file);
self::$server->stop();
}
}