node-red / node-red-node-test-helper

A test framework for Node-RED nodes
Apache License 2.0
57 stars 40 forks source link

Allow helper to test the URL defined by http in node #31

Open HirokiUchikawa opened 5 years ago

HirokiUchikawa commented 5 years ago

Proposed changes

Currently helper.request() supports testing editor/admin URL, but it cannot request the URL which defined by http in node. This is because helper.request() does not handle runtime.httpNode which routes a request to the path defined by the node.

I fixed it in order to support runtime.httpNode. This PR will fix #29.

Checklist

axgkl commented 4 years ago

@HirokiUchikawa many thanks!! I wonder how is your patch being used, could you post a demo test spec?

szell commented 3 years ago

Hi,

I tried out this PR with this spec. (merged to 0.2.3 release)

Its works for me

(I have custom HttpIn node )

var helper = require("node-red-node-test-helper");
var httpInNode = require("../httpinauth.js");
helper.init(require.resolve('node-red'));

describe('http in auth Node', function () {

  afterEach(function () {
    helper.unload();
  });

  var flow = [{id:"n1",type:"http in auth",name:"testname",url:"/test",method:"get",upload:false,swaggerDoc:"",
wires:[["n2"]]},{id:"n2",type:"http response",name:"",statusCode:"200",wires:[]}];
    var credentials = {n1: {'apikey': '****'}};
    var b64ApiKey = '****=';

  it('test api without auth header', function (done) {
    helper.load(httpInNode, flow, credentials, function () {
      var n1 = helper.getNode("n1");
      helper.request("httpNode").get('/test').expect(400).end(done);
    });
  });

  it('test api with correct auth header', function (done) {
    helper.load(httpInNode, flow, credentials, function () {
      var n2 = helper.getNode("n2");
      var n1 = helper.getNode("n1");
      helper.request("httpNode").get('/test').set('Authorization', b64ApiKey).expect(200).end(done);
    });
  });

  it('test api with wrong auth header', function (done) {
    helper.load(httpInNode, flow, credentials, function () {
      var n2 = helper.getNode("n2");
      var n1 = helper.getNode("n1");
      helper.request("httpNode").get('/test').set('Authorization', 'differentPassword').expect(401).end(done);
    });
  });
});
√ test api without auth header
√ test api with correct auth header
√ test api with wrong auth header

3 passing (235ms)

WS-Daniel commented 4 months ago

We really need this fix to be merged to test some endpoints in our nodes.