1
1
mirror of https://github.com/ariya/phantomjs.git synced 2024-09-11 12:55:33 +03:00

Don't hardwire listening ports for the test HTTP(S) servers.

Instead, run-tests.py asks the kernel to assign a random unused port
number for each, and then sets environment variables TEST_HTTP_BASE
and TEST_HTTPS_BASE to the base URLs of each server.  In tests that
use testharness.js, these are exposed as global variables with the
same name; tests that don't use the harness will need to pick them out
of require('system').env if they need them.  (Currently there are no
such tests.)

Part of issue #13478 (test suite overhaul).
This commit is contained in:
Zack Weinberg 2015-08-19 16:21:18 -04:00
parent 75944c8d47
commit f69d44b829
31 changed files with 92 additions and 67 deletions

View File

@ -3,23 +3,30 @@
var webpage = require('webpage');
function test_one_page(url) {
async_test(function () {
var page = webpage.create();
page.onResourceReceived = this.step_func(function (response) {
assert_equals(response.status, 200);
});
page.onResourceError = this.unreached_func();
page.onResourceTimeout = this.unreached_func();
page.onLoadFinished = this.step_func_done(function (status) {
assert_equals(status, 'success');
});
page.open(url);
}, url);
var page = webpage.create();
page.onResourceReceived = this.step_func(function (response) {
assert_equals(response.status, 200);
});
page.onResourceError = this.unreached_func();
page.onResourceTimeout = this.unreached_func();
page.onLoadFinished = this.step_func_done(function (status) {
assert_equals(status, 'success');
});
page.open(url);
}
function do_test(path) {
var http_url = TEST_HTTP_BASE + path;
var https_url = TEST_HTTPS_BASE + path;
var http_test = async_test(http_url);
var https_test = async_test(https_url);
http_test.step(test_one_page, null, http_url);
https_test.step(test_one_page, null, https_url);
}
[
'http://localhost:9180/hello.html',
'http://localhost:9180/status?200',
'http://localhost:9180/echo'
'hello.html',
'status?200',
'echo'
]
.forEach(test_one_page);
.forEach(do_test);

View File

@ -26,7 +26,7 @@ async_test(function () {
assert_regexp_not_match(response.url, urlToBlockRegExp);
});
page.open('http://localhost:9180/logo.html',
page.open(TEST_HTTP_BASE + 'logo.html',
this.step_func_done(function (status) {
assert_equals(status, 'success');
assert_equals(abortCount, 1);

View File

@ -12,7 +12,7 @@ async_test(function () {
assert_type_of(request.setHeader, 'function');
request.setHeader('CustomHeader', 'CustomValue');
});
page.open('http://localhost:9180/echo', this.step_func_done(function (status) {
page.open(TEST_HTTP_BASE + 'echo', this.step_func_done(function (status) {
var json, headers;
assert_equals(status, 'success');
json = JSON.parse(page.plainText);

View File

@ -17,7 +17,7 @@ async_test(function () {
lastChunk = resource.body;
bodySize = resource.bodySize;
};
page.open("http://localhost:9180/hello.html",
page.open(TEST_HTTP_BASE + "hello.html",
this.step_func_done(function (status) {
assert_equals(status, "success");
assert_equals(bodySize, content.length);
@ -37,7 +37,7 @@ async_test(function () {
lastChunk = resource.body;
bodySize = resource.bodySize;
};
page.open("http://localhost:9180/hello.html",
page.open(TEST_HTTP_BASE + "hello.html",
this.step_func_done(function (status) {
assert_equals(status, "success");
assert_equals(bodySize, 0);

View File

@ -3,7 +3,7 @@ var webpage = require('webpage');
async_test(function () {
var page = webpage.create();
var url = "http://localhost:9180/cdn-cgi/pe/bag?r%5B%5D="+
var url = TEST_HTTP_BASE + "cdn-cgi/pe/bag?r%5B%5D="+
"http%3A%2F%2Fwww.example.org%2Fcdn-cgi%2Fnexp%2F"+
"abv%3D927102467%2Fapps%2Fabetterbrowser.js";
var receivedUrl;

View File

@ -3,8 +3,8 @@ var webpage = require('webpage');
async_test(function () {
var page = webpage.create();
var urlToChange = 'http://localhost:9180/logo.png';
var alternativeUrl = 'http://localhost:9180/phantomjs-logo.gif';
var urlToChange = TEST_HTTP_BASE + 'logo.png';
var alternativeUrl = TEST_HTTP_BASE + 'phantomjs-logo.gif';
var startStage = 0;
var endStage = 0;
@ -25,7 +25,7 @@ async_test(function () {
}
});
page.open('http://localhost:9180/logo.html',
page.open(TEST_HTTP_BASE + 'logo.html',
this.step_func_done(function (status) {
assert_equals(status, 'success');
assert_equals(startStage, 1);

View File

@ -1,5 +1,5 @@
async_test(function () {
var url = "http://localhost:9180/echo";
var url = TEST_HTTP_BASE + "echo";
var page = new WebPage();
page.cookies = [{
@ -39,7 +39,7 @@ async_test(function () {
}, "adding and deleting cookies with page.cookies");
async_test(function () {
var url = "http://localhost:9180/echo";
var url = TEST_HTTP_BASE + "echo";
var page = new WebPage();
page.addCookie({
@ -66,7 +66,7 @@ async_test(function () {
}, "adding and deleting cookies with page.addCookie and page.deleteCookie");
async_test(function () {
var url = "http://localhost:9180/echo";
var url = TEST_HTTP_BASE + "echo";
var page = new WebPage();
page.cookies = [

View File

@ -11,7 +11,7 @@ async_test(function () {
'User-Agent': 'Overriden-UA',
'Referer': 'Overriden-Referer'
};
page.open('http://localhost:9180/echo', this.step_func_done(function (status) {
page.open(TEST_HTTP_BASE + 'echo', this.step_func_done(function (status) {
var json, headers;
assert_equals(status, 'success');
json = JSON.parse(page.plainText);

View File

@ -60,7 +60,7 @@ async_test(function () {
["frame2-1", "frame2-2", "frame2-3"]);
}
p.open("http://localhost:9180/frameset", this.step_func_done(function (s) {
p.open(TEST_HTTP_BASE + "frameset", this.step_func_done(function (s) {
assert_equals(s, "success");
testFrameSwitchingDeprecated();
}));

View File

@ -88,7 +88,8 @@ async_test(function () {
assert_equals(p.frameName, "frame1");
}
p.open("http://localhost:9180/frameset", this.step_func_done(function (s) {
p.open(TEST_HTTP_BASE + "frameset",
this.step_func_done(function (s) {
assert_equals(s, "success");
testFrameSwitching();
}));

View File

@ -5,7 +5,7 @@ async_test(function () {
// HTTPS server uses, so it should fail.
var page = require('webpage').create();
var url = 'https://localhost:9181/';
var url = TEST_HTTPS_BASE;
page.onResourceError = this.step_func(function (err) {
assert_equals(err.url, url);
assert_equals(err.errorString, "SSL handshake failed");

View File

@ -4,7 +4,7 @@ async_test(function () {
// that the test HTTPS server uses, so it should succeed.
var page = require('webpage').create();
var url = 'https://localhost:9181/';
var url = TEST_HTTPS_BASE;
page.onResourceError = this.unreached_func();
page.open(url, this.step_func_done(function (status) {
assert_equals(status, "success");

View File

@ -9,7 +9,8 @@ async_test(function () {
assert_is_false(page.loading);
assert_equals(page.loadingProgress, 0);
page.open('http://localhost:9180/hello.html', this.step_func_done(function (status) {
page.open(TEST_HTTP_BASE + 'hello.html',
this.step_func_done(function (status) {
assert_equals(status, 'success');
assert_equals(page.loading, false);
assert_equals(page.loadingProgress, 100);

View File

@ -4,7 +4,7 @@ var webpage = require("webpage");
async_test(function () {
var page = webpage.create();
var url = "http://localhost:9180/iframe.html#file:///nonexistent";
var url = TEST_HTTP_BASE + "iframe.html#file:///nonexistent";
var rsErrorCalled = false;
page.onResourceError = this.step_func(function (error) {

View File

@ -4,7 +4,7 @@ var webpage = require("webpage");
async_test(function () {
var page = webpage.create();
var url = "http://localhost:9180/iframe.html#file:///nonexistent";
var url = TEST_HTTP_BASE + "iframe.html#file:///nonexistent";
var rsErrorCalled = false;
page.onResourceError = this.step_func(function (error) {

View File

@ -5,7 +5,7 @@ async_test(function () {
page.stopJavaScript();
});
page.open("http://localhost:9180/js-infinite-loop.html",
page.open(TEST_HTTP_BASE + "js-infinite-loop.html",
this.step_func(function (s) {
assert_equals(s, "success");
}));

View File

@ -15,7 +15,7 @@ async_test(function () {
request.setHeader('CustomHeader', 'ModifiedCustomValue');
});
page.open('http://localhost:9180/echo', this.step_func_done(function (status) {
page.open(TEST_HTTP_BASE + 'echo', this.step_func_done(function (status) {
var json, headers;
assert_equals(status, 'success');
json = JSON.parse(page.plainText);

View File

@ -1,7 +1,7 @@
async_test(function () {
var page = require("webpage").create();
var url1 = "http://localhost:9180/navigation/index.html";
var url2 = "http://localhost:9180/navigation/dest.html";
var url1 = TEST_HTTP_BASE + "navigation/index.html";
var url2 = TEST_HTTP_BASE + "navigation/dest.html";
var onLoadFinished1 = this.step_func(function (status) {
assert_equals(status, "success");

View File

@ -7,7 +7,7 @@ async_test(function () {
});
assert_equals(pluginLength, 0);
page.open('http://localhost:9180/hello.html',
page.open(TEST_HTTP_BASE + 'hello.html',
this.step_func_done(function (status) {
assert_equals(status, 'success');
var pluginLength = page.evaluate(function() {

View File

@ -6,7 +6,7 @@ async_test(function () {
page.onResourceReceived = this.step_func(function (resource) {
assert_equals(resource.status, 200);
});
page.open('http://localhost:9180/hello.html',
page.open(TEST_HTTP_BASE + 'hello.html',
this.step_func_done(function (status) {
assert_equals(status, 'success');
assert_type_of(page.title, 'string');
@ -28,7 +28,7 @@ async_test(function () {
assert_equals(err.errorString, "Operation canceled");
});
page.open('http://localhost:9180/status?status=401' +
page.open(TEST_HTTP_BASE + 'status?status=401' +
'&WWW-Authenticate=Basic%20realm%3D%22PhantomJS%20test%22',
this.step_func_done(function (status) {
assert_equals(status, 'fail');
@ -45,7 +45,7 @@ async_test(function () {
// This is all you have to do to assert that a hook does get called.
page.onResourceTimeout = this.step_func(function(){});
page.open("http://localhost:9180/delay?5",
page.open(TEST_HTTP_BASE + "delay?5",
this.step_func_done(function (s) {
assert_not_equals(s, "success");
}));

View File

@ -22,7 +22,7 @@ async_test(function () {
})
};
var page = new WebPage(pageOptions);
page.openUrl("http://localhost:9180/echo", openOptions, {});
page.openUrl(TEST_HTTP_BASE + "echo", openOptions, {});
}, "processing request body for POST");
@ -40,7 +40,7 @@ async_test(function () {
};
var page = new WebPage(pageOptions);
page.open("http://localhost:9180/echo", 'post', postdata);
page.open(TEST_HTTP_BASE + "echo", 'post', postdata);
}, "POST data is available in onResourceRequested");

View File

@ -14,7 +14,7 @@ async_test(function () {
request.setHeader('CustomHeader', null);
});
page.open('http://localhost:9180/echo',
page.open(TEST_HTTP_BASE + 'echo',
this.step_func_done(function (status) {
var json, headers;
assert_equals(status, 'success');

View File

@ -25,7 +25,7 @@ function render_test(format, option) {
p.clipRect = { top: 0, left: 0, width: 300, height: 300};
p.viewportSize = { width: 300, height: 300};
p.open("http://localhost:9180/render/", this.step_func_done(function (status) {
p.open(TEST_HTTP_BASE + "render/", this.step_func_done(function (status) {
p.render(scratch, opt);
this.add_cleanup(function () { fs.remove(scratch); });
var content = fs.read(scratch, "b");

View File

@ -10,7 +10,7 @@ async_test(function () {
}
});
page.open('http://localhost:9180/hello.html',
page.open(TEST_HTTP_BASE + 'hello.html',
this.step_func_done(function (status) {
assert_equals(status, 'success');
assert_greater_than(requestCount, 0);

View File

@ -2,7 +2,7 @@ var webpage = require('webpage');
async_test(function () {
var page = webpage.create();
var url = 'http://localhost:9180/status?400';
var url = TEST_HTTP_BASE + 'status?400';
var startStage = 0;
var endStage = 0;
var errors = 0;

View File

@ -9,15 +9,15 @@ async_test(function () {
assert_equals(err.status, 404);
assert_equals(err.statusText, 'File not found');
assert_equals(err.url, 'http://localhost:9180/notExist.png');
assert_equals(err.url, TEST_HTTP_BASE + 'notExist.png');
assert_equals(err.errorCode, 203);
assert_regexp_match(err.errorString,
/Error downloading http:\/\/localhost:9180\/notExist\.png/);
/Error downloading http:\/\/localhost:[0-9]+\/notExist\.png/);
assert_regexp_match(err.errorString,
/server replied: File not found/);
});
page.open('http://localhost:9180/missing-img.html',
page.open(TEST_HTTP_BASE + 'missing-img.html',
this.step_func_done(function (status) {
assert_equals(status, 'success');
assert_equals(resourceErrors, 1);

View File

@ -10,7 +10,7 @@ async_test(function () {
assert_equals(page.settings.userAgent, ua);
page.open('http://localhost:9180/user-agent.html',
page.open(TEST_HTTP_BASE + 'user-agent.html',
this.step_func_done(function (status) {
assert_equals(status, 'success');
var agent = page.evaluate(function() {

View File

@ -1,6 +1,6 @@
// Issue 10690: the second page load used to crash on OSX.
var url = 'http://localhost:9180/regression/pjs-10690/index.html';
var url = TEST_HTTP_BASE + 'regression/pjs-10690/index.html';
function do_test() {
var page = require('webpage').create();

View File

@ -1,5 +1,4 @@
var url =
"http://localhost:9180/regression/webkit-60448.html";
var url = TEST_HTTP_BASE + "regression/webkit-60448.html";
async_test(function () {
var p = require("webpage").create();

View File

@ -34,9 +34,6 @@ TIMEOUT = 7 # Maximum duration of PhantomJS execution (in seconds).
# This is a backstop; testharness.js imposes a shorter
# timeout. Both can be increased if necessary.
HTTP_PORT = 9180 # These are currently hardwired into every test that
HTTPS_PORT = 9181 # uses the test servers.
#
# Utilities
#
@ -346,8 +343,8 @@ class TCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
# https://docs.python.org/2/library/socketserver.html#SocketServer.BaseServer.allow_reuse_address
allow_reuse_address = True
def __init__(self, port, use_ssl, handler, base_path, signal_error):
SocketServer.TCPServer.__init__(self, ('localhost', port), handler)
def __init__(self, use_ssl, handler, base_path, signal_error):
SocketServer.TCPServer.__init__(self, ('localhost', 0), handler)
if use_ssl:
self.socket = wrap_socket_ssl(self.socket, base_path)
self._signal_error = signal_error
@ -365,12 +362,13 @@ class TCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
self._signal_error(sys.exc_info())
class HTTPTestServer(object):
def __init__(self, base_path, signal_error):
def __init__(self, base_path, signal_error, verbose):
self.httpd = None
self.httpsd = None
self.base_path = base_path
self.www_path = os.path.join(base_path, 'www')
self.signal_error = signal_error
self.verbose = verbose
def __enter__(self):
handler = FileHandler
@ -384,23 +382,35 @@ class HTTPTestServer(object):
handler.www_path = self.www_path
handler.get_response_hook = ResponseHookImporter(self.www_path)
self.httpd = TCPServer(HTTP_PORT, False,
handler, self.base_path, self.signal_error)
self.httpd = TCPServer(False, handler,
self.base_path, self.signal_error)
os.environ['TEST_HTTP_BASE'] = \
'http://localhost:{}/'.format(self.httpd.server_address[1])
httpd_thread = threading.Thread(target=self.httpd.serve_forever)
httpd_thread.daemon = True
httpd_thread.start()
if self.verbose >= 3:
sys.stdout.write("## HTTP server at {}\n".format(
os.environ['TEST_HTTP_BASE']))
self.httpsd = TCPServer(HTTPS_PORT, True,
handler, self.base_path, self.signal_error)
self.httpsd = TCPServer(True, handler,
self.base_path, self.signal_error)
os.environ['TEST_HTTPS_BASE'] = \
'https://localhost:{}/'.format(self.httpsd.server_address[1])
httpsd_thread = threading.Thread(target=self.httpsd.serve_forever)
httpsd_thread.daemon = True
httpsd_thread.start()
if self.verbose >= 3:
sys.stdout.write("## HTTPS server at {}\n".format(
os.environ['TEST_HTTPS_BASE']))
return self
def __exit__(self, *dontcare):
self.httpd.shutdown()
del os.environ['TEST_HTTP_BASE']
self.httpsd.shutdown()
del os.environ['TEST_HTTPS_BASE']
#
# Running tests and interpreting their results
@ -988,7 +998,9 @@ def init():
def main():
runner = init()
try:
with HTTPTestServer(runner.base_path, runner.signal_server_error):
with HTTPTestServer(runner.base_path,
runner.signal_server_error,
runner.verbose):
sys.exit(runner.run_tests())
except Exception:

View File

@ -1468,6 +1468,11 @@ if (args.test_script === "") {
test_script.lastIndexOf(fs.separator));
require.paths.push(phantom.libraryPath);
// run-tests.py sets these environment variables to the base URLs
// of its HTTP and HTTPS servers.
expose(sys.env['TEST_HTTP_BASE'], 'TEST_HTTP_BASE');
expose(sys.env['TEST_HTTPS_BASE'], 'TEST_HTTPS_BASE');
var output = new Output(sys.stdout, args.verbose);
var tests = new Tests(output);