node-red-node-test-helper icon indicating copy to clipboard operation
node-red-node-test-helper copied to clipboard

Allow helper to test the URL defined by http in node

Open HirokiUchikawa opened this issue 6 years ago • 3 comments

  • [x] Bugfix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)

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

  • [x] I have read the contribution guidelines
  • [ ] For non-bugfix PRs, I have discussed this change on the mailing list/slack team.
  • [ ] I have run grunt to verify the unit tests pass
  • [ ] I have added suitable unit tests to cover the new/changed functionality

HirokiUchikawa avatar Feb 12 '19 07:02 HirokiUchikawa

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

axgkl avatar Feb 24 '20 16:02 axgkl

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)

szell avatar Apr 13 '21 11:04 szell

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

WS-Daniel avatar Mar 05 '24 17:03 WS-Daniel