added helpers to req/resp and cleaned up phpcs/stan errors

pull/545/head
n0nag0n 11 months ago
parent a60a31c4bd
commit 4d4c0d5420

@ -66,7 +66,6 @@ use flight\net\Route;
*/
class Engine
{
/**
* @var array<string> List of methods that can be extended in the Engine class.
*/

@ -331,6 +331,49 @@ class Request
return $headers;
}
/**
* Alias of Request->getHeader(). Gets a single header.
*
* @param string $header Header name. Can be caps, lowercase, or mixed.
* @param string $default Default value if the header does not exist
*
* @return string
*/
public static function header(string $header, $default = '')
{
return self::getHeader($header, $default);
}
/**
* Alias of Request->getHeaders(). Gets all the request headers
*
* @return array<string, string|int>
*/
public static function headers(): array
{
return self::getHeaders();
}
/**
* Gets the full request URL.
*
* @return string URL
*/
public function getFullUrl(): string
{
return $this->scheme . '://' . $this->host . $this->url;
}
/**
* Grabs the scheme and host. Does not end with a /
*
* @return string
*/
public function getBaseUrl(): string
{
return $this->scheme . '://' . $this->host;
}
/**
* Parse query parameters from a URL.
*

@ -173,6 +173,34 @@ class Response
return $this;
}
/**
* Gets a single header from the response.
*
* @param string $name the name of the header
*
* @return string|null
*/
public function getHeader(string $name): ?string
{
$headers = $this->headers;
// lowercase all the header keys
$headers = array_change_key_case($headers, CASE_LOWER);
return $headers[strtolower($name)] ?? null;
}
/**
* Alias of Response->header(). Adds a header to the response.
*
* @param array<string, int|string>|string $name Header name or array of names and values
* @param ?string $value Header value
*
* @return $this
*/
public function setHeader($name, ?string $value): self
{
return $this->header($name, $value);
}
/**
* Returns the headers from the response.
*
@ -183,6 +211,16 @@ class Response
return $this->headers;
}
/**
* Alias for Response->headers(). Returns the headers from the response.
*
* @return array<string, int|string|array<int, string>>
*/
public function getHeaders(): array
{
return $this->headers();
}
/**
* Writes content to the response body.
*

@ -243,7 +243,8 @@ class FlightTest extends TestCase
$this->assertEquals('/user/all_users/check_user/check_one/normalpath', $url);
}
public function testHookOutputBuffering() {
public function testHookOutputBuffering()
{
Flight::route('/test', function () {
echo 'test';
});
@ -259,7 +260,8 @@ class FlightTest extends TestCase
$this->assertEquals('test', Flight::response()->getBody());
}
public function testHookOutputBufferingV2OutputBuffering() {
public function testHookOutputBufferingV2OutputBuffering()
{
Flight::route('/test', function () {
echo 'test';
});

@ -205,10 +205,10 @@ class RequestTest extends TestCase
// or the headers that are already in $_SERVER
$this->assertEquals('XMLHttpRequest', $request->getHeader('X-REqUesTed-WiTH'));
$this->assertEquals('32.32.32.32', $request->getHeader('X-Forwarded-For'));
$this->assertEquals('32.32.32.32', $request->header('X-Forwarded-For'));
// default values
$this->assertEquals('default value', $request->getHeader('X-Non-Existent-Header', 'default value'));
$this->assertEquals('default value', $request->header('X-Non-Existent-Header', 'default value'));
}
public function testGetHeaders()
@ -231,7 +231,7 @@ class RequestTest extends TestCase
$_SERVER = [];
$_SERVER['HTTP_X_CUSTOM_HEADER'] = '';
$request = new Request();
$this->assertEquals(['X-Custom-Header' => ''], $request->getHeaders());
$this->assertEquals(['X-Custom-Header' => ''], $request->headers());
}
public function testGetHeadersWithMultipleHeaders()
@ -245,4 +245,38 @@ class RequestTest extends TestCase
'X-Custom-Header2' => 'custom header value 2'
], $request->getHeaders());
}
public function testGetFullUrlNoHttps()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/page?id=1';
$request = new Request();
$this->assertEquals('http://example.com/page?id=1', $request->getFullUrl());
}
public function testGetFullUrlWithHttps()
{
$_SERVER['HTTP_HOST'] = 'localhost:8000';
$_SERVER['REQUEST_URI'] = '/page?id=1';
$_SERVER['HTTPS'] = 'on';
$request = new Request();
$this->assertEquals('https://localhost:8000/page?id=1', $request->getFullUrl());
}
public function testGetBaseUrlNoHttps()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/page?id=1';
$request = new Request();
$this->assertEquals('http://example.com', $request->getBaseUrl());
}
public function testGetBaseUrlWithHttps()
{
$_SERVER['HTTP_HOST'] = 'localhost:8000';
$_SERVER['REQUEST_URI'] = '/page?id=1';
$_SERVER['HTTPS'] = 'on';
$request = new Request();
$this->assertEquals('https://localhost:8000', $request->getBaseUrl());
}
}

@ -65,7 +65,7 @@ class ResponseTest extends TestCase
$response = new Response();
$response->header('content-type', 'text/html');
$response->header('x-test', 'test');
$this->assertEquals(['content-type' => 'text/html', 'x-test' => 'test'], $response->headers());
$this->assertEquals(['content-type' => 'text/html', 'x-test' => 'test'], $response->getHeaders());
}
public function testHeaderArray()
@ -81,6 +81,13 @@ class ResponseTest extends TestCase
$this->assertEquals($response, $response->header('Content-Type', 'text/html'));
}
public function testGetHeaderCrazyCase()
{
$response = new Response();
$response->setHeader('CoNtEnT-tYpE', 'text/html');
$this->assertEquals('text/html', $response->getHeader('content-type'));
}
public function testWrite()
{
$response = new Response();
@ -104,6 +111,9 @@ class ResponseTest extends TestCase
public function testClear()
{
$response = new Response();
// Should clear this echo out
echo 'hi';
$response->write('test');
$response->status(404);
$response->header('Content-Type', 'text/html');
@ -111,6 +121,7 @@ class ResponseTest extends TestCase
$this->assertEquals('', $response->getBody());
$this->assertEquals(200, $response->status());
$this->assertEquals([], $response->headers());
$this->assertEquals(0, ob_get_length());
}
public function testCacheSimple()
@ -219,8 +230,8 @@ class ResponseTest extends TestCase
return $this;
}
};
$response->header('Content-Type', 'text/html');
$response->header('X-Test', 'test');
$response->setHeader('Content-Type', 'text/html');
$response->setHeader('X-Test', 'test');
$response->write('Something');
$this->expectOutputString('Something');

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/*
* This is the test file where we can open up a quick test server and make
* sure that the UI is really working the way we would expect it to.
@ -71,14 +73,16 @@ Flight::group('/group', function(){
Flight::route('/alias', function () {
echo '<span id="infotext">Route text:</span> Alias route works!';
}, false, 'aliasroute');
class authCheck {
public function before(){
class AuthCheck
{
public function before()
{
if (!isset($_COOKIE['user'])) {
echo '<span id="infotext">Middleware text:</span> You are not authorized to access this route!';
}
}
}
$middle = new authCheck();
$middle = new AuthCheck();
// Test 6: Route with middleware
Flight::route('/protected', function () {
echo '<span id="infotext">Route text:</span> Protected route works!';
@ -86,7 +90,7 @@ Flight::route('/protected', function(){
// Test 7: Route with template
Flight::route('/template/@name', function ($name) {
Flight::render('template.php', ['name' => $name]);
Flight::render('template.phtml', ['name' => $name]);
});
Flight::set('flight.views.path', './');
Flight::map('error', function (Throwable $error) {
@ -172,5 +176,3 @@ Flight::after('start', function ($params) {
echo "</div>";
});
Flight::start();

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
class AuthCheck
{
/**
* Before
*
* @return void
*/
public function before()
{
if (!isset($_COOKIE['user'])) {
echo '<span id="infotext">Middleware text:</span> You are not authorized to access this route!';
}
}
}

@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
class LayoutMiddleware
{
/**
* Before
*
* @return void
*/
public function before()
{
$final_route = Flight::getUrl('final_group');
echo <<<HTML
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
#infotext {
font-weight: bold;
color: blueviolet;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
#container {
color: #333;
font-size: 16px;
line-height: 1.5;
margin: 20px 0;
padding: 10px;
border: 1px solid #ddd;
background-color: #f9f9f9;
}
#debugrequest {
color: #333;
font-size: 16px;
line-height: 1.5;
margin: 20px 0;
padding: 10px;
border: 1px solid #ddd;
background-color: #f9f9f9;
}
</style>
<ul>
<li><a href="/">Root Route</a></li>
<li><a href="/test">Test Route</a></li>
<li><a href="/user/John">User Route with Parameter (John)</a></li>
<li><a href="/group/test">Grouped Test Route</a></li>
<li><a href="/group/user/Jane">Grouped User Route with Parameter (Jane)</a></li>
<li><a href="/alias">Alias Route</a></li>
<li><a href="/protected">Protected path</a></li>
<li><a href="/template/templatevariable">Template path</a></li>
<li><a href="/querytestpath?test=1&variable2=uuid&variable3=tester">Query path</a></li>
<li><a href="/postpage">404 Not Found</a></li>
<li><a href="{$final_route}">Mega group</a></li>
<li><a href="/error">Error</a></li>
</ul>
HTML;
echo '<div id="container">';
}
public function after()
{
echo '</div>';
echo '<div id="debugrequest">';
echo "<h2>Request Information</h2><pre>";
print_r(Flight::request());
echo '<h3>Raw Request Information</h3>';
print_r($_SERVER);
echo "</pre><h2>Response Information</h2><pre>";
print_r(Flight::response());
echo "</pre>";
echo "</div>";
}
}

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/*
* This is the test file where we can open up a quick test server and make
* sure that the UI is really working the way we would expect it to.
@ -13,87 +15,7 @@ Flight::set('flight.content_length', false);
Flight::set('flight.views.path', './');
//Flight::set('flight.v2.output_buffering', true);
class LayoutMiddleware {
public function before() {
$final_route = Flight::getUrl('final_group');
echo <<<HTML
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
#infotext {
font-weight: bold;
color: blueviolet;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
#container {
color: #333;
font-size: 16px;
line-height: 1.5;
margin: 20px 0;
padding: 10px;
border: 1px solid #ddd;
background-color: #f9f9f9;
}
#debugrequest {
color: #333;
font-size: 16px;
line-height: 1.5;
margin: 20px 0;
padding: 10px;
border: 1px solid #ddd;
background-color: #f9f9f9;
}
</style>
<ul>
<li><a href="/">Root Route</a></li>
<li><a href="/test">Test Route</a></li>
<li><a href="/user/John">User Route with Parameter (John)</a></li>
<li><a href="/group/test">Grouped Test Route</a></li>
<li><a href="/group/user/Jane">Grouped User Route with Parameter (Jane)</a></li>
<li><a href="/alias">Alias Route</a></li>
<li><a href="/protected">Protected path</a></li>
<li><a href="/template/templatevariable">Template path</a></li>
<li><a href="/querytestpath?test=1&variable2=uuid&variable3=tester">Query path</a></li>
<li><a href="/postpage">404 Not Found</a></li>
<li><a href="{$final_route}">Mega group</a></li>
<li><a href="/error">Error</a></li>
</ul>
HTML;
echo '<div id="container">';
}
public function after() {
echo '</div>';
echo '<div id="debugrequest">';
echo "<h2>Request Information</h2><pre>";
print_r(Flight::request());
echo '<h3>Raw Request Information</h3>';
print_r($_SERVER);
echo "</pre><h2>Response Information</h2><pre>";
print_r(Flight::response());
echo "</pre>";
echo "</div>";
}
}
require_once 'LayoutMiddleware.php';
Flight::group('', function () {
@ -155,15 +77,9 @@ Flight::group('', function() {
echo '<span id="infotext">Route text:</span> Alias route works!';
}, false, 'aliasroute');
/** middleware test */
class authCheck {
public function before(){
if(!isset($_COOKIE['user'])){
echo '<span id="infotext">Middleware text:</span> You are not authorized to access this route!';
}
}
}
$middle = new authCheck();
/** Middleware test */
include_once 'AuthCheck.php';
$middle = new AuthCheck();
// Test 6: Route with middleware
Flight::route('/protected', function () {
echo '<span id="infotext">Route text:</span> Protected route works!';
@ -171,14 +87,14 @@ Flight::group('', function() {
// Test 7: Route with template
Flight::route('/template/@name', function ($name) {
Flight::render('template.php', ['name' => $name]);
Flight::render('template.phtml', ['name' => $name]);
});
// Test 8: Throw an error
Flight::route('/error', function () {
trigger_error('This is a successful error');
});
}, [ new LayoutMiddleware ]);
}, [ new LayoutMiddleware() ]);
Flight::map('error', function (Throwable $e) {
echo sprintf(

Loading…
Cancel
Save