Node.js的HTTP模拟和预期测试库:Nock

jopen 10年前

Nock 是一个Node.js 平台的 HTTP 模拟和预期测试库 。可用于在隔离的情况下测试执行HTTP请求模块。

For instance, if a module performs HTTP requests to a CouchDB server or makes HTTP requests to the Amazon API, you can test that module in isolation.


示例代码:

var nock = require('nock');    var couchdb = nock('http://myapp.iriscouch.com')                  .get('/users/1')                  .reply(200, {                    _id: '123ABC',                    _rev: '946B7D1C',                    username: 'pgte',                    email: 'pedro.teixeira@gmail.com'                   });

This setup says that we will intercept every HTTP call tohttp://myapp.iriscouch.com.

It will intercept an HTTP GET request to '/users/1' and reply with a status 200, and the body will contain a user representation in JSON.

项目主页:http://www.open-open.com/lib/view/home/1425977756388