Skip to content

Commit

Permalink
local webserver support for local testing
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgoucher committed Aug 21, 2012
1 parent 1acf4de commit b0d20ca
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
76 changes: 76 additions & 0 deletions test/LocalWebServer.php
@@ -0,0 +1,76 @@
<?php

require_once 'HTTP/Server.php';
require_once 'Net/Server/Driver/Fork.php';

class LocalWebServer extends HTTP_Server {
function __construct($hostname, $port, $driver = 'Fork') {
if ($port == 0) {
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, '127.0.0.1', 0);
socket_getsockname($socket, $socket_address, $port);
socket_close($socket);
}
$this->_driver = &new E34_Net_Server_Driver_Fork($hostname, $port);
$this->_driver->readEndCharacter = "\r\n\r\n";
$this->_driver->setCallbackObject($this);
}

function GET($clientId, &$request) {
$path_info = $request->getPathInfo();
$path_translated = $this->documentRoot . $path_info;

$headers = array();

// path does not exist
if (!file_exists($path_translated)) {
return array(
"code" => 404
);
}

if (is_readable($path_translated) ) {
$body = fopen($path_translated, "rb");
}

return array(
"code" => 200,
"headers" => $headers,
"body" => $body
);
}
}

class E34_Net_Server_Driver_Fork extends Net_Server_Driver_Fork {
function serviceRequest() {
while (true) {
$readFDs = array($this->clientFD[0]);

// block and wait for data
$ready = @socket_select($readFDs, $this->null, $this->null, null);
// the auto-request of favicon.ico gives things fits, so null check
if ($ready === false && $this->clientFD[0] && socket_last_error($this->clientFD[0]) !== 0) {
$this->_sendDebugMessage('socket_select() failed.');
$this->shutdown();
}

if (in_array($this->clientFD[0], $readFDs) && (!$ready===false)) {
$data = $this->readFromSocket();

// empty data => connection was closed
if ($data === false) {
$this->_sendDebugMessage('Connection closed by peer');
$this->closeConnection();
} else {
$this->_sendDebugMessage('Received ' . trim($data) . ' from ' . $this->_getDebugInfo());

if (method_exists($this->callbackObj, 'onReceiveData')) {
$this->callbackObj->onReceiveData(0, $data);
}
}
}
}
}
}

73 changes: 73 additions & 0 deletions www/alerts.html
@@ -0,0 +1,73 @@
<html>
<head>
<title>Testing Alerts</title>

<script type="text/javascript">
function setInnerText(id, value) {
document.getElementById(id).innerHTML = '<p>' + value + '</p>';
}

function displayPrompt() {
setInnerText('text', prompt('Enter something'));
}

function displayPromptWithDefault() {
setInnerText('text', prompt('Enter something', 'This is a default value'));
}

function displayTwoPrompts() {
setInnerText('text1', prompt('First'));
setInnerText('text2', prompt('Second'));
}

function slowAlert() {
window.setTimeout(function() {
alert('Slow');
}, 200);
}
</script>
</head>
<body>

<h1>Testing Alerts and Stuff</h1>

<p>This tests alerts: <a href="#" id="alert" onclick="alert('cheese');">click me</a></p>

<p>This tests alerts: <a href="#" id="empty-alert" onclick="alert('');">click me</a></p>

<p>Let's make the <a href="#" id="prompt" onclick="displayPrompt();">prompt happen</a></p>

<p>Let's make the <a href="#" id="prompt-with-default" onclick="displayPromptWithDefault();">prompt with default happen</a></p>

<p>Let's make TWO <a href="#" id="double-prompt" onclick="displayTwoPrompts();">prompts happen</a></p>

<p>A <a href="#" id="slow-alert" onclick="slowAlert();">SLOW</a> alert</p>

<p>This is a test of a confirm:
<a href="simpleTest.html" id="confirm" onclick="return confirm('Are you sure?');">test confirm</a></p>

<p>This is a test of showModalDialog: <a href="#" id="dialog" onclick="showModalDialog('javascriptPage.html')">test dialog</a></p>

<p>This is a test of an alert in an iframe:
<iframe src="iframeWithAlert.html" name="iframeWithAlert"></iframe>
</p>

<p>This is a test of an alert in a nested iframe:
<iframe src="iframeWithIframe.html" name="iframeWithIframe"></iframe>
</p>

<p>This is a test of an alert open from onload event handler: <a id="open-page-with-onload-alert" href="pageWithOnLoad.html">open new page</a></p>

<p>This is a test of an alert open from onload event handler: <a id="open-window-with-onload-alert" href="pageWithOnLoad.html" target="onload">open new window</a></p>

<p>This is a test of an alert open from onunload event handler: <a id="open-page-with-onunload-alert" href="pageWithOnUnload.html">open new page</a></p>

<p>This is a test of an alert open from onclose event handler: <a id="open-window-with-onclose-alert" href="pageWithOnUnload.html" target="onclose">open new window</a></p>

<p>This is a test of an alert open from onclose event handler: <a id="open-new-window" href="blank.html" target="newwindow">open new window</a></p>

<div id="text"></div>
<div id="text1"></div>
<div id="text2"></div>
</body>
</html>

0 comments on commit b0d20ca

Please sign in to comment.