Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2014 IBM Corp. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | **/ |
| 16 | |
| 17 | var should = require("should"); |
| 18 | var when = require("when"); |
| 19 | var request = require('supertest'); |
| 20 | var nock; |
| 21 | if (!process.version.match(/^v0\.[0-9]\./)) { |
| 22 | // only set nock for node >= 0.10 |
| 23 | try { |
| 24 | nock = require('nock'); |
| 25 | } catch (err) { |
| 26 | // nevermind, will skip nock tests |
| 27 | nock = null; |
| 28 | } |
| 29 | } |
| 30 | var RED = require("../../red/red.js"); |
| 31 | var redNodes = require("../../red/nodes"); |
| 32 | var flows = require("../../red/nodes/flows"); |
| 33 | var credentials = require("../../red/nodes/credentials"); |
| 34 | var comms = require("../../red/comms.js"); |
| 35 | |
| 36 | var http = require('http'); |
| 37 | var express = require('express'); |
| 38 | var app = express(); |
| 39 | |
| 40 | var address = '127.0.0.1'; |
| 41 | var listenPort = 0; // use ephemeral port |
| 42 | var port; |
| 43 | var url; |
| 44 | |
| 45 | var server; |
| 46 | |
| 47 | function helperNode(n) { |
| 48 | RED.nodes.createNode(this, n); |
| 49 | } |
| 50 | |
| 51 | module.exports = { |
| 52 | load: function(testNode, testFlows, testCredentials, cb) { |
| 53 | if (typeof testCredentials === 'function') { |
| 54 | cb = testCredentials; |
| 55 | testCredentials = {}; |
| 56 | } |
| 57 | |
| 58 | var storage = { |
| 59 | getFlows: function() { |
| 60 | var defer = when.defer(); |
| 61 | defer.resolve(testFlows); |
| 62 | return defer.promise; |
| 63 | }, |
| 64 | getCredentials: function() { |
| 65 | var defer = when.defer(); |
| 66 | defer.resolve(testCredentials); |
| 67 | return defer.promise; |
| 68 | }, |
| 69 | saveCredentials: function() { |
| 70 | // do nothing |
| 71 | }, |
| 72 | }; |
| 73 | var settings = { |
| 74 | available: function() { return false; } |
| 75 | }; |
| 76 | |
| 77 | redNodes.init(settings, storage); |
| 78 | credentials.init(storage); |
| 79 | RED.nodes.registerType("helper", helperNode); |
| 80 | testNode(RED); |
| 81 | flows.load().then(function() { |
| 82 | should.deepEqual(testFlows, flows.getFlows()); |
| 83 | cb(); |
| 84 | }); |
| 85 | }, |
| 86 | unload: function() { |
| 87 | // TODO: any other state to remove between tests? |
| 88 | redNodes.clearRegistry(); |
| 89 | return flows.stopFlows(); |
| 90 | }, |
| 91 | |
| 92 | getNode: function(id) { |
| 93 | return flows.get(id); |
| 94 | }, |
| 95 | |
| 96 | credentials: credentials, |
| 97 | |
| 98 | clearFlows: function() { |
| 99 | return flows.clear(); |
| 100 | }, |
| 101 | |
| 102 | request: function() { |
| 103 | return request(RED.httpAdmin); |
| 104 | }, |
| 105 | |
| 106 | startServer: function(done) { |
| 107 | server = http.createServer(function(req,res){app(req,res);}); |
| 108 | RED.init(server, {}); |
| 109 | server.listen(listenPort, address); |
| 110 | server.on('listening', function() { |
| 111 | port = server.address().port; |
| 112 | url = 'http://' + address + ':' + port; |
| 113 | comms.start(); |
| 114 | done(); |
| 115 | }); |
| 116 | }, |
| 117 | //TODO consider saving TCP handshake/server reinit on start/stop/start sequences |
| 118 | stopServer: function(done) { |
| 119 | if(server) { |
| 120 | server.close(done); |
| 121 | } |
| 122 | }, |
| 123 | |
| 124 | url: function() { return url; }, |
| 125 | |
| 126 | nock: nock, |
| 127 | |
| 128 | }; |