Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Created June 19, 2012 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderofsalvation/2953899 to your computer and use it in GitHub Desktop.
Save coderofsalvation/2953899 to your computer and use it in GitHub Desktop.
Recbox - record and replay PHP requests (Great for testing SaaS-applications on devservers with livedata)
<?
/**
* File: recbox.php
* Date: Tue 19 Jun 2012 01:31:59 PM CEST
*
* records/replays php requests.
*
* PROBLEM: big SaaS-applications are like a racecar running 200kmph nonstop.
* Usually developers add new features on a developmentserver, to test them.
* Then..eventually it needs to be integrated on the liveserver (fingerscrossed).
* The testing what the developer can do is not really comparable to whats going
* on the liveserver.
*
* SOLUTION: wouldnt it be great if you could record all requests on the liveserver and replay
* it on the devserver? Now THAT would be a comparable stresstest. This class is a
* great startingpoint for it.
*
* DEMO:
*
* Changelog:
*
* [Tue 19 Jun 2012 01:31:59 PM CEST]
* first sketch from scratch
*
* @todo assertions
* @todo commenting
* @todo simulating $_FILES array ?
* @todo simulation of date/time() ?
* @todo maybe additional time-dependent testing instead of a foreach
*
* Usage example (recording)
* =========================
* <code>
* include_once("recbox.php");
* session_start(); // dont forget
* $rc = new recbox();
* $rc->capture();
* </code>
*
* Usage example (replaying)
* =========================
* <code>
* $ php recbox.php index.php /tmp/recbox-*.php
*
* [x] running '/tmp/recbox-1340108751.php'
* [x] requesting: /recbox/?id=hehehehe1
* [x] running '/tmp/recbox-1340108754.php'
* [x] requesting: /recbox/?id=hehehehe2
* [x] running '/tmp/recbox-1340108755.php'
* [x] requesting: /recbox/?id=hehehehe3
* [x] running '/tmp/recbox-1340108756.php'
* [x] requesting: /recbox/?id=hehehehe4
* [x] running '/tmp/recbox-1340108757.php'
* [x] requesting: /recbox/?id=hehehehe5
*
* </code>
*
* @version $id$
* @copyright 2012 Coder of Salvation
* @author Leon van Kammen | Coder of Salvation, sqz <info@leon.vankammen.eu>
*
* @license BSD
*
* Copyright 2012, Leon van Kammen | Coder of Salvation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY LEON VAN KAMMEN ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of Leon van Kammen.
*
*/
class recbox {
private $filename = "";
public static $items = array();
public function __construct( $filename = "/tmp/recbox-%s.php" ){
$this->filename = $filename;
}
public function capture( $data = false ){
$container = (object)array(
"_GET" => $_GET,
"_POST" => $_POST,
"_SESSION" => session_encode(),
"_SERVER" => $_SERVER,
"data" => $data
);
try{
$filename = sprintf( $this->filename, time() );
file_put_contents( $filename, serialize( $container ) );
}catch (Exception $e ){ print("unable write {$filename}"); }
}
public function play( $filename ){
/* *FIXME* how to emulate the date? */
print("[x] running '{$filename}'\n");
try{
$container = unserialize( file_get_contents( $filename ) );
$_GET = array_merge( $_GET, $container->_GET );
$_POST = array_merge( $_POST, $container->_POST );
$_SERVER = array_merge( $_SERVER, $container->_SERVER );
session_decode( $container->_SESSION );
print("[x] requesting: {$_SERVER['REQUEST_URI']}\n");
}catch (Exception $e ){ print("[!] could not run it\n"); }
}
}
// minimal cli interface
if( isset($argv[1]) ){
$files = $argv;
array_shift($files);
$mainfile = array_shift($files);
$rc = new recbox();
foreach( $files as $file ) $rc->play($file);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment