You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
flight-core/tests/RequestTest.php

139 lines
4.1 KiB

12 years ago
<?php
/**
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license MIT, http://flightphp.com/license
12 years ago
*/
use flight\net\Request;
require_once 'vendor/autoload.php';
require_once __DIR__ . '/../flight/autoload.php';
12 years ago
class RequestTest extends PHPUnit\Framework\TestCase
12 years ago
{
private Request $request;
12 years ago
protected function setUp(): void
{
$_SERVER['REQUEST_URI'] = '/';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
$_SERVER['REMOTE_ADDR'] = '8.8.8.8';
12 years ago
$_SERVER['HTTP_X_FORWARDED_FOR'] = '32.32.32.32';
$_SERVER['HTTP_HOST'] = 'example.com';
12 years ago
$this->request = new Request();
12 years ago
}
public function testDefaults()
{
self::assertEquals('/', $this->request->url);
self::assertEquals('/', $this->request->base);
self::assertEquals('GET', $this->request->method);
self::assertEquals('', $this->request->referrer);
self::assertTrue($this->request->ajax);
self::assertEquals('http', $this->request->scheme);
self::assertEquals('', $this->request->type);
self::assertEquals(0, $this->request->length);
self::assertFalse($this->request->secure);
self::assertEquals('', $this->request->accept);
self::assertEquals('example.com', $this->request->host);
12 years ago
}
public function testIpAddress()
{
self::assertEquals('8.8.8.8', $this->request->ip);
self::assertEquals('32.32.32.32', $this->request->proxy_ip);
12 years ago
}
public function testSubdirectory()
{
$_SERVER['SCRIPT_NAME'] = '/subdir/index.php';
12 years ago
$request = new Request();
12 years ago
self::assertEquals('/subdir', $request->base);
12 years ago
}
public function testQueryParameters()
{
$_SERVER['REQUEST_URI'] = '/page?id=1&name=bob';
12 years ago
$request = new Request();
12 years ago
self::assertEquals('/page?id=1&name=bob', $request->url);
self::assertEquals(1, $request->query->id);
self::assertEquals('bob', $request->query->name);
12 years ago
}
public function testCollections()
{
$_SERVER['REQUEST_URI'] = '/page?id=1';
12 years ago
$_GET['q'] = 1;
$_POST['q'] = 1;
$_COOKIE['q'] = 1;
$_FILES['q'] = 1;
$request = new Request();
12 years ago
self::assertEquals(1, $request->query->q);
self::assertEquals(1, $request->query->id);
self::assertEquals(1, $request->data->q);
self::assertEquals(1, $request->cookies->q);
self::assertEquals(1, $request->files->q);
12 years ago
}
public function testMethodOverrideWithHeader()
{
$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
$request = new Request();
self::assertEquals('PUT', $request->method);
}
public function testMethodOverrideWithPost()
{
$_REQUEST['_method'] = 'PUT';
$request = new Request();
self::assertEquals('PUT', $request->method);
}
public function testHttps()
{
$_SERVER['HTTPS'] = 'on';
$request = new Request();
self::assertEquals('https', $request->scheme);
$_SERVER['HTTPS'] = 'off';
$request = new Request();
self::assertEquals('http', $request->scheme);
$_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https';
$request = new Request();
self::assertEquals('https', $request->scheme);
$_SERVER['HTTP_X_FORWARDED_PROTO'] = 'http';
$request = new Request();
self::assertEquals('http', $request->scheme);
$_SERVER['HTTP_FRONT_END_HTTPS'] = 'on';
$request = new Request();
self::assertEquals('https', $request->scheme);
$_SERVER['HTTP_FRONT_END_HTTPS'] = 'off';
$request = new Request();
self::assertEquals('http', $request->scheme);
$_SERVER['REQUEST_SCHEME'] = 'https';
$request = new Request();
self::assertEquals('https', $request->scheme);
$_SERVER['REQUEST_SCHEME'] = 'http';
$request = new Request();
self::assertEquals('http', $request->scheme);
}
12 years ago
}