{"id":18926932,"url":"https://github.com/bedrockstreaming/superagent-mock","last_synced_at":"2025-08-02T13:39:18.489Z","repository":{"id":29334317,"uuid":"32868077","full_name":"BedrockStreaming/superagent-mock","owner":"BedrockStreaming","description":"superagent plugin allowing to simulate HTTP calls by returning data fixtures based on the requested URL","archived":false,"fork":false,"pushed_at":"2023-10-18T10:00:37.000Z","size":1060,"stargazers_count":172,"open_issues_count":20,"forks_count":42,"subscribers_count":33,"default_branch":"master","last_synced_at":"2025-07-24T23:02:22.503Z","etag":null,"topics":["javascript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BedrockStreaming.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-03-25T14:02:00.000Z","updated_at":"2025-05-21T17:15:02.000Z","dependencies_parsed_at":"2024-06-18T13:31:54.901Z","dependency_job_id":"2b163e82-7ed1-45fc-829a-3bf8e0cd0c49","html_url":"https://github.com/BedrockStreaming/superagent-mock","commit_stats":null,"previous_names":["m6web/superagent-mock"],"tags_count":25,"template":false,"template_full_name":null,"purl":"pkg:github/BedrockStreaming/superagent-mock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BedrockStreaming%2Fsuperagent-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BedrockStreaming%2Fsuperagent-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BedrockStreaming%2Fsuperagent-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BedrockStreaming%2Fsuperagent-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BedrockStreaming","download_url":"https://codeload.github.com/BedrockStreaming/superagent-mock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BedrockStreaming%2Fsuperagent-mock/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268400582,"owners_count":24244445,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript"],"created_at":"2024-11-08T11:17:33.160Z","updated_at":"2025-08-02T13:39:18.446Z","avatar_url":"https://github.com/BedrockStreaming.png","language":"JavaScript","readme":"![npm](https://img.shields.io/npm/v/superagent-mock)\n![npm bundle size](https://img.shields.io/bundlephobia/minzip/superagent-mock)\n![npm](https://img.shields.io/npm/dy/superagent-mock)\n![GitHub last commit](https://img.shields.io/github/last-commit/M6web/superagent-mock)\n![NPM](https://img.shields.io/npm/l/superagent-mock)\n[![NPM Downloads](http://img.shields.io/npm/dm/superagent-mock.svg)](https://www.npmjs.org/package/superagent-mock)\n[![Continous Integration](https://github.com/M6Web/superagent-mock/actions/workflows/node.js.yml/badge.svg)](https://github.com/M6Web/superagent-mock/actions/workflows/node.js.yml)\n\n\u003cp align=\"center\"\u003e\n\u003cb\u003e\u003ca href=\"#installation\"\u003eInstallation\u003c/a\u003e\u003c/b\u003e\n|\n\u003cb\u003e\u003ca href=\"#usage\"\u003eUsage\u003c/a\u003e\u003c/b\u003e\n|\n\u003cb\u003e\u003ca href=\"#supported-methods\"\u003eSupported Methods\u003c/a\u003e\u003c/b\u003e\n|\n\u003cb\u003e\u003ca href=\"#credits\"\u003eCredits\u003c/a\u003e\u003c/b\u003e\n|\n\u003cb\u003e\u003ca href=\"#license\"\u003eLicense\u003c/a\u003e\u003c/b\u003e\n\u003c/p\u003e\n\n# superagent-mock\n\n[superagent](https://github.com/visionmedia/superagent) plugin allowing to simulate HTTP calls by returning data fixtures based on the requested URL.\n\nSee [this post](https://tech.bedrockstreaming.com//how-did-we-mock-the-backend-developers.html) to know why we use superagent-mock at Bedrock Streaming.\n\n## Installation\n\nInstall with [npm](http://npmjs.org/): `npm install superagent-mock`\n\nInstall with [yarn](https://yarnpkg.com/): `yarn add superagent-mock`\n\n## Requirements\n\nnode \u003e= 8.0\nsuperagent \u003e= ^3.6.0\n\n## Usage\n\nFirst, you have to define the URLs to mock in a configuration file:\n\n```js\n// ./superagent-mock-config.js file\nmodule.exports = [\n  {\n    /**\n     * regular expression of URL\n     */\n    pattern: 'https://domain.example(.*)',\n\n    /**\n     * returns the data\n     *\n     * @param match array Result of the resolution of the regular expression\n     * @param params object sent by 'send' function\n     * @param headers object set by 'set' function\n     * @param context object the context of running the fixtures function\n     */\n    fixtures: function (match, params, headers, context) {\n      /**\n       * Returning error codes example:\n       *   request.get('https://domain.example/404').end(function(err, res){\n       *     console.log(err); // 404\n       *     console.log(res.notFound); // true\n       *   })\n       */\n      if (match[1] === '/404') {\n        throw new Error(404);\n      }\n\n      /**\n       * Checking on parameters example:\n       *   request.get('https://domain.example/hero').send({superhero: \"superman\"}).end(function(err, res){\n       *     console.log(res.body); // \"Your hero: superman\"\n       *   })\n       */\n\n      if (match[1] === '/hero') {\n        if(params['superhero']) {\n          return 'Your hero:' + params['superhero'];\n        } else {\n          return 'You didnt choose a hero';\n        }\n      }\n\n\n      /**\n       * Checking on headers example:\n       *   request.get('https://domain.example/authorized_endpoint').set({Authorization: \"9382hfih1834h\"}).end(function(err, res){\n       *     console.log(res.body); // \"Authenticated!\"\n       *   })\n       */\n\n      if (match[1] === '/authorized_endpoint') {\n        if(headers['Authorization']) {\n          return 'Authenticated!';\n        } else {\n          throw new Error(401); // Unauthorized\n        }\n      }\n\n      /**\n       * Cancelling the mocking for a specific matched route example:\n       *   request.get('https://domain.example/server_test').end(function(err, res){\n       *     console.log(res.body); // (whatever the actual server would have returned)\n       *   })\n       */\n\n      if (match[1] === '/server_test') {\n        context.cancel = true; // This will cancel the mock process and continue as usual (unmocked)\n        return null;\n      }\n\n      /**\n       * Delaying the response with a specific number of milliseconds:\n       *   request.get('https://domain.example/delay_test').end(function(err, res){\n       *     console.log(res.body); // This log will be written after the delay time has passed \n       *   })\n       */\n\n      if (match[1] === '/delay_test') {\n        context.delay = 3000; // This will delay the response by 3 seconds\n        return 'zzZ';\n      }\n\n      /**\n       * Mocking progress events:\n       *   request.get('https://domain.example/progress_test')\n       *     .on('progress', function (e) { console.log(e.percent + '%'); })\n       *     .end(function(err, res){\n       *       console.log(res.body); // This log will be written after all progress events emitted \n       *     })\n       */\n\n      if (match[1] === '/progress_test') {\n        context.progress = {\n          parts: 3,               // The number of progress events to emit one after the other with linear progress\n                                  //   (Meaning, loaded will be [total/parts])\n          delay: 1000,            // [optional] The delay of emitting each of the progress events by ms \n                                  //   (default is 0 unless context.delay specified, then it's [delay/parts])\n          total: 100,             // [optional] The total as it will appear in the progress event (default is 100)\n          lengthComputable: true, // [optional] The same as it will appear in the progress event (default is true)\n          direction: 'upload'     // [optional] superagent adds 'download'/'upload' direction to the event (default is 'upload')\n        };\n        return 'Hundred percent!';\n      }\n    },\n\n    /**\n     * returns the result of the GET request\n     *\n     * @param match array Result of the resolution of the regular expression\n     * @param data  mixed Data returns by `fixtures` attribute\n     */\n    get: function (match, data) {\n      return {\n        body: data\n      };\n    },\n\n    /**\n     * returns the result of the POST request\n     *\n     * @param match array Result of the resolution of the regular expression\n     * @param data  mixed Data returns by `fixtures` attribute\n     */\n    post: function (match, data) {\n      return {\n        status: 201\n      };\n    }\n  },\n  ...\n];\n```\n\nThen use the plugin:\n\n```js\n// ./server.js file\nvar request = require('superagent');\nvar config = require('./superagent-mock-config');\n\n// Before tests\nvar superagentMock = require('superagent-mock')(request, config);\n\n...\n\n// After tests\nsuperagentMock.unset();\n```\n\n## Supported methods\n\nAll request methods are supported (get, put, post, etc.).\n\nEach request method mock have to be declared in the config file. Otherwise, the `callback` method is used.\n\n## Logging\n\nYou can monitor each call, that has been intercepted by superagent-mock or not, by passing a callback function at initialization.\n\n``` js\n// ./server.js file\nvar request = require('superagent');\nvar config = require('./superagent-mock-config');\n\nvar logger = function(log)  {\n  console.log('superagent call', log);\n};\n\n// Before tests\nvar superagentMock = require('superagent-mock')(request, config, logger);\n\n...\n\n// After tests\nsuperagentMock.unset();\n```\n\nThe callback function will be called with an object containing the following informations\n - data : data used with `superagent.send` function\n - headers : array of headers given by `superagent.set` function\n - matcher : regex matching the current url which is defined in the provided config\n - url : url which superagent was called\n - method : HTTP method used for the call\n - timestamp : timestamp of the superagent call\n - mocked : true if the call was mocked by superagent mock, false if it used superagent real methods\n\n## Development scripts\n\nTo run units tests: `yarn test`.\n\nTo check code style: `yarn lint`.\n\nTo build code: `yarn build`.\n\n## Credits\n\nDevelopped by the Cytron Team of [Bedrock Streaming](https://tech.bedrockstreaming.com/).\nTested with [Jest](https://jestjs.io/).\n\n## License\n\nsuperagent-mock is licensed under the [MIT license](LICENSE).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbedrockstreaming%2Fsuperagent-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbedrockstreaming%2Fsuperagent-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbedrockstreaming%2Fsuperagent-mock/lists"}