我想模拟对调用 api 的快速路由的 curl 请求。我找到了很多关于如何执行此操作的文档,但我一直遇到问题,因为我的代码中有回调。
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;
当我运行当前测试时,出现错误:超时 200 毫秒
这是我的测试,我尝试使用 nock 作为存根服务,任何帮助将不胜感激!!!
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);
});
});
});