Skip to content

Commit

Permalink
Add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdotcom committed Feb 1, 2025
1 parent 44d0d23 commit 5f40c2f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
13 changes: 13 additions & 0 deletions tests/CurlHttpMessage/CurlReqResTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ public function testExecThrowsException()
}
*/

public function testEncoding()
{
$request = $this->factory->request('GET', $this->baseUrl . '/echo?poop', array(
'Accept-Encoding' => 'gzip',
));
$curlReqRes = $this->factory->curlReqRes($request);
$response = $curlReqRes->exec();

self::assertSame('gzip', $response->getHeaderLine('x-content-encoding'));
$data = \json_decode((string) $response->getBody(), true);
self::assertIsArray($data);
}

public function testExec()
{
$request = $this->factory->request('GET', $this->baseUrl . '/echo');
Expand Down
1 change: 1 addition & 0 deletions tests/CurlHttpMessage/Handler/CurlMultiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/**
* @covers bdk\CurlHttpMessage\Handler\CurlMulti
* @covers bdk\CurlHttpMessage\CurlReqRes
*/
class CurlMultiTest extends TestCase
{
Expand Down
8 changes: 4 additions & 4 deletions tests/Debug/Collector/OAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testGetAccessToken()
'args' => array(
'additional info',
array(
'size_download' => 63.0,
// 'size_download' => 63.0,
'sbs' => 'POST&http%3A%2F%2F127.0.0.1%3A8080%2Foauth%2Faccess_token&oauth_consumer_key%3Dkey%26oauth_nonce%3D%s%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D%s%26oauth_version%3D1.0',
),
),
Expand Down Expand Up @@ -231,7 +231,7 @@ public function testGetRequestToken()
'args' => array(
'additional info',
array(
'size_download' => 65.0,
// 'size_download' => 65.0,
'sbs' => 'GET&http%3A%2F%2F127.0.0.1%3A8080%2Foauth%2Frequest_token&oauth_callback%3Dhttp%253A%252F%252Fwww.bradkent.com%252F%26oauth_consumer_key%3Dkey%26oauth_nonce%3D%s%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D%s%26oauth_version%3D1.0',
),
),
Expand Down Expand Up @@ -321,7 +321,7 @@ public function testFetch()
$sizeDownload = null;
foreach ($logEntriesActual as $i => $logEntry) {
if (isset($logEntry['args'][0]) && $logEntry['args'][0] === 'additional info') {
$sizeDownload = $logEntry['args'][1]['size_download'];
// $sizeDownload = $logEntry['args'][1]['size_download'];
$logEntriesActual[$i]['args'][1] = \array_intersect_key($logEntry['args'][1], \array_flip(['size_download', 'size_upload', 'sbs']));
} elseif (isset($logEntry['args'][0]) && $logEntry['args'][0] === 'response body') {
$logEntriesActual[$i]['args'][1] = $logEntry['args'][1]['value'];
Expand Down Expand Up @@ -371,7 +371,7 @@ public function testFetch()
'args' => array(
'additional info',
array(
'size_download' => $sizeDownload,
// 'size_download' => $sizeDownload,
'size_upload' => 7.0,
'sbs' => 'POST&http%3A%2F%2F127.0.0.1%3A8080%2Fecho&foo%3Dbar%26oauth_consumer_key%3Dkey%26oauth_nonce%3D%s%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3%s%26oauth_version%3D1.0',
),
Expand Down
17 changes: 16 additions & 1 deletion tests/Debug/Plugin/Method/TraceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/**
* PHPUnit tests for Debug::trace() method
*
* @covers \bdk\Debug\Dump\Html
* @covers \bdk\Debug\Dump\Html\Helper
* @covers \bdk\Debug\Dump\Html\HtmlString
* @covers \bdk\Debug\Dump\Html\Table
Expand Down Expand Up @@ -369,5 +370,19 @@ public function testTraceNoFunction()
);
}


// this test needs improvement
public function testInclInternal()
{
$this->testMethod(
'trace',
[
\bdk\Debug::meta('inclInternal')
],
array(
'entry' => static function (LogEntry $logEntry) {
self::assertSame('bdk\Debug->__call(\'trace\')', $logEntry['args'][0][0]['function']);
},
)
);
}
}
28 changes: 27 additions & 1 deletion tests/docroot/frontController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use \bdk\HttpMessage\ServerRequest;

/**
* php -S 127.0.0.1:8080 frontController.php
*
Expand All @@ -12,7 +14,7 @@
\define('STDERR', \fopen('php://stderr', 'wb'));
}

$serverRequest = \bdk\HttpMessage\ServerRequest::fromGlobals();
$serverRequest = ServerRequest::fromGlobals();
$serverParams = $serverRequest->getServerParams();
$requestUri = $serverRequest->getUri();

Expand Down Expand Up @@ -63,7 +65,11 @@
return;
}
if (\strtolower(\substr($realpath, -4)) === '.php') {
\ob_start();
include $realpath;
$body = compressResponse($serverRequest, \ob_get_clean());
header('Content-Length: ' . \strlen($body));
echo $body;
return;
}
// serve from filesystem
Expand All @@ -86,7 +92,11 @@
continue;
}
\header('Content-Type: ' . $contentType);
\ob_start();
include $realpath;
$body = compressResponse($serverRequest, \ob_get_clean());
header('Content-Length: ' . \strlen($body));
echo $body;
return;
}

Expand All @@ -102,3 +112,19 @@ function notFound()
\header('HTTP/1.1 404 Not Found');
echo '<h1>404 Not Found</h1>';
}

function compressResponse(ServerRequest $serverRequest, $responseBody)
{
$acceptEncoding = $serverRequest->getHeaderLine('Accept-Encoding');
$acceptEncodings = \explode(', ', \strtolower($acceptEncoding));
foreach ($acceptEncodings as $encoding) {
if ($encoding === 'deflate') {
\header('Content-Encoding: deflate');
return \gzcompress($responseBody);
} elseif ($encoding === 'gzip') {
\header('Content-Encoding: gzip');
return \gzencode($responseBody);
}
}
return $responseBody;
}

0 comments on commit 5f40c2f

Please sign in to comment.