当前位置: 首页 > 工具软件 > Nock > 使用案例 >

nock模拟服务响应_使用Node.js nock拦截HTTP请求

谭伟
2023-12-01

nock模拟服务响应

Unit testing external APIs is difficult no matter what language you do it in.  Hell, working with any external API is scary, if only because you have zero control of networking issues, API changes, and a host of other issues.  But if you do create a service which relies on another host's data, it's still important to create unit tests that rely on the other service.

无论使用哪种语言,都很难对单元API进行单元测试。使用任何外部API的地狱都是令人恐惧的,仅是因为您对网络问题,API更改以及许多其他问题没有任何控制。 但是,如果您确实创建了依赖于另一台主机数据的服务,则创建依赖于另一台服务的单元测试仍然很重要。

If you're using a third party service, creating unit tests is really tough...until you discover nock, a node module which intercepts requests and allows you to respond to them as you wish, including sending back custom response codes and payloads.  Let me show you how to use nock!

如果您使用的是第三方服务,创建单元测试非常困难……直到发现nock为止,它是一个节点模块,该模块拦截请求并允许您根据需要进行响应,包括发送回自定义响应代码和有效负载。 让我告诉你如何使用nock!

变态 (Getting nock)

Like every node package, you have to install it first:

像每个节点程序包一样,您必须先安装它:


npm install nock

Once installed, required it in your script:

安装后,在脚本中需要它:


var nock = require('nock');

That's the setup.

这就是设置。

使用诺克 (Using nock)

The most basic usage of nock is intercepting a GET request to a given URL:

nock的最基本用法是拦截对给定URL的GET请求:


nock('https://davidwalsh.name')
	.get('/users/22').reply(200, {
		username: 'davidwalshblog',
		firstname: 'David'
	});

nock('https://davidwalsh.name')
	.get('/content/homepage')
	.reply(200, 'This is the HTML for the homepage');

nock('https://davidwalsh.name')
	.get('/content/page-no-exist')
	.reply(404, 'This page could not be found');

The example above intercepts a GET request to a given host + path and responds with a desired response code and contents.  You can intercept POST requests as well:

上面的示例截取到给定主机+路径的GET请求,并以所需的响应代码和内容进行响应。 您也可以拦截POST请求:


nock('https://davidwalsh.name')
	.post('/users/detail')
	.reply(200, {
		firstname: 'David'
	});

You can also narrow down GET and POST matches by setting the data or query strings:

您还可以通过设置数据或查询字符串来缩小GET和POST的匹配范围:


nock('https://davidwalsh.name')
	.post('/users/detail', { username: 'davidwalshblog' })
	.reply(200, {
		firstname: 'David'
	});

If responding with given headers is important, you can do that too:

如果使用给定的标题响应很重要,您也可以这样做:


var scope = nock('https://davidwalsh.name')
	.get('/')
	.reply(200, 'Hello World!', {
		'X-My-Headers': 'My Header value'
	});

If you want to do some advanced processing logic before responding to the request, you can reply with a function instead:

如果您想在响应请求之前执行一些高级处理逻辑,则可以使用函数来代替:


nock('https://davidwalsh.name')
	.post('/users/detail', { username: 'davidwalshblog' })
	.reply(function() {

		// Some logic

		return [200, resultingContent];
	});

So why is all of this important?  If you do any service-based testing within Node.js, including anything from HTTP to local db/service testing, you'll be desperate for a utility that can intercept real requests instead of attempting to monkey patch request methods or use other gross workarounds. In short: your app can function as usual during testing, nock intercepts those requests and throws back what you want!

那么,为什么所有这些都很重要? 如果您在Node.js中进行任何基于服务的测试,包括从HTTP到本地db / service测试的任何测试,您将迫切需要一个实用程序,该实用程序可以拦截实际请求,而不是尝试伪造补丁程序请求方法或使用其他总体解决方法。 简而言之:您的应用在测试过程中可以正常运行,nock截获这些请求并返回您想要的内容!

And what's awesome about nock?  This post touches the very basics of nock.  For a project I'm working on called Discord, I've created a test suite which runs off of tests recorded by...nock.  I created a recorded which saved requests and their responses, saving us loads of manual labor.

那Nock有什么很棒的呢? 这篇文章触及了nock的基础知识。 对于我正在研究的一个名为Discord的项目, 我创建了一个测试套件,该套件使用了... nock 记录的测试 。 我创建了一个记录,该记录保存了请求及其响应,从而节省了我们大量的体力劳动。

Get to know nock!  And thank me on Twitter when you're done!

认识诺克! 完成后, 在Twitter上感谢我

翻译自: https://davidwalsh.name/nock

nock模拟服务响应

 类似资料: