Name: js-handler/tests/test.js
| 1: | var restify = require('restify'); |
| 2: | var url=require ("url"); |
| 3: | var assert=require ('assert'); |
| 4: | |
| 5: | module.exports = { |
| 6: | setUp: function (callback) { |
| 7: | this.dataelements={'numberA':50,'content':'text'}; |
| 8: | this.endpoints = { |
| 9: | 'a': 'xmpp://[email protected]', |
| 10: | 'b': 'xmpp://[email protected]/17' |
| 11: | }; |
| 12: | this.result= { |
| 13: | 'res1':50, |
| 14: | 'res2':30 |
| 15: | }; |
| 16: | this.correctCode="data.numberA=100"; |
| 17: | this.statusTrue="50==resultvar.res1"; |
| 18: | this.statusFalse="50!=resultvar.res1"; |
| 19: | this.statusOther="50"; |
| 20: | this.errorCode="donsole.log('x');"; |
| 21: | this.content="'<xml><data><numberA>100</numberA><content>'+data.content+'</content></data></xml>'"; |
| 22: | this.jsonToXML = "data.content="+this.content+";"; |
| 23: | //var asynccode= "console.log('loop');"; |
| 24: | this.asynccode= " for(var i = 0; i < 100000;i++){ var a=10*100;}; data.numberA=42;"; |
| 25: | callback(); |
| 26: | }, |
| 27: | tearDown: function (callback) { |
| 28: | callback(); |
| 29: | }, |
| 30: | test1: function (test){ |
| 31: | var client = restify.createStringClient({ |
| 32: | url: 'http://sumatra.pri.univie.ac.at:9292', |
| 33: | version: '*' |
| 34: | }); |
| 35: | |
| 36: | var ins; |
| 37: | var server = restify.createServer(); |
| 38: | server.use(restify.queryParser()); |
| 39: | server.use(restify.bodyParser()); |
| 40: | server.listen(9293, function() { |
| 41: | console.log('%s listening at %s', server.name, server.url); |
| 42: | }); |
| 43: | |
| 44: | var dataelements = this.dataelements; |
| 45: | server.post('/callback', function create(mreq, mres, mnext) { |
| 46: | console.log("receiving callback "+JSON.stringify(mreq.params.instance)); |
| 47: | var callbackInstance=mreq.params.instance |
| 48: | test.equals(ins,callbackInstance, "expecting instance: "+ins+" ,receivingInstance: "+callbackInstance); |
| 49: | |
| 50: | client.get('/' + ins + '/dataelements', function(err,req,res,obj){ |
| 51: | dataelements.numberA=42; |
| 52: | test.equals(obj,JSON.stringify(dataelements), "dataelements.numberA should be 42"); |
| 53: | |
| 54: | client.get('/' + ins + '/state', function(err,req,res,obj){ |
| 55: | test.equals(obj,"\"executed\"", "code not executed asychronously"); |
| 56: | mres.send(200); |
| 57: | test.done(); |
| 58: | client.close(); |
| 59: | server.close(); |
| 60: | }); |
| 61: | }); |
| 62: | |
| 63: | test.expect(3); |
| 64: | }); |
| 65: | |
| 66: | client.post('/', {'dataelements':JSON.stringify(this.dataelements), 'endpoints':JSON.stringify(this.endpoints), 'resultname':'resultvar', 'resultvalue':JSON.stringify(this.result),'code':this.asynccode, 'callback':'http://localhost:9293/callback'} , function(err,req,res,obj){ |
| 67: | ins = obj; |
| 68: | console.log("received instance before execution: "+ins); |
| 69: | }); |
| 70: | }, |
| 71: | test2: function (test){ |
| 72: | var client = restify.createStringClient({ |
| 73: | url: 'http://sumatra.pri.univie.ac.at:9292', |
| 74: | version: '*' |
| 75: | }); |
| 76: | client.post('/', {'dataelements':JSON.stringify(this.dataelements), 'endpoints':JSON.stringify(this.endpoints), 'resultname':'resultvar', 'resultvalue':JSON.stringify(this.result),'code':this.statusFalse} , function(err,req,res,obj){ |
| 77: | var ins = obj; |
| 78: | console.log('Instance false: ' + ins); |
| 79: | client.get('/' + ins +'/status', function(err,req,res,obj){ |
| 80: | test.equals(obj,"false", "result should be false"); |
| 81: | test.expect(1); |
| 82: | test.done(); |
| 83: | client.close(); |
| 84: | }); |
| 85: | }); |
| 86: | }, |
| 87: | test3: function (test){ |
| 88: | var client = restify.createStringClient({ |
| 89: | url: 'http://sumatra.pri.univie.ac.at:9292', |
| 90: | version: '*' |
| 91: | }); |
| 92: | client.post('/', {'dataelements':JSON.stringify(this.dataelements), 'endpoints':JSON.stringify(this.endpoints), 'resultname':'resultvar', 'resultvalue':JSON.stringify(this.result),'code':this.statusTrue} , function(err,req,res,obj){ |
| 93: | var ins = obj; |
| 94: | console.log('instance true '+ins); |
| 95: | client.get('/' + ins +'/status', function(err,req,res,obj){ |
| 96: | test.equals(obj,"true", "result should be true"); |
| 97: | test.expect(1); |
| 98: | test.done(); |
| 99: | }); |
| 100: | }); |
| 101: | |
| 102: | }, |
| 103: | test4: function (test){ |
| 104: | var client = restify.createStringClient({ |
| 105: | url: 'http://sumatra.pri.univie.ac.at:9292', |
| 106: | version: '*' |
| 107: | }); |
| 108: | var dataelements=this.dataelements; |
| 109: | client.post('/', {'dataelements':JSON.stringify(this.dataelements), 'endpoints':JSON.stringify(this.endpoints), 'resultname':'resultvar', 'resultvalue':JSON.stringify(this.result),'code':this.statusOther} , function(err,req,res,obj){ |
| 110: | var ins = obj; |
| 111: | console.log("instance other "+ins); |
| 112: | client.get('/' + ins +'/status', function(err,req,res,obj){ |
| 113: | test.equals(obj,50, "result should be "+dataelements.numberA); |
| 114: | test.expect(1); |
| 115: | test.done(); |
| 116: | }); |
| 117: | }); |
| 118: | }, |
| 119: | test5: function (test){ |
| 120: | var client = restify.createStringClient({ |
| 121: | url: 'http://sumatra.pri.univie.ac.at:9292', |
| 122: | version: '*' |
| 123: | }); |
| 124: | client.post('/', {'dataelements':JSON.stringify(this.dataelements), 'endpoints':JSON.stringify(this.endpoints), 'resultname':'resultvar', 'resultvalue':JSON.stringify(this.result),'code':this.correctCode} , function(err,req,res,obj){ |
| 125: | var ins = obj; |
| 126: | console.log("correct code "+ins); |
| 127: | client.get('/' + ins +'/status', function(err,req,res,obj){ |
| 128: | test.equals(obj,100, "result should be 100"); |
| 129: | test.expect(1); |
| 130: | test.done(); |
| 131: | }); |
| 132: | }); |
| 133: | }, |
| 134: | test6: function (test){ |
| 135: | var client = restify.createStringClient({ |
| 136: | url: 'http://sumatra.pri.univie.ac.at:9292', |
| 137: | version: '*' |
| 138: | }); |
| 139: | client.post('/', {'dataelements':JSON.stringify(this.dataelements), 'endpoints':JSON.stringify(this.endpoints)}, function(err,req,res,obj){ |
| 140: | test.equals(obj,"No code", "no code posted"); |
| 141: | test.expect(1); |
| 142: | test.done(); |
| 143: | }); |
| 144: | }, |
| 145: | test7: function (test){ |
| 146: | var client = restify.createStringClient({ |
| 147: | url: 'http://sumatra.pri.univie.ac.at:9292', |
| 148: | version: '*' |
| 149: | }); |
| 150: | client.post('/', {'dataelements':JSON.stringify(this.dataelements), 'endpoints':JSON.stringify(this.endpoints), 'resultname':'resultvar', 'resultvalue':JSON.stringify(this.result),'code':this.errorCode} , function(err,req,res,obj){ |
| 151: | console.log("return: "+obj); |
| 152: | test.equals(obj,"donsole is not defined", "wrong code executed correctly?"); |
| 153: | test.expect(1); |
| 154: | test.done(); |
| 155: | }); |
| 156: | }, |
| 157: | test8: function (test){ |
| 158: | var client = restify.createStringClient({ |
| 159: | url: 'http://sumatra.pri.univie.ac.at:9292', |
| 160: | version: '*' |
| 161: | }); |
| 162: | client.post('/', {'dataelements':JSON.stringify(this.dataelements), 'endpoints':JSON.stringify(this.endpoints), 'resultname':'resultvar', 'resultvalue':JSON.stringify(this.result),'code':this.jsonToXML} , function(err,req,res,obj){ |
| 163: | var ins = obj; |
| 164: | console.log("json to xml "+ins); |
| 165: | client.get('/' + ins + '/status', function(err,req,res,obj){ |
| 166: | test.equals(obj,"\"<xml><data><numberA>100</numberA><content>text</content></data></xml>\"", "result should be "+"\n<xml><data><numberA>100</numberA><content>text</content></data></xml>\n"); |
| 167: | |
| 168: | client.del('/'+ins, function(err,req,res,obj){ |
| 169: | test.equals(res.statusCode,200,"instance could not be deleted"); |
| 170: | test.expect(2); |
| 171: | test.done(); |
| 172: | }); |
| 173: | }); |
| 174: | }); |
| 175: | }, |
| 176: | test9: function (test){ |
| 177: | var server = restify.createServer(); |
| 178: | server.use(restify.queryParser()); |
| 179: | server.use(restify.bodyParser()); |
| 180: | server.listen(9294, function() { |
| 181: | console.log('%s listening at %s', server.name, server.url); |
| 182: | }); |
| 183: | |
| 184: | server.post('/callback', function create(req, res, next) { |
| 185: | var ins = req.params.instance; |
| 186: | console.log("params: "+ins); |
| 187: | test.equals(ins,"error","Code erroneous, therefore instance should be error "); |
| 188: | res.send(200); |
| 189: | server.close(); |
| 190: | test.expect(1); |
| 191: | test.done(); |
| 192: | }); |
| 193: | |
| 194: | var client = restify.createStringClient({ |
| 195: | url: 'http://sumatra.pri.univie.ac.at:9292', |
| 196: | version: '*' |
| 197: | }); |
| 198: | client.post('/', {'dataelements':JSON.stringify(this.dataelements), 'endpoints':JSON.stringify(this.endpoints), 'resultname':'resultvar', 'resultvalue':JSON.stringify(this.result),'code':this.errorCode, 'callback':'http://localhost:9294/callback'} , function(err,req,res,obj){ |
| 199: | var ins = obj; |
| 200: | console.log("instance for erroneous callback: "+ins); |
| 201: | }); |
| 202: | } |
| 203: | |
| 204: | }; |
