0

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;

現在のテストを実行すると、Error: timeout of 200ms が表示されます

これが私のテストです。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);
    });
  });
});
4

1 に答える 1