| 1 |
<!DOCTYPE html> |
| 2 |
<!-- Copyright (c) 2015 Lars Kuhtz <[email protected]> --> |
| 3 |
<html> |
| 4 |
<head> |
| 5 |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> |
| 6 |
<script type="text/javascript"> |
| 7 |
|
| 8 |
var url = 'http://localhost:8080' |
| 9 |
var wsurl = 'ws://localhost:8080' |
| 10 |
|
| 11 |
/* ************************************************************************** */ |
| 12 |
/* Utils */ |
| 13 |
|
| 14 |
/* asynchronous array map function |
| 15 |
* |
| 16 |
* The function f is called on each element in the value and |
| 17 |
* is expected to return a promise object. |
| 18 |
* |
| 19 |
* asyncMap itself returns a promise object that is completed |
| 20 |
* when the promises for all elements completed. When all element |
| 21 |
* promises resolve the array promises is resolved. If at least |
| 22 |
* one element promise is reject the array promises is rejected. |
| 23 |
* |
| 24 |
* The done and fail callbacks are called with an object of the form |
| 25 |
* |
| 26 |
* { 'successes':successes, 'failures': failures, 'results': results } |
| 27 |
* |
| 28 |
* The results that array has one value for each element of the original |
| 29 |
* array. The values in the result array are objects of the form |
| 30 |
* |
| 31 |
* { success: trueOrFalse, result: elementPromiseResult } |
| 32 |
* |
| 33 |
* The progress callback is given an object of the form |
| 34 |
* |
| 35 |
* { successes: sumS, failures: numF, missing: numberOfMissingElementPromises } |
| 36 |
*/ |
| 37 |
var asyncMap = function (a,f) { |
| 38 |
var def = $.Deferred(); |
| 39 |
var successes = 0; |
| 40 |
var failures = 0; |
| 41 |
var results = new Array(a.length); |
| 42 |
var c = a.length; |
| 43 |
|
| 44 |
a.map(function (val, idx, arr) { |
| 45 |
t = f(val); |
| 46 |
t.done(function(arg) { |
| 47 |
successes++; |
| 48 |
results[idx] = { 'success':true, 'result':arg }; |
| 49 |
}); |
| 50 |
t.fail(function(arg) { |
| 51 |
failures++; |
| 52 |
results[idx] = { 'success':false, 'result':arg }; |
| 53 |
}); |
| 54 |
t.always(function() { |
| 55 |
c--; |
| 56 |
if (c > 0) { |
| 57 |
def.notify({ 'successes':successes, 'failures': failures, 'missing': c }); |
| 58 |
} else { |
| 59 |
var result = { 'successes':successes, 'failures': failures, 'results': results }; |
| 60 |
if (failures === 0) { |
| 61 |
def.resolve(result); |
| 62 |
} else { |
| 63 |
def.reject(result); |
| 64 |
}; |
| 65 |
}; |
| 66 |
}); |
| 67 |
}); |
| 68 |
return def.promise(); |
| 69 |
}; |
| 70 |
|
| 71 |
// monoid concatenation for asyncMap results |
| 72 |
var concatResults = function (r1, r2) { |
| 73 |
return { |
| 74 |
'successes': r1.successes + r2.successes, |
| 75 |
'failures': r1.failures + r2.failures, |
| 76 |
'results': r1.results.concat(r2.results) |
| 77 |
}; |
| 78 |
} |
| 79 |
|
| 80 |
// monoid identity for asyncMap results |
| 81 |
var emptyResult = { 'successes': 0, 'failures': 0, 'results':[] }; |
| 82 |
|
| 83 |
// monad join for asyncMap results |
| 84 |
var flattenResults = function (r) { |
| 85 |
return r.results.reduce(function (p,c,i,a) { return concatResults(p,c.result); }, emptyResult); |
| 86 |
}; |
| 87 |
|
| 88 |
/* ************************************************************************** */ |
| 89 |
/* PhantomJS Integration */ |
| 90 |
|
| 91 |
// Integration with PhantomJS |
| 92 |
// |
| 93 |
// call with: phantomjs --ignore-ssl-errors=true phantomjs.js $PWD/index.html |
| 94 |
// |
| 95 |
if (typeof window.callPhantom === 'function') { |
| 96 |
window.setTimeout(function () { window.callPhantom("timeout"); },3000); |
| 97 |
}; |
| 98 |
|
| 99 |
var phantomJsCb = function (result) { |
| 100 |
if (typeof window.callPhantom === 'function') { |
| 101 |
window.callPhantom(result); |
| 102 |
}; |
| 103 |
}; |
| 104 |
|
| 105 |
/* ************************************************************************** */ |
| 106 |
/* Run Tests */ |
| 107 |
|
| 108 |
var runTest = function (params) { |
| 109 |
|
| 110 |
var test = $.Deferred(); |
| 111 |
var req = $.ajax( |
| 112 |
{ 'method': params.method |
| 113 |
, 'url': url + "/cors/" + params.path + "/" + encodeURIComponent(params.label) |
| 114 |
, 'contentType': params.contentType |
| 115 |
, 'crossDomain': params.cors |
| 116 |
, 'headers': params.headers |
| 117 |
}); |
| 118 |
|
| 119 |
var succ = function () { test.resolve(params); } |
| 120 |
var fail = function () { test.reject(params); } |
| 121 |
var check = function () { |
| 122 |
/* check if the response matches our expectations */ |
| 123 |
for (var i in params.expectedHeaders) { |
| 124 |
if (params.expectedHeaders.hasOwnProperty(i)) { |
| 125 |
var hdr = req.getResponseHeader(i); |
| 126 |
// console.log(req.getAllResponseHeaders()); |
| 127 |
if ( ! hdr || hdr != params.expectedHeaders.i) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
return true; |
| 133 |
} |
| 134 |
req.done(check() === params.expectedResult ? succ : fail); |
| 135 |
req.fail(params.expectedResult ? fail : succ); |
| 136 |
|
| 137 |
return test.promise(); |
| 138 |
} |
| 139 |
|
| 140 |
var pageTest = function (params) { |
| 141 |
test = runTest(params); |
| 142 |
|
| 143 |
test.done(function() { |
| 144 |
$('#results').append('<p style="color:green">' + params.label + ': success</p>'); |
| 145 |
}); |
| 146 |
|
| 147 |
test.fail(function() { |
| 148 |
$('#results').append('<p style="color:red">' + params.label + ': failure</p>'); |
| 149 |
}); |
| 150 |
return test; |
| 151 |
} |
| 152 |
|
| 153 |
var runAllPageTests = function (tests) { |
| 154 |
return asyncMap(tests, pageTest); |
| 155 |
} |
| 156 |
|
| 157 |
/* ************************************************************************** */ |
| 158 |
/* WebSockets */ |
| 159 |
|
| 160 |
/* a super simple ws service client |
| 161 |
* |
| 162 |
* TODO add proper callbacks (e.g. close, error); |
| 163 |
* TODO in case we use this for anything more complex we should add proper |
| 164 |
* streaming by supporting chunking. |
| 165 |
* |
| 166 |
*/ |
| 167 |
var WsService = function (url, timeout) { |
| 168 |
var timeout = timeout ? timeout : 1000; |
| 169 |
var curMsgId = 0; |
| 170 |
var ready = $.Deferred(); |
| 171 |
var websocket = new WebSocket(url + "/cors"); |
| 172 |
var newMsgId = function () { return ++curMsgId; }; |
| 173 |
|
| 174 |
// maps message ids data to promise objects |
| 175 |
var msgMap = {} |
| 176 |
|
| 177 |
var ws = function (msg) { |
| 178 |
var p = $.Deferred(); |
| 179 |
var req = { id: newMsgId(), msg: msg}; |
| 180 |
ready.then(function () { websocket.send(JSON.stringify(req)); }); |
| 181 |
msgMap[req.id] = p; |
| 182 |
setTimeout(function () { p.reject("timeout: no response from websocket after " + timeout + " milliseconds"); }, timeout); |
| 183 |
return p.promise(); |
| 184 |
}; |
| 185 |
|
| 186 |
websocket.onmessage = function (evt) { |
| 187 |
var resp = JSON.parse(evt.data); |
| 188 |
var prom = msgMap[resp.id]; |
| 189 |
if (prom) { |
| 190 |
prom.resolve(resp.msg); |
| 191 |
} else { |
| 192 |
console.log("unexpected message from websocket: " + JSON.stringify(resp)); |
| 193 |
}; |
| 194 |
}; |
| 195 |
|
| 196 |
var wserror = function (evt) { |
| 197 |
for (i in msgMap) { |
| 198 |
if (msgMap.hasOwnProperty(i)) { |
| 199 |
msgMap[i].fail(); |
| 200 |
}; |
| 201 |
}; |
| 202 |
msgMap = {}; |
| 203 |
}; |
| 204 |
|
| 205 |
websocket.onopen = function(evt) { |
| 206 |
ready.resolve(); |
| 207 |
}; |
| 208 |
|
| 209 |
websocket.onerror = function(evt) { |
| 210 |
console.log("websocket error: " + evt.data); |
| 211 |
wserror(evt); |
| 212 |
}; |
| 213 |
|
| 214 |
websocket.onclose = function(evt) { |
| 215 |
console.log("websocket closed unexpectedly: " + evt.data); |
| 216 |
wserror(evt); |
| 217 |
}; |
| 218 |
|
| 219 |
return ws; |
| 220 |
}; |
| 221 |
|
| 222 |
var websocketTest = function () { |
| 223 |
|
| 224 |
var ws = WsService(wsurl); |
| 225 |
|
| 226 |
var wstest = function (msg) { |
| 227 |
var p = ws(msg); |
| 228 |
|
| 229 |
p.done(function() { |
| 230 |
$('#wsresults').append('<p style="color:green">' + msg + ': success</p>'); |
| 231 |
}); |
| 232 |
|
| 233 |
p.fail(function() { |
| 234 |
$('#wsresults').append('<p style="color:red">' + msg + ': failure</p>'); |
| 235 |
}); |
| 236 |
return p; |
| 237 |
} |
| 238 |
|
| 239 |
return asyncMap(["msg 0", "msg 1", "msg 2"], wstest); |
| 240 |
}; |
| 241 |
|
| 242 |
/* ************************************************************************** */ |
| 243 |
/* Test Vectors */ |
| 244 |
|
| 245 |
var mkTest = function (label, expectedResult, method, path, credentials, contentType, headers, cors) { |
| 246 |
return { |
| 247 |
label: label, |
| 248 |
expectedResult: expectedResult, |
| 249 |
method: method, |
| 250 |
path: path, |
| 251 |
credentials: credentials, |
| 252 |
contentType: contentType, |
| 253 |
headers: headers, |
| 254 |
}; |
| 255 |
} |
| 256 |
|
| 257 |
var simpleTests = [ |
| 258 |
mkTest("simple GET 1", true, "GET", "simple", false, 'text/plain', []), |
| 259 |
mkTest("simple GET 2", true, "GET", "simple", false, 'application/x-www-form-urlencoded', []), |
| 260 |
mkTest("simple GET 3", true, "GET", "simple", false, 'multipart/form-data', []), |
| 261 |
mkTest("simple GET 4", false, "GET", "simple", false, 'application/json', []), |
| 262 |
mkTest("simple GET 5", false, "GET", "simple", false, 'text/plain', {'abc': 'abc'}), |
| 263 |
|
| 264 |
mkTest("simple HEAD 1", true, "HEAD", "simple", false, 'text/plain', []), |
| 265 |
mkTest("simple HEAD 2", true, "HEAD", "simple", false, 'application/x-www-form-urlencoded', []), |
| 266 |
mkTest("simple HEAD 3", true, "HEAD", "simple", false, 'multipart/form-data', []), |
| 267 |
mkTest("simple HEAD 4", false, "HEAD", "simple", false, 'application/json', []), |
| 268 |
mkTest("simple HEAD 5", false, "HEAD", "simple", false, 'text/plain', {'abc': 'abc'}), |
| 269 |
|
| 270 |
mkTest("simple POST 1", true, "POST", "simple", false, 'text/plain', []), |
| 271 |
mkTest("simple POST 2", true, "POST", "simple", false, 'application/x-www-form-urlencoded', []), |
| 272 |
mkTest("simple POST 3", true, "POST", "simple", false, 'multipart/form-data', []), |
| 273 |
mkTest("simple POST 4", false, "POST", "simple", false, 'application/json', []), |
| 274 |
mkTest("simple POST 5", false, "POST", "simple", false, 'test/plain', {'abc':'abc'}), |
| 275 |
|
| 276 |
mkTest("simple PUT 1", false, "PUT", "simple", false, 'text/plain', []), |
| 277 |
mkTest("simple DELETE 1", false, "DELETE", "simple", false, 'text/plain', []), |
| 278 |
mkTest("simple PATCH 1", false, "PATCH", "simple", false, 'text/plain', []), |
| 279 |
]; |
| 280 |
|
| 281 |
var nonSimplePolicyTests = [ |
| 282 |
/* are the simple requests (GET and POST) expected to succeed? */ |
| 283 |
mkTest("nonSimplePolicy GET", true, "GET", "non-simple", false, 'text/plain', []), |
| 284 |
mkTest("nonSimplePolicy POST", true, "POST", "non-simple", false, 'text/plain', []), |
| 285 |
mkTest("nonSimplePolicy PUT", true, "PUT", "non-simple", false, 'text/plain', []), |
| 286 |
mkTest("nonSimplePolicy DELETE", false, "DELETE", "non-simple", false, 'text/plain', []), |
| 287 |
|
| 288 |
/* check presence of 'Vary: Origin' header */ |
| 289 |
/* Currenly disabled. It seems that phantomjs doesn't support it |
| 290 |
{ |
| 291 |
label: "nonSimplePolicy vary origin", |
| 292 |
expectedResult: true, |
| 293 |
method: "PUT", |
| 294 |
path: "non-simple", |
| 295 |
credentials: false, |
| 296 |
contentType: 'text/plain', |
| 297 |
headers: {}, |
| 298 |
cors: true, |
| 299 |
expectedHeaders: { 'Vary' : 'Origin' } |
| 300 |
}, |
| 301 |
*/ |
| 302 |
|
| 303 |
/* check that X-cors-test is accepted header */ |
| 304 |
{ |
| 305 |
label: "nonSimplePolicy request headers", |
| 306 |
expectedResult: true, |
| 307 |
method: "PUT", |
| 308 |
path: "non-simple", |
| 309 |
credentials: false, |
| 310 |
contentType: 'text/plain', |
| 311 |
headers: { 'X-cors-test' : 'cors-test' }, |
| 312 |
cors: true, |
| 313 |
expectedHeaders : {} |
| 314 |
} |
| 315 |
]; |
| 316 |
|
| 317 |
$(document).ready(function() { |
| 318 |
var p = runAllPageTests(simpleTests.concat(nonSimplePolicyTests)); |
| 319 |
var q = websocketTest(); |
| 320 |
var r = asyncMap([p,q], function (x) { return x; }); |
| 321 |
r.always(function (x) { phantomJsCb(flattenResults(x)); }); |
| 322 |
}); |
| 323 |
|
| 324 |
</script> |
| 325 |
</head> |
| 326 |
<body> |
| 327 |
<h1>Results</h1> |
| 328 |
<div id="results"></div> |
| 329 |
<h1>Websocket Test Results</h1> |
| 330 |
<div id="wsresults"></div> |
| 331 |
</body> |
| 332 |
</html> |
| 333 |
|
| 334 |
|