SimpleSAML\Test\BuiltInServer::get PHP Method

get() public method

This function performs an HTTP GET request to the built-in server.
public get ( string $query, array $parameters, array $curlopts = [] ) : array | string
$query string The query to perform.
$parameters array An array (can be empty) with parameters for the requested URI.
$curlopts array An array (can be empty) with options for cURL.
return array | string The response obtained from the built-in server.
    public function get($query, $parameters, $curlopts = array())
    {
        $ch = curl_init();
        $url = 'http://' . $this->address . $query;
        $url .= !empty($parameters) ? '?' . http_build_query($parameters) : '';
        curl_setopt_array($ch, array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => 1, CURLOPT_HEADER => 1));
        curl_setopt_array($ch, $curlopts);
        $resp = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        list($header, $body) = explode("\r\n\r\n", $resp, 2);
        $raw_headers = explode("\r\n", $header);
        array_shift($raw_headers);
        $headers = array();
        foreach ($raw_headers as $header) {
            list($name, $value) = explode(':', $header, 2);
            $headers[trim($name)] = trim($value);
        }
        curl_close($ch);
        return array('code' => $code, 'headers' => $headers, 'body' => $body);
    }

Usage Example

 /**
  * 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']);
 }