summaryrefslogtreecommitdiffstats
path: root/tests/www/IndexTest.php
blob: aa7df8bb5d68a1b36e6acdf0a0eb05382c6ed4cb (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
<?php
/**
 * Simple test for the www/index.php script.
 *
 * This test is intended mostly as a demonstration of how to test the public web interface in SimpleSAMLphp.
 *
 * @author Jaime Pérez Crespo <jaime.perez@uninett.no>
 * @package SimpleSAMLphp
 */

namespace SimpleSAML\Test\Web;

include(dirname(__FILE__).'/../BuiltInServer.php');

use \SimpleSAML\Test\BuiltInServer;

class IndexTest extends \PHPUnit_Framework_TestCase
{

    /**
     * @var \SimpleSAML\Test\BuiltInServer
     */
    protected $server;

    /**
     * @var string
     */
    protected $server_addr;

    /**
     * @var int
     */
    protected $server_pid;

    /**
     * @var string
     */
    protected $shared_file;


    /**
     * The setup method that is run before any tests in this class.
     */
    protected function setup()
    {
        $this->server = new BuiltInServer('configLoader');
        $this->server_addr = $this->server->start();
        $this->server_pid = $this->server->getPid();

        $this->shared_file = sys_get_temp_dir().'/'.$this->server_pid.'.lock';
        @unlink($this->shared_file); // remove it if it exists
    }


    protected function updateConfig($config)
    {
        @unlink($this->shared_file);
        $config = "<?php\n\$config = ".var_export($config, true).";\n";
        file_put_contents($this->shared_file, $config);
    }


    /**
     * A simple test to make sure the index.php file redirects appropriately to the right URL.
     */
    public function testRedirection()
    {
        if (defined('HHVM_VERSION')) {
            // can't test this in HHVM for the moment
            $this->markTestSkipped('The web-based tests cannot be run in HHVM for the moment.');
        }

        if (version_compare(phpversion(), '5.4') === -1) {
            // no built-in server prior to 5.4
            $this->markTestSkipped('The web-based tests cannot be run in PHP versions older than 5.4.');
        }

        // test most basic redirection
        $this->updateConfig(array(
                'baseurlpath' => 'http://example.org/simplesaml/'
        ));
        $resp = $this->server->get('/index.php', array(), array(
            CURLOPT_FOLLOWLOCATION => 0,
        ));
        $this->assertEquals('302', $resp['code']);
        $this->assertEquals(
            'http://example.org/simplesaml/module.php/core/frontpage_welcome.php',
            $resp['headers']['Location']
        );

        // test non-default path and https
        $this->updateConfig(array(
            'baseurlpath' => 'https://example.org/'
        ));
        $resp = $this->server->get('/index.php', array(), array(
            CURLOPT_FOLLOWLOCATION => 0,
        ));
        $this->assertEquals('302', $resp['code']);
        $this->assertEquals(
            'https://example.org/module.php/core/frontpage_welcome.php',
            $resp['headers']['Location']
        );

        // test URL guessing
        $this->updateConfig(array(
            'baseurlpath' => '/simplesaml/'
        ));
        $resp = $this->server->get('/index.php', array(), array(
            CURLOPT_FOLLOWLOCATION => 0,
        ));
        $this->assertEquals('302', $resp['code']);
        $this->assertEquals(
            'http://'.$this->server_addr.'/simplesaml/module.php/core/frontpage_welcome.php',
            $resp['headers']['Location']
        );
    }


    /**
     * The tear down method that is executed after all tests in this class.
     */
    protected function tearDown()
    {
        unlink($this->shared_file);
        $this->server->stop();
    }
}