2 * Copyright 2014 IBM Corp.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 var should = require("should");
18 var when = require("when");
19 var request = require('supertest');
21 if (!process.version.match(/^v0\.[0-9]\./)) {
22 // only set nock for node >= 0.10
24 nock = require('nock');
26 // nevermind, will skip nock tests
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");
36 var http = require('http');
37 var express = require('express');
40 var address = '127.0.0.1';
41 var listenPort = 0; // use ephemeral port
47 function helperNode(n) {
48 RED.nodes.createNode(this, n);
52 load: function(testNode, testFlows, testCredentials, cb) {
53 if (typeof testCredentials === 'function') {
59 getFlows: function() {
60 var defer = when.defer();
61 defer.resolve(testFlows);
64 getCredentials: function() {
65 var defer = when.defer();
66 defer.resolve(testCredentials);
69 saveCredentials: function() {
74 available: function() { return false; }
77 redNodes.init(settings, storage);
78 credentials.init(storage);
79 RED.nodes.registerType("helper", helperNode);
81 flows.load().then(function() {
82 should.deepEqual(testFlows, flows.getFlows());
87 // TODO: any other state to remove between tests?
88 redNodes.clearRegistry();
89 return flows.stopFlows();
92 getNode: function(id) {
96 credentials: credentials,
98 clearFlows: function() {
102 request: function() {
103 return request(RED.httpAdmin);
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;
117 //TODO consider saving TCP handshake/server reinit on start/stop/start sequences
118 stopServer: function(done) {
124 url: function() { return url; },