pollyjs

pollyjs

灵活强大的HTTP请求和响应记录、重放和模拟工具

Polly.JS是一个用于记录、重放和模拟HTTP请求和响应的JavaScript库。它支持Node和浏览器环境,提供拦截、传递和事件附加功能,可将数据保存到磁盘或本地存储。通过利用多个请求API,Polly.JS能以简单直观的方式模拟HTTP交互,还可调整时间流速,为开发人员提供了强大的测试工具。这个工具特别适用于自动化测试、API调试以及模拟各种网络条件下的应用表现。

Polly.JSHTTP交互测试工具记录重放模拟请求Github开源项目
<p align="center"> <img alt="Polly.JS" width="400px" src="https://netflix.github.io/pollyjs/assets/images/wordmark-logo-alt.png" /> </p> <h2 align="center">Record, Replay, and Stub HTTP Interactions</h2>

Build Status license

Polly.JS is a standalone, framework-agnostic JavaScript library that enables recording, replaying, and stubbing of HTTP interactions. By tapping into multiple request APIs across both Node & the browser, Polly.JS is able to mock requests and responses with little to no configuration while giving you the ability to take full control of each request with a simple, powerful, and intuitive API.

Interested in contributing or just seeing Polly in action? Head over to CONTRIBUTING.md to learn how to spin up the project!

Why Polly?

Keeping fixtures and factories in parity with your APIs can be a time consuming process. Polly alleviates this process by recording and maintaining actual server responses while also staying flexible.

  • Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
  • Use Polly's client-side server to modify or intercept requests and responses to simulate different application states (e.g. loading, error, etc.).

Features

  • 🚀 Node & Browser Support
  • ⚡️️ Simple, Powerful, & Intuitive API
  • 💎 First Class Mocha & QUnit Test Helpers
  • 🔥 Intercept, Pass-Through, and Attach Events
  • 📼 Record to Disk or Local Storage
  • ⏱ Slow Down or Speed Up Time

Getting Started

Check out the Quick Start documentation to get started.

Usage

Let's take a look at what an example test case would look like using Polly.

import { Polly } from '@pollyjs/core'; import XHRAdapter from '@pollyjs/adapter-xhr'; import FetchAdapter from '@pollyjs/adapter-fetch'; import RESTPersister from '@pollyjs/persister-rest'; /* Register the adapters and persisters we want to use. This way all future polly instances can access them by name. */ Polly.register(XHRAdapter); Polly.register(FetchAdapter); Polly.register(RESTPersister); describe('Netflix Homepage', function () { it('should be able to sign in', async function () { /* Create a new polly instance. Connect Polly to both fetch and XHR browser APIs. By default, it will record any requests that it hasn't yet seen while replaying ones it has already recorded. */ const polly = new Polly('Sign In', { adapters: ['xhr', 'fetch'], persister: 'rest' }); const { server } = polly; /* Intercept all Google Analytic requests and respond with a 200 */ server .get('/google-analytics/*path') .intercept((req, res) => res.sendStatus(200)); /* Pass-through all GET requests to /coverage */ server.get('/coverage').passthrough(); /* start: pseudo test code */ await visit('/login'); await fillIn('email', 'polly@netflix.com'); await fillIn('password', '@pollyjs'); await submit(); /* end: pseudo test code */ expect(location.pathname).to.equal('/browse'); /* Calling `stop` will persist requests as well as disconnect from any connected browser APIs (e.g. fetch or XHR). */ await polly.stop(); }); });

The above test case would generate the following HAR file which Polly will use to replay the sign-in response when the test is rerun:

{ "log": { "_recordingName": "Sign In", "browser": { "name": "Chrome", "version": "67.0" }, "creator": { "name": "Polly.JS", "version": "0.5.0", "comment": "persister:rest" }, "entries": [ { "_id": "06f06e6d125cbb80896c41786f9a696a", "_order": 0, "cache": {}, "request": { "bodySize": 51, "cookies": [], "headers": [ { "name": "content-type", "value": "application/json; charset=utf-8" } ], "headersSize": 97, "httpVersion": "HTTP/1.1", "method": "POST", "postData": { "mimeType": "application/json; charset=utf-8", "text": "{\"email\":\"polly@netflix.com\",\"password\":\"@pollyjs\"}" }, "queryString": [], "url": "https://netflix.com/api/v1/login" }, "response": { "bodySize": 0, "content": { "mimeType": "text/plain; charset=utf-8", "size": 0 }, "cookies": [], "headers": [], "headersSize": 0, "httpVersion": "HTTP/1.1", "redirectURL": "", "status": 200, "statusText": "OK" }, "startedDateTime": "2018-06-29T17:31:55.348Z", "time": 11, "timings": { "blocked": -1, "connect": -1, "dns": -1, "receive": 0, "send": 0, "ssl": -1, "wait": 11 } } ], "pages": [], "version": "1.2" } }

Prior Art

The "Client Server" API of Polly is heavily influenced by the very popular mock server library pretender. Pretender supports XHR and Fetch stubbing and is a great lightweight alternative to Polly if your project does not require persisting capabilities or Node adapters.

Thank you to all contributors especially the maintainers: trek, stefanpenner, and xg-wang.

Contributors

<a href="https://github.com/offirgolan"><img src="https://avatars.githubusercontent.com/u/575938?v=4" title="offirgolan" width="80" height="80"></a> <a href="https://github.com/jasonmit"><img src="https://avatars.githubusercontent.com/u/3108309?v=4" title="jasonmit" width="80" height="80"></a> <a href="https://github.com/cibernox"><img src="https://avatars.githubusercontent.com/u/265339?v=4" title="cibernox" width="80" height="80"></a> <a href="https://github.com/DenrizSusam"><img src="https://avatars.githubusercontent.com/u/39295979?v=4" title="DenrizSusam" width="80" height="80"></a> <a href="https://github.com/dustinsoftware"><img src="https://avatars.githubusercontent.com/u/942358?v=4" title="dustinsoftware" width="80" height="80"></a> <a href="https://github.com/silverchen"><img src="https://avatars.githubusercontent.com/u/6683103?v=4" title="silverchen" width="80" height="80"></a> <a href="https://github.com/tombh"><img src="https://avatars.githubusercontent.com/u/160835?v=4" title="tombh" width="80" height="80"></a> <a href="https://github.com/zkwentz"><img src="https://avatars.githubusercontent.com/u/4832?v=4" title="zkwentz" width="80" height="80"></a> <a href="https://github.com/agraves"><img src="https://avatars.githubusercontent.com/u/46964?v=4" title="agraves" width="80" height="80"></a> <a href="https://github.com/brandon-leapyear"><img src="https://avatars.githubusercontent.com/u/27799541?v=4" title="brandon-leapyear" width="80" height="80"></a> <a href="https://github.com/swashcap"><img src="https://avatars.githubusercontent.com/u/1858316?v=4" title="swashcap" width="80" height="80"></a> <a href="https://github.com/justsml"><img src="https://avatars.githubusercontent.com/u/397632?v=4" title="justsml" width="80" height="80"></a> <a href="https://github.com/DanielRuf"><img src="https://avatars.githubusercontent.com/u/827205?v=4" title="DanielRuf" width="80" height="80"></a> <a href="https://github.com/dciccale"><img src="https://avatars.githubusercontent.com/u/539546?v=4" title="dciccale" width="80" height="80"></a> <a href="https://github.com/ericclemmons"><img src="https://avatars.githubusercontent.com/u/15182?v=4" title="ericclemmons" width="80" height="80"></a> <a href="https://github.com/jamesgeorge007"><img src="https://avatars.githubusercontent.com/u/25279263?v=4" title="jamesgeorge007" width="80" height="80"></a> <a href="https://github.com/woodenconsulting"><img src="https://avatars.githubusercontent.com/u/4163029?v=4" title="woodenconsulting" width="80" height="80"></a> <a href="https://github.com/feinoujc"><img src="https://avatars.githubusercontent.com/u/1733707?v=4" title="feinoujc" width="80" height="80"></a> <a href="https://github.com/josex2r"><img src="https://avatars.githubusercontent.com/u/3719730?v=4" title="josex2r" width="80" height="80"></a> <a href="https://github.com/jomaxx"><img src="https://avatars.githubusercontent.com/u/2747424?v=4" title="jomaxx" width="80" height="80"></a> <a href="https://github.com/karlhorky"><img src="https://avatars.githubusercontent.com/u/1935696?v=4" title="karlhorky" width="80" height="80"></a> <a href="https://github.com/kennethlarsen"><img src="https://avatars.githubusercontent.com/u/1408595?v=4" title="kennethlarsen" width="80" height="80"></a> <a href="https://github.com/poteto"><img src="https://avatars.githubusercontent.com/u/1390709?v=4" title="poteto" width="80" height="80"></a> <a href="https://github.com/fastfrwrd"><img src="https://avatars.githubusercontent.com/u/231133?v=4" title="fastfrwrd" width="80" height="80"></a> <a href="https://github.com/rwd"><img src="https://avatars.githubusercontent.com/u/218337?v=4" title="rwd" width="80" height="80"></a> <a href="https://github.com/rmachielse"><img src="https://avatars.githubusercontent.com/u/2470404?v=4" title="rmachielse" width="80" height="80"></a> <a href="https://github.com/ROpdebee"><img src="https://avatars.githubusercontent.com/u/15186467?v=4" title="ROpdebee" width="80" height="80"></a> <a href="https://github.com/gribnoysup"><img src="https://avatars.githubusercontent.com/u/5036933?v=4" title="gribnoysup" width="80" height="80"></a> <a href="https://github.com/shriyash"><img src="https://avatars.githubusercontent.com/u/4494915?v=4" title="shriyash" width="80" height="80"></a>

License

Copyright (c) 2018 Netflix, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

编辑推荐精选

扣子-AI办公

扣子-AI办公

职场AI,就用扣子

AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!

堆友

堆友

多风格AI绘画神器

堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。

图像生成热门AI工具AI图像AI反应堆AI工具箱AI绘画GOAI艺术字堆友相机
码上飞

码上飞

零代码AI应用开发平台

零代码AI应用开发平台,用户只需一句话简单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。

Vora

Vora

免费创建高清无水印Sora视频

Vora是一个免费创建高清无水印Sora视频的AI工具

Refly.AI

Refly.AI

最适合小白的AI自动化工作流平台

无需编码,轻松生成可复用、可变现的AI自动化工作流

酷表ChatExcel

酷表ChatExcel

大模型驱动的Excel数据处理工具

基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。

AI工具使用教程AI营销产品酷表ChatExcelAI智能客服
TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

热门AI工具生产力协作转型TraeAI IDE
AIWritePaper论文写作

AIWritePaper论文写作

AI论文写作指导平台

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

数据安全AI助手热门AI工具AI辅助写作AI论文工具论文写作智能生成大纲
博��思AIPPT

博思AIPPT

AI一键生成PPT,就用博思AIPPT!

博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。

热门AI工具AI办公办公工具智能排版AI生成PPT博思AIPPT海量精品模板AI创作
潮际好麦

潮际好麦

AI赋能电商视觉革命,一站式智能商拍平台

潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。

下拉加载更多