Snippet Information
Version: 71 (see latest version)
Author: jeff
Language: php
Licensed under MIT
Other versions
Snippets included by
test_suite v.71
Snippets that include
test_suite v.71
None
"test_suite" v.71 (see latest version)
Description
Scans urls for a series of specified strings and “passes” or “fails” the specified tests. Give it a URL of a YAML file in the format:
- name: first_test url: http://www.google.com match: /google/ - name: second_test url: http://www.google.com match: /blabla.com/
like “http://run.kogbox.com/php/1/test_suite&yaml=example.com”
Run
| View code [+] |
Create a snippet based on this »
<?php
include_snippet(1,"curl_function");
include_snippet(1,"curl_login");
include_snippet(1,"spyc_yaml");
include_snippet(1,'make_pretty_proto'); ?>
<style>
.error a {
color:white;
}
</style>
<h1>Kogbox Test Suite</h1>
<p>Test URLs for a particular string to confirm uptime.</p>
<form method="post" action="http://run.kogbox.com/php/1/test_suite">
<label for="url">Enter test YAML url:</label>
<input style="font-size:1.4em;width:400px;" type="text" name="yaml" value="<?php echo $_REQUEST['yaml'];?>" />
</form>
<p>Ex: "http://example.com/tests.yaml"</p>
<p><a href="javascript:void(0);" onClick="$('field_tests').toggle();">Enter tests as text »</a></p>
<div id="field_tests" style="display:none;">
<form method="post" action="http://run.kogbox.com/php/1/test_suite">
<label for="url">Enter test YAML:</label><br />
<textarea style="font-size:1em;width:400px;height:250px;" rows="10" cols="50" name="field_tests"><?php echo $_REQUEST['field_tests'];?></textarea>
</form>
<p><a href="javascript:void(0);" onClick="$('field_tests_example').toggle();">View example YAML »</a></p>
<p id="field_tests_example" style="display:none;"><pre style="font-size:11px;">
-
name: second_test
url: http://www.weardrobe.com/clothing/search?q=blue&g=all
match: /All clothes that match "/
-
name: test_login_inbox
type: login
url: http://www.weardrobe.com/home/inbox
login_url: http://www.weardrobe.com/account/login
login_params: login=zaphod&password=foobar
match: /using Microsoft Internet Explorer/
</pre ></p>
</div>
<hr />
<?php
function test_for_string($name,$url,$match) {
$url_text = curl($url);
$pass = preg_match($match, $url_text, $out);
if ($pass == 1) {
$color = "green";
$status = "passed";
$error = "";
} else {
$color = "red";
$status = "failed";
$error = "<p><small><code>(Expected '".$match."', but it was not found at '".$url."'.) : ".$pass."</code></small></p>";
}
echo "<p class='error' style='color:white;font-weight:bold;background:".$color."'>Test '".$name."': ".$status." (".$url.")</p>";
echo $error;
}
function test_for_string_with_login($name,$url,$match,$login_url,$login_params) {
$url_text = curl_login($login_url,$login_params,$url);
echo "<p style='color:white;font-weight:bold;background:#009;'>Logged in at ".$login_url."</p>";
$pass = preg_match($match, $url_text, $out);
if ($pass == 1) {
$color = "green";
$status = "passed";
$error = "";
} else {
$color = "red";
$status = "failed";
$error = "<p><small><code>(Expected '".$match."', but it was not found at '".$fetch_url."'.) : ".$pass."</code></small></p>";
}
echo "<p class='error' style='color:white;font-weight:bold;background:".$color."'>Test '".$name."': ".$status." (".$url.")";
?> <a href='javascript:void(0);' onClick="$('<?php echo $name; ?>').toggle();">View output »</a></p><p id='<?php echo $name; ?>' style='display:none;'><code><?php echo htmlentities($url_text); ?></code></p><?php
echo $error;
}
$test_yaml = curl($_REQUEST['yaml']);
$tests = Spyc::YAMLLoad($test_yaml);
foreach ($tests as $test) {
if ($test['type'] == "login") {
test_for_string_with_login($test['name'],$test['url'],$test['match'],$test['login_url'],$test['login_params']);
} else {
test_for_string($test['name'],$test['url'],$test['match']);
}
}
$field_tests = Spyc::YAMLLoad($_REQUEST['field_tests']);
foreach ($field_tests as $test) {
if ($test['type'] == "login") {
test_for_string_with_login($test['name'],$test['url'],$test['match'],$test['login_url'],$test['login_params']);
} else {
test_for_string($test['name'],$test['url'],$test['match']);
}
}
?>

Comments (0) | View comments for all versions (2)
Log in if you have a Kogbox account.