简体   繁体   English

用node / js在第三方js库中模拟HTTP请求的最简单或惯用的方法是什么

[英]What's the easiest or idiomatic way to mock HTTP requests in 3rd party js libraries with node / js

When I've coded in Ruby or Python, I've been able to use libraries like VCR that intercept HTTP requests, and record them, so when for example I'm hitting an 3rd party API in tests, I can save that response as a fixture instead of manually building a huge mock objects to check behaviour against. 当我用Ruby或Python进行编码时,我已经能够使用VCR之类的库来拦截HTTP请求并记录它们,因此,例如,当我在测试中点击第3方API时,可以将该响应保存为夹具,而不是手动构建庞大的模拟对象来检查行为。

It's not perfect, but it has saved a load of time when I've been exploring which API requests to make against a third party API, (often wrapping a 3rd party library), then writing tests to check this behaviour. 这并不完美,但是当我一直在探索针对第三方API提出哪些API请求(通常包装第三方库),然后编写测试以检查此行为时,它节省了很多时间。

What's the closest thing in JS these days to this? 如今,JS中最接近的东西是什么?

I'm looking for an open source tool I can require in my test files, so when I run tests where I might call methods on third party APIs, I don't make expensive, slow HTTP requests. 我正在寻找测试文件中可能require开源工具,因此,当我运行可能在第三方API上调用方法的测试时,我不会发出昂贵而缓慢的HTTP请求。 I imagine the code might look a bit like: 我认为代码可能看起来像:

it('does something I expect it to', () {

  // set up some state I care about
  let someVar = someSetupCode()

  let library = thirdPartyLib({creds: 'somecreds'})
  library.someMethod()

  // check state has changed 
  expect(someVar.value).toBe('what I Expect after calling someMethod')

})

Where here, when I call library.someMethod() , instead of hitting actual servers, I'm checking against the values the server would be returning, that I've saved previously. 在这里,当我调用library.someMethod() ,我没有检查实际的服务器,而是对照服务器将要返回的值进行检查,这些值是我之前保存的。

Monkey patching an existing library or function 猴子修补现有库或函数

I see things fetch-vcr , or axios-vcr , but these seem to rely on explicitly reaching into a library to replace say, a call to fetch with the http-intercepting version instead, be reading a 'cassette' file containing the canned response. 我看到的东西是fetch-vcraxios-vcr ,但是这些似乎依赖于显式地进入库以替换说,例如,使用http-intercepting版本调用fetch ,正在读取包含罐头响应的“盒式”文件。

I'm looking for a way to avoid patching 3rd party code if I can help it, a this is how I understand VCR works for other languages. 我正在寻找一种方法来避免打补丁第三方代码,如果我能帮忙的话,这就是我了解VCR适用于其他语言的方式。

Presumably, if there's an HTTP client built somewhere into node then that would be the place you'd patch a function - I haven't come across a specific library that does this. 大概,如果在节点的某个位置内置了HTTP客户端,那么这就是您要修补功能的地方-我还没有遇到执行此操作的特定库。

Running an entire HTTP server 运行整个HTTP服务器

Alternatively I can see libraries like vcr.js , or yakbak , which essentially set up an HTTP server which serves JSON blobs you define, at various urls, like serving a saved users.json file at http://localhost:8100/users/ 或者,我可以看到vcr.jsyakbak之类的库,它们实际上是在各种URL上设置了一个HTTP服务器,该服务器可为您定义的JSON Blob提供服务,例如在http://localhost:8100/users/处提供已保存的users.json文件。

This is okay, but again, if I don't need to spin up a whole HTTP server, and make actual HTTP requests, that would be wonderful. 没关系,但是如果我不需要启动整个HTTP服务器并发出实际的HTTP请求,那就太好了。

Oh, hang on, it looks like sepia from linkedin works well for nodejs at least. 哦,等等,看来来自Linkedin的棕褐色至少对nodejs效果很好。

I haven't looked into it too much, but I'd welcome comments if you have been using it. 我还没有研究太多,但是如果您一直在使用它,我欢迎您提出意见。

Probably SoapUI works for you. SoapUI可能为您工作。 Although its name, it also works with REST API. 尽管是它的名称,但它也可以与REST API一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM