【问题标题】:Trigger a test http request触发测试http请求
【发布时间】:2014-09-29 16:23:25
【问题描述】:

出于单元测试的目的,我想在 expressjs 中向我的路由触发一个虚假的 http 请求,以测试每个路由的响应方式。有没有简单的方法可以做到这一点?

【问题讨论】:

  • 您可以使用 Superfish 和 Mocha 相当轻松地做到这一点。

标签: node.js unit-testing express


【解决方案1】:

触发一个真正的请求。它不会使用任何带宽,因为它在同一台机器上

var options = {
    hostname: 'example.com',
    path: '/test'
};

http.request(options).end();

【讨论】:

    【解决方案2】:

    Request.js 是使用 node.js 发送请求的最常见方式。

    var request = require('request');
    request('http://www.google.com', function (error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log(body) // Print the google web page.
      }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      相关资源
      最近更新 更多