-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35f15fa
commit a7d6706
Showing
14 changed files
with
6,298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* The server that proxies the requests to CouchDB and serves the testrunner */ | ||
/* Props to the guys at CloudAnt for teaching me how to setup the reverse-proxy <3 */ | ||
/* Bases on: http://support.cloudant.com/customer/portal/articles/359321-how-do-i-read-and-write-to-my-cloudant-database-from-the-browser- */ | ||
|
||
var util = require('util'); | ||
var http = require('http'); | ||
var url = require('url'); | ||
var express = require('express'); | ||
|
||
var app = express.createServer(); | ||
|
||
// The remote database information | ||
var PREFIX = '/db/'; | ||
var TARGET = 'http://localhost'; | ||
var PORT = 5984; | ||
|
||
// set up a username and a password | ||
// set to null if not needed | ||
var USERNAME = ''; | ||
var PASSWORD = ''; | ||
|
||
// This app's port | ||
var appPort = process.env['app_port'] || 3000; | ||
|
||
// decide to forward the request or to | ||
function couchDBForward(req, res, next){ | ||
var u = url.parse(req.url); | ||
// Only serve URLs that start with PREFIX | ||
if(u.pathname.substring(0, PREFIX.length) != PREFIX) | ||
next(); // this may be a static page request or whatever | ||
else{ | ||
u = TARGET + ':' + PORT + u.pathname.substring(PREFIX.length-1) + (u.search||''); | ||
couchDBRequest(req, res, u); | ||
} | ||
}; | ||
|
||
function error(response, error, reason, code) { | ||
util.log('Error '+code+': '+error+' ('+reason+').'); | ||
response.writeHead(code, { 'Content-Type': 'application/json' }); | ||
response.write(JSON.stringify({ error: error, reason: reason })); | ||
response.end(); | ||
} | ||
|
||
function unknownError(response, e) { | ||
util.log(e.stack); | ||
error(response, 'unknown', 'Unexpected error.', 500); | ||
} | ||
|
||
function couchDBRequest(inRequest, inResponse, uri) { | ||
util.log(inRequest.method + ' ' + uri); | ||
uri = url.parse(uri); | ||
var outPort = (uri.port || 80); | ||
var path = uri.pathname + (uri.search || ''); | ||
|
||
var headers = inRequest.headers; | ||
headers['host'] = uri.hostname + ':' + outPort; | ||
headers['x-forwarded-for'] = inRequest.connection.remoteAddress; | ||
headers['referer'] = 'http://' + uri.hostname + ':' + outPort + '/'; | ||
|
||
var reqOptions = { | ||
hostname: uri.hostname, | ||
path: path, | ||
port: outPort, | ||
method: inRequest.method, | ||
headers: headers | ||
}; | ||
|
||
if(USERNAME && PASSWORD) | ||
reqOptions.auth = USERNAME + ':' + PASSWORD; | ||
|
||
var outRequest = http.request(reqOptions, function(res){ | ||
inResponse.statusCode = res.statusCode; | ||
|
||
res.on('data', function(chunk){ | ||
inResponse.write(chunk); | ||
}); | ||
|
||
res.on('end', function(){ | ||
inResponse.end(); | ||
}); | ||
}); | ||
|
||
outRequest.on('error', function(e){ | ||
console.log('error'); | ||
console.log(arguments); | ||
unknownError(inResponse, e); | ||
}); | ||
|
||
if(inRequest.method == 'POST' || inRequest.method == 'PUT') | ||
outRequest.write(JSON.stringify(inRequest.body)); | ||
|
||
outRequest.end(); | ||
}; | ||
|
||
process.on('uncaughtException', function(e) { | ||
util.log(e.stack); | ||
}); | ||
|
||
app.configure(function(){ | ||
app.use(express.methodOverride()); | ||
app.use(express.bodyParser()); | ||
app.use(express.cookieParser()); | ||
app.use(express.session({secret: '2398hf8326fd236g39'})); | ||
app.use(couchDBForward); | ||
app.use(app.router); | ||
app.use(express.static(__dirname + '/testrunner')); | ||
}); | ||
|
||
app.listen(appPort); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>QUnit Test Suite</title> | ||
<link rel="stylesheet" href="../qunit/qunit.css" type="text/css" media="screen"> | ||
<script type="text/javascript" src="../qunit/qunit.js"></script> | ||
<script type="text/javascript" src="test.js"></script> | ||
<script type="text/javascript" src="same.js"></script> | ||
<script> | ||
var logs = ["begin", "testStart", "testDone", "log", "moduleStart", "moduleDone", "done"]; | ||
for (var i = 0; i < logs.length; i++) { | ||
(function() { | ||
var log = logs[i]; | ||
QUnit[log] = function() { | ||
console.log(log, arguments); | ||
}; | ||
})(); | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<div id="qunit-fixture">test markup</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>QUnit Test Suite</title> | ||
<link rel="stylesheet" href="qunit/qunit.css" type="text/css" media="screen"> | ||
<script type="text/javascript" src="src/jquery.js"></script> | ||
<script type="text/javascript" src="src/jquery.couch.js"></script> | ||
<script type="text/javascript" src="src/underscore.js"></script> | ||
<script type="text/javascript" src="src/backbone.js"></script> | ||
<script type="text/javascript" src="src/backbone-couchdb.js"></script> | ||
<script type="text/javascript" src="qunit/qunit.js"></script> | ||
<script type="text/javascript" src="test.js"></script> | ||
<script type="text/javascript" src="same.js"></script> | ||
</head> | ||
<body> | ||
<h1 id="qunit-header">QUnit Test Suite</h1> | ||
<h2 id="qunit-banner"></h2> | ||
<div id="qunit-testrunner-toolbar"></div> | ||
<h2 id="qunit-userAgent"></h2> | ||
<ol id="qunit-tests"></ol> | ||
<div id="qunit-fixture">test markup</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>QUnit Test Suite</title> | ||
<link rel="stylesheet" href="../qunit/qunit.css" type="text/css" media="screen"> | ||
<script type="text/javascript" src="../qunit/qunit.js"></script> | ||
<script type="text/javascript" src="logs.js"></script> | ||
</head> | ||
<body> | ||
<h1 id="qunit-header">QUnit Test Suite</h1> | ||
<h2 id="qunit-banner"></h2> | ||
<div id="qunit-testrunner-toolbar"></div> | ||
<h2 id="qunit-userAgent"></h2> | ||
<ol id="qunit-tests"></ol> | ||
<div id="qunit-fixture">test markup</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// TODO disable reordering for this suite! | ||
|
||
|
||
var begin = 0, | ||
moduleStart = 0, | ||
moduleDone = 0, | ||
testStart = 0, | ||
testDone = 0, | ||
log = 0, | ||
moduleContext, | ||
moduleDoneContext, | ||
testContext, | ||
testDoneContext, | ||
logContext; | ||
|
||
QUnit.begin = function() { | ||
begin++; | ||
}; | ||
QUnit.done = function() { | ||
}; | ||
QUnit.moduleStart = function(context) { | ||
moduleStart++; | ||
moduleContext = context; | ||
}; | ||
QUnit.moduleDone = function(context) { | ||
moduleDone++; | ||
moduleDoneContext = context; | ||
}; | ||
QUnit.testStart = function(context) { | ||
testStart++; | ||
testContext = context; | ||
}; | ||
QUnit.testDone = function(context) { | ||
testDone++; | ||
testDoneContext = context; | ||
}; | ||
QUnit.log = function(context) { | ||
log++; | ||
logContext = context; | ||
}; | ||
|
||
var logs = ["begin", "testStart", "testDone", "log", "moduleStart", "moduleDone", "done"]; | ||
for (var i = 0; i < logs.length; i++) { | ||
(function() { | ||
var log = logs[i], | ||
logger = QUnit[log]; | ||
QUnit[log] = function() { | ||
console.log(log, arguments); | ||
logger.apply(this, arguments); | ||
}; | ||
})(); | ||
} | ||
|
||
module("logs1"); | ||
|
||
test("test1", 13, function() { | ||
equal(begin, 1); | ||
equal(moduleStart, 1); | ||
equal(testStart, 1); | ||
equal(testDone, 0); | ||
equal(moduleDone, 0); | ||
|
||
deepEqual(logContext, { | ||
result: true, | ||
message: undefined, | ||
actual: 0, | ||
expected: 0 | ||
}); | ||
equal("foo", "foo", "msg"); | ||
deepEqual(logContext, { | ||
result: true, | ||
message: "msg", | ||
actual: "foo", | ||
expected: "foo" | ||
}); | ||
strictEqual(testDoneContext, undefined); | ||
deepEqual(testContext, { | ||
name: "test1" | ||
}); | ||
strictEqual(moduleDoneContext, undefined); | ||
deepEqual(moduleContext, { | ||
name: "logs1" | ||
}); | ||
|
||
equal(log, 12); | ||
}); | ||
test("test2", 10, function() { | ||
equal(begin, 1); | ||
equal(moduleStart, 1); | ||
equal(testStart, 2); | ||
equal(testDone, 1); | ||
equal(moduleDone, 0); | ||
|
||
deepEqual(testDoneContext, { | ||
name: "test1", | ||
failed: 0, | ||
passed: 13, | ||
total: 13 | ||
}); | ||
deepEqual(testContext, { | ||
name: "test2" | ||
}); | ||
strictEqual(moduleDoneContext, undefined); | ||
deepEqual(moduleContext, { | ||
name: "logs1" | ||
}); | ||
|
||
equal(log, 22); | ||
}); | ||
|
||
module("logs2"); | ||
|
||
test("test1", 9, function() { | ||
equal(begin, 1); | ||
equal(moduleStart, 2); | ||
equal(testStart, 3); | ||
equal(testDone, 2); | ||
equal(moduleDone, 1); | ||
|
||
deepEqual(testContext, { | ||
name: "test1" | ||
}); | ||
deepEqual(moduleDoneContext, { | ||
name: "logs1", | ||
failed: 0, | ||
passed: 23, | ||
total: 23 | ||
}); | ||
deepEqual(moduleContext, { | ||
name: "logs2" | ||
}); | ||
|
||
equal(log, 31); | ||
}); | ||
test("test2", 8, function() { | ||
equal(begin, 1); | ||
equal(moduleStart, 2); | ||
equal(testStart, 4); | ||
equal(testDone, 3); | ||
equal(moduleDone, 1); | ||
|
||
deepEqual(testContext, { | ||
name: "test2" | ||
}); | ||
deepEqual(moduleContext, { | ||
name: "logs2" | ||
}); | ||
|
||
equal(log, 39); | ||
}); |
Oops, something went wrong.