0
我想嘲笑一個捲曲請求到一個調用api的快速路由。我發現了很多關於如何做到這一點的文檔,但我一直有問題,因爲我的代碼中有一個回調。測試快速路由,該路由有回調
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);
});
});
});