私は、curlリクエストを、apiを呼び出すエクスプレスルートに模擬したいと思っています。私はこれを行う方法について多くの文書を見つけましたが、私のコードにコールバックがあるので問題が発生しています。私は私の現在のテストを実行するとそのルートがコールバックを持っているExpressルートをテストする
var request = require('request')
function queryConsul(req, res) {
var options = {
url: 'http://10.244.68.3:8500/v1/catalog/node/services'
};
request(options, callback)
function callback(error, response, body) {
console.log("hola?");
if (!error && response.statusCode == 200) {
response=body
}
res.send(response)
}
}
module.exports = queryConsul;
が、私はエラーを取得:200msの
のタイムアウトがここに私のテストで、私はスタブサービスとしてノックを使用しようとした、任意の助けいただければ幸いです! !
var queryConsul = require("../../../helper/queryConsulService");
var expect = require("chai").expect;
var nock = require("nock");
describe("Consul API Queries",() => {
beforeEach(() => {
var consulResponse =
{
"Node": {
"Node": "Services",
"Address": "some_address",
"TaggedAddresses": null,
"CreateIndex": 72389,
"ModifyIndex": 72819
},
"Services": {
"OneBitesTheDust": {
"ID": "OneBitesTheDust",
"Service": "OneBitesTheDust",
"Tags": ["{\"Type\" : \"service type\", \"Description\": \"ddfadf\"}"],
"Address": "www.google.com",
"Port": 80,
"EnableTagOverride": false,
"CreateIndex": 72819,
"ModifyIndex": 72819
},
"anotherOneBitesTheDust": {
"ID": "anotherOneBitesTheDust",
"Service": "anotherOneBitesTheDust",
"Tags": ["{\"Type\" : \"service type\", \"Description\": \"ddfadf\"}"],
"Address": "www.google.com",
"Port": 80,
"EnableTagOverride": false,
"CreateIndex": 72465,
"ModifyIndex": 72465
},
"newService": {
"ID": "newService",
"Service": "newService",
"Tags": ["{\"Type\" : \"service type\", \"Description\": \"ddfadf\"}"],
"Address": "www.google.com",
"Port": 80,
"EnableTagOverride": false,
"CreateIndex": 72389,
"ModifyIndex": 72389
}
}
}
nock("http://10.244.68.3:8500")
.get('/v1/catalog/node/services')
.reply(200, consulResponse);
});
it("returns a status code of 200 when the services domain is queried", function(done) {
queryConsul(function(err, res){
console.log(res);
expect(res.statusCode).to.equal(200, done);
});
});
});