Added test mode and verbose level
This commit is contained in:
parent
65a5e7b577
commit
4482ac6076
90
hcra.php
90
hcra.php
|
@ -4,7 +4,6 @@ use GuzzleHttp\Psr7;
|
||||||
|
|
||||||
include __DIR__.'/vendor/autoload.php';
|
include __DIR__.'/vendor/autoload.php';
|
||||||
|
|
||||||
$debug = false;
|
|
||||||
$colors = new Wujunze\Colors();
|
$colors = new Wujunze\Colors();
|
||||||
|
|
||||||
if( count($argv) < 2 ){
|
if( count($argv) < 2 ){
|
||||||
|
@ -37,7 +36,7 @@ It does not seems a good JSON file!', 'green').'
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
$debug = $requests['configuration']['debug'];
|
$verbose_level = $requests['configuration']['verbose_level'];
|
||||||
|
|
||||||
// Loop through URLs :-)
|
// Loop through URLs :-)
|
||||||
echo $pointer." Found ".$colors->getColoredString( count($requests['urls']), 'yellow')." requests!\n\n";
|
echo $pointer." Found ".$colors->getColoredString( count($requests['urls']), 'yellow')." requests!\n\n";
|
||||||
|
@ -50,6 +49,9 @@ $s = 1;
|
||||||
// Placeholder for values extracted by regexp
|
// Placeholder for values extracted by regexp
|
||||||
$all_values = [];
|
$all_values = [];
|
||||||
|
|
||||||
|
$tests_run = 0;
|
||||||
|
$tests_ok = 0;
|
||||||
|
|
||||||
foreach ($requests['urls'] as $request) {
|
foreach ($requests['urls'] as $request) {
|
||||||
$new_values = [];
|
$new_values = [];
|
||||||
|
|
||||||
|
@ -66,19 +68,26 @@ foreach ($requests['urls'] as $request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Preview of the request
|
// Preview of the request
|
||||||
echo $colors->getColoredString('[>]', 'red').' '.$colors->getColoredString('REQUEST '.$s.'/'.count($requests), 'yellow').'
|
echo $colors->getColoredString( '['.$s.'/'.count($requests['urls']).'] '.$request['title'], 'yellow')."\n";
|
||||||
|
|
||||||
|
if( $verbose_level == 2 ){
|
||||||
|
echo '
|
||||||
'.$colors->getColoredString('[>]', 'red').' '.$colors->getColoredString('METHOD :', 'cyan').' '.$colors->getColoredString($request['method'], 'yellow').'
|
'.$colors->getColoredString('[>]', 'red').' '.$colors->getColoredString('METHOD :', 'cyan').' '.$colors->getColoredString($request['method'], 'yellow').'
|
||||||
'.$colors->getColoredString('[>]', 'red').' '.$colors->getColoredString('URL :', 'cyan').' '.$colors->getColoredString($request['url'], 'yellow').'
|
'.$colors->getColoredString('[>]', 'red').' '.$colors->getColoredString('URL :', 'cyan').' '.$colors->getColoredString($request['url'], 'yellow').'
|
||||||
'.$colors->getColoredString('[>]', 'red').' '.$colors->getColoredString('HEADERS :', 'cyan').'
|
'.$colors->getColoredString('[>]', 'red').' '.$colors->getColoredString('HEADERS :', 'cyan')."\n";
|
||||||
';
|
|
||||||
|
|
||||||
// Print all headers
|
|
||||||
foreach( $headers as $name => $value ){
|
|
||||||
echo ' '.$name.': '.$value."\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
echo $colors->getColoredString('[>]', 'red').' '.$colors->getColoredString('BODY :', 'cyan').' '.$colors->getColoredString($request['body'], 'yellow').'
|
// Print all headers
|
||||||
';
|
if( $verbose_level == 2 ){
|
||||||
|
foreach( $headers as $name => $value ){
|
||||||
|
echo ' '.$name.': '.$value."\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print request body
|
||||||
|
if( $verbose_level == 2 ){
|
||||||
|
echo $colors->getColoredString('[>]', 'red').' '.$colors->getColoredString('BODY :', 'cyan').' '.$colors->getColoredString($request['body'], 'yellow')."\n";
|
||||||
|
}
|
||||||
|
|
||||||
$guzzle_options = [
|
$guzzle_options = [
|
||||||
'body' => $request['body'],
|
'body' => $request['body'],
|
||||||
|
@ -112,7 +121,7 @@ foreach ($requests['urls'] as $request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if( $debug ){
|
if( $verbose_level == 3 ){
|
||||||
echo $response->getBody();
|
echo $response->getBody();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,21 +137,72 @@ foreach ($requests['urls'] as $request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints out found values with regexp!
|
// Prints out found values with regexp!
|
||||||
if( !empty($request['header-regexp']) || !empty($request['body-regexp']) ){
|
if( $verbose_level == 2 ){
|
||||||
foreach( $new_values as $name => $value ){
|
if( !empty($request['header-regexp']) || !empty($request['body-regexp']) ){
|
||||||
echo $colors->getColoredString('[<]', 'green').' '.$name.': '.$colors->getColoredString($value, 'yellow')."\n";
|
foreach( $new_values as $name => $value ){
|
||||||
|
echo $colors->getColoredString('[<]', 'green').' '.$name.': '.$colors->getColoredString($value, 'yellow')."\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Merge all values found
|
||||||
if( !empty($new_values) ){
|
if( !empty($new_values) ){
|
||||||
$all_values = array_merge( $all_values, $new_values );
|
$all_values = array_merge( $all_values, $new_values );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there is an expected value, check if it corresponds
|
||||||
|
if( !empty($request['header-expected']) ){
|
||||||
|
foreach( $request['header-expected'] as $expected_name => $expected_value ){
|
||||||
|
$tests_run++;
|
||||||
|
|
||||||
|
if( isset($new_values[$expected_name]) ){
|
||||||
|
if( $expected_value != $new_values[$expected_name] ){
|
||||||
|
echo $colors->getColoredString('[X]', 'red').' '.$expected_name.': '.$colors->getColoredString($new_values[$expected_name], 'red')." (should be ".$colors->getColoredString($expected_value, 'yellow').")\n";
|
||||||
|
} else {
|
||||||
|
$tests_ok++;
|
||||||
|
echo $colors->getColoredString('[V]', 'green').' '.$expected_name.': '.$colors->getColoredString($new_values[$expected_name], 'green')."\n";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo $colors->getColoredString('[X]', 'red').' '.$expected_name.': '.$colors->getColoredString('<not found>', 'red')." (should be ".$colors->getColoredString($expected_value, 'yellow').")\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if( !empty($request['body-expected']) ){
|
||||||
|
foreach( $request['body-expected'] as $expected_name => $expected_value ){
|
||||||
|
$tests_run++;
|
||||||
|
|
||||||
|
if( isset($new_values[$expected_name]) ){
|
||||||
|
if( $expected_value != $new_values[$expected_name] ){
|
||||||
|
echo $colors->getColoredString('[X]', 'red').' '.$expected_name.': '.$colors->getColoredString($new_values[$expected_name], 'red')." (should be ".$colors->getColoredString($expected_value, 'yellow').")\n";
|
||||||
|
} else {
|
||||||
|
$tests_ok++;
|
||||||
|
echo $colors->getColoredString('[V]', 'green').' '.$expected_name.': '.$colors->getColoredString($new_values[$expected_name], 'green')."\n";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo $colors->getColoredString('[X]', 'red').' '.$expected_name.': '.$colors->getColoredString('<not found>', 'red')." (should be ".$colors->getColoredString($expected_value, 'yellow').")\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$s++;
|
$s++;
|
||||||
|
|
||||||
echo "\n";
|
echo "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prints the result of the tests
|
||||||
|
if( $tests_run > 0 ){
|
||||||
|
echo "Tests passed:\n";
|
||||||
|
|
||||||
|
if( $tests_ok == $tests_run ){
|
||||||
|
echo $colors->getColoredString('[V] ', 'green');
|
||||||
|
} else {
|
||||||
|
echo $colors->getColoredString('[X] ', 'red');
|
||||||
|
}
|
||||||
|
|
||||||
|
echo $tests_ok.'/'.$tests_run."\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function usage(){
|
function usage(){
|
||||||
|
|
24
params.json
24
params.json
|
@ -1,11 +1,12 @@
|
||||||
{
|
{
|
||||||
"configuration":
|
"configuration":
|
||||||
{
|
{
|
||||||
"debug": false
|
"verbose_level": 1
|
||||||
}
|
}
|
||||||
,
|
,
|
||||||
"urls": [
|
"urls": [
|
||||||
{
|
{
|
||||||
|
"title": "First open to get the cookie",
|
||||||
"url": "http://localhost/mysite/",
|
"url": "http://localhost/mysite/",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"headers": null,
|
"headers": null,
|
||||||
|
@ -18,6 +19,7 @@
|
||||||
"body-regexp": null
|
"body-regexp": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"title": "Login",
|
||||||
"url": "http://localhost/mysite/",
|
"url": "http://localhost/mysite/",
|
||||||
"method": "POST",
|
"method": "POST",
|
||||||
"headers": {
|
"headers": {
|
||||||
|
@ -38,20 +40,36 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"title": "Get the email",
|
||||||
"url": "http://localhost§next_url§",
|
"url": "http://localhost§next_url§",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"headers": {
|
"headers": {
|
||||||
"Cookie": "§cookie§"
|
"Cookie": "§cookie§"
|
||||||
},
|
},
|
||||||
"body": null,
|
"body": null,
|
||||||
"header-regexp": null,
|
"header-regexp": [
|
||||||
|
{
|
||||||
|
"content_type": "/Content-Type: (.+)/",
|
||||||
|
"pragma": "/Pragma: (.+)/"
|
||||||
|
}
|
||||||
|
],
|
||||||
"body-regexp": [
|
"body-regexp": [
|
||||||
{
|
{
|
||||||
"email": "/<p>Your email is (.+)!<\\/p>/"
|
"email": "/<p>Your email is (.+)!<\\/p>/"
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"header-expected":
|
||||||
|
{
|
||||||
|
"content_type": "text/html; charset=UTF-8",
|
||||||
|
"pragma": "no-cache"
|
||||||
|
},
|
||||||
|
"body-expected":
|
||||||
|
{
|
||||||
|
"email": "dude@dudelang.com"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"title": "Get the flags",
|
||||||
"url": "http://localhost§next_url§?email=§email§",
|
"url": "http://localhost§next_url§?email=§email§",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"headers": {
|
"headers": {
|
||||||
|
|
Loading…
Reference in New Issue