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;

当我运行当前测试时,出现错误:超时 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);
    });
  });
});
4

1 回答 1

0

所以经过更多的挖掘,我们想通了。我们完全改变了我们的代码并使用了这个很棒的 npm(节点包管理器)

request-promise gem...(确保将“json:true”添加到选项中

var express = require('express');
var router = express.Router();
var request = require('request-promise')


router.get('/', function(req, res, next) {
  res.render('index')
});

router.get('/data', function(req, res, next){
  var options = {
    uri: 'http://10.244.68.3:8500/v1/catalog/node/services',
    json: true
};
  request(options).then(function(result){
    res.send(result)
  }).catch(function(err){
    res.send(err)
  })
 })


module.exports = router;

下面是我们如何创建测试来命中这条路线(“/data”),创建一个模拟并返回我们所期望的

var expect = require("chai").expect;
var nock = require("nock");
var app = require('../../../app');
var request = require("supertest")

var consulResponse

describe("Consul API Queries", () => {
  beforeEach(() => {
    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", () => {
    var scope = nock("http://10.244.68.3:8500")
        .get('/v1/catalog/node/services')
        .reply(200, "no way are we getting this to work");
    expect(scope.interceptors[0].statusCode).to.equal(200);
  });


  it("gets the data from consul", (done) => {
    request(app).get('/data')
        .expect(consulResponse, done)

  })

});

问题是 request 没有按原样提交承诺,因此您需要 request-promise gem 来实现这一点。一旦你得到这个,你应该很好去!

于 2016-09-20T21:23:35.653 回答