1

I'm trying to test a node API in mocha, but I get no response from the API request that works fine in browser.

Here is the response in browser:

Response in browser: {"succes":true}

And this is the API test that is not working:

var request = require("request");
var expect = require("chai").expect;

describe("API TEST", function(){
    it("Should return true", function(done){

        request.get({ url: "https://skilltree.benis.hu/apitest" },
            function(error, response, body) {
                    console.log(response);
                done();
            });
    });
});

Result:

API TEST -> undefined (response=undefined)

But the testing syntax seems fine, since it works with a public API:

describe("API TEST", function(){
        it("Should return true", function(done){

        request.get({ url: "https://anapioficeandfire.com/api/characters/583" },
            function(error, response, body) {

                    console.log(response);
                done();
            });
    });
});

Result: Result was a bunch of response data.

Tried to test it in POSTMAN, MIGHT have found the issue, not sure: Couldn't get response.

Possible solutions:

Turn off ssl certificate verification <- did this one Turned off SSL Certificate verification in postman, and it worked:

Response was the same as in browser: {"succes":true}

I tried to find a way to disable it in code too for testing, but I had no luck finding one. This is pretty much the first time I am testing anything.

bluetoothfx
  • 643
  • 9
  • 23

1 Answers1

1

Your assumption is correct in that it is a certificate verification related issue. I believe this should work:

 it.only("Should return true", function(done){
     process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
     request.get({ url: "https://skilltree.benis.hu/apitest" },
     function(error, response) {
       console.log(response.body);
       done();
     });
 });

When I run it, I get {"succes":true}.

Note, this is for demo purposes only, the process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; shouldn't be part of every test, but rather defined at a higher level, so that it only runs once.

Mo A
  • 717
  • 1
  • 8
  • 19