{"id":21072432,"url":"https://github.com/binarymist/mocksse","last_synced_at":"2025-05-16T05:32:22.270Z","repository":{"id":42231424,"uuid":"145415505","full_name":"binarymist/mocksse","owner":"binarymist","description":"Allows testing of Server Sent Events","archived":false,"fork":false,"pushed_at":"2023-01-07T22:16:09.000Z","size":496,"stargazers_count":19,"open_issues_count":4,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-08T14:41:05.274Z","etag":null,"topics":[],"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/binarymist.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}},"created_at":"2018-08-20T12:35:44.000Z","updated_at":"2025-02-22T16:48:34.000Z","dependencies_parsed_at":"2023-02-08T02:48:21.713Z","dependency_job_id":null,"html_url":"https://github.com/binarymist/mocksse","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binarymist%2Fmocksse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binarymist%2Fmocksse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binarymist%2Fmocksse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binarymist%2Fmocksse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/binarymist","download_url":"https://codeload.github.com/binarymist/mocksse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254474613,"owners_count":22077320,"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","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":[],"created_at":"2024-11-19T18:56:33.651Z","updated_at":"2025-05-16T05:32:20.083Z","avatar_url":"https://github.com/binarymist.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# MockSSE\n\n[![pipeline status](https://gitlab.com/binarymist/mocksse/badges/master/pipeline.svg)](https://gitlab.com/binarymist/mocksse/commits/master) \u0026nbsp; [![test coverage](https://gitlab.com/binarymist/mocksse/badges/master/coverage.svg)](https://gitlab.com/binarymist/mocksse/commits/master) \u0026nbsp; [![license](https://img.shields.io/badge/license-MIT-green.svg)](https://gitlab.com/binarymist/mocksse/blob/master/LICENSE)\n\nThis project is a port/rewrite of [MockEvent](https://github.com/eloyz/mockevent) (which targets the browser). Instead, this project was designed for mocking EventSource (specifically the npm package [eventsource](https://github.com/EventSource/eventsource)) /Server Sent Events (SSE) where both your server side and client side are written for the NodeJS platform.\n\nMockSSE simulates [SSE](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events).\n\nUseful if you want to:\n\n* Build the client side without having the server side available to you\n* Or simply unit testing -\u003e C/I, rather than integration tests\n\nThis project has no dependencies other than native modules.\n\n* [Installation](#install)\n* [Usage](#usage)\n* [Contribution](#contribution)\n* [Acknowledgements](#acknowledgements)\n* [License](#license)\n\n## Install\n\n`npm install mocksse --save-dev`\n\n## Usage\n\nAll examples include:\n\n* How to instantiate a [`MockEvent`](https://gitlab.com/binarymist/mocksse/blob/494675303c32cc8f5c87e20f8482e8c10bd76742/src/mocksse.js#L234) which streams the data\n* How to instantiate an [`EventSource`](https://gitlab.com/binarymist/mocksse/blob/494675303c32cc8f5c87e20f8482e8c10bd76742/src/mocksse.js#L137) which consumes the data\n* Adding at least one event listener\n\nAs well as the following examples:\n\n* The [tests](https://gitlab.com/binarymist/mocksse/blob/master/test/mocksse.js) demonstrate how to use the project along with the feature set\n* The project that uses `mocksse` for testing the client side of its server sent events (SSE) can be seen at the [purpleteam](https://gitlab.com/purpleteam-labs/purpleteam/tree/master) repository\n\n### Example 0\n\nGetting started with the least amount of work (based on test [`should handle an event to relative url`](https://gitlab.com/binarymist/mocksse/blob/494675303c32cc8f5c87e20f8482e8c10bd76742/test/mocksse.js#L19)).\n\n```javascript\nconst { MockEvent, EventSource } = require('mocksse');\n// Or using ESM:\n// import { MockEvent, EventSource } from 'mocksse';\n\n// Instantiate a MockEvent.\nnew MockEvent({\n  url: '/your-relative-or-absolute-url',\n  responses: [\n    { type: 'a message event', data: 'a short message' }\n  ]\n});\n\n// Instantiate an EventSource.\nconst evtSource = new EventSource('/your-relative-or-absolute-url');\n\n// Listen for a \"a message event\" event and handle appropriately.\nevtSource.addEventListener('a message event', (event) =\u003e {\n  // event.type === 'a message event'\n  // event.data === 'a short message'\n});\n\n```\n\n### Example 1\n\nMultiple event listeners (based on test [should handle an event with setInterval, and collection of full responses](https://gitlab.com/binarymist/mocksse/blob/494675303c32cc8f5c87e20f8482e8c10bd76742/test/mocksse.js#L81)).\n\n```javascript\nconst { MockEvent, EventSource } = require('mocksse');\n// Or using ESM:\n// import { MockEvent, EventSource } from 'mocksse';\n\n// Instantiate a MockEvent.\nnew MockEvent({\n  url: 'http://noPlaceLikeHome:2000/your-route',\n  setInterval: 10,\n  responses: [\n    { lastEventId: 'event Id One', type: 'progressEvent', data: { progress: 'Look mum, I am making great progress' } },\n    { lastEventId: 'event Id Two', type: 'pctCompleteEvent', data: { pctComplete: 11 } },\n    { lastEventId: 'event Id three', type: 'temperatureEvent', data: { temperature: 25 } }\n  ]\n});\n\n// Instantiate an EventSource.\nconst evtSource = new EventSource('http://noPlaceLikeHome:2000/your-route');\n\n// Listen for multiple events and handle appropriately.\nevtSource.onopen = (event) =\u003e { // Optionally subscribing to the open event\n  // You can see this event in mocksse.js.\n  // event === { message: 'The opening message.', anotherCustomeProp: { prop: 'whatever' } }  \n};\nevtSource.addEventListener('progressEvent', (event) =\u003e {\n  // event.type === 'progressEvent'\n  // event.data === { progress: 'Look mum, I am making great progress' }\n  // event.origin === 'http://noPlaceLikeHome:2000'\n  // event.lastEventId === 'event Id One'\n});\nevtSource.addEventListener('pctCompleteEvent', (event) =\u003e {\n  // event.type === 'pctCompleteEvent'\n  // event.data === { pctComplete: 11 }\n  // event.origin === 'http://noPlaceLikeHome:2000'\n  // event.lastEventId === 'event Id Two'\n});\nevtSource.addEventListener('temperatureEvent', (event) =\u003e {\n  // event.type === 'temperatureEvent'\n  // event.data === { temperature: 25 }\n  // event.origin === 'http://noPlaceLikeHome:2000'\n  // event.lastEventId === 'event Id three'\n});\n```\n\n* `url`: The relative or full URL for your Server Sent Event API.  This is the URL we will subscribe to via `EventSource`\n* `setInterval`: Miliseconds to wait before sending the next event/response\n* `responses`: A list of event/responses to send and the order in which to send them\n  * `lastEventId` can be set and read\n  * `type`: Server Sent Events can have a name that you directly subscribe to in case you want to handle that name differently. In the above example we require you to subscribe to 3 different names `progressEvent`, `pctCompleteEvent`, and `temperatureEvent`. A [working example](https://gitlab.com/purpleteam-labs/purpleteam/blob/94ed3203086173d5fd06920a25b785a57673279c/src/presenter/apiDecoratingAdapter.js#L101-L103) of this can be seen in the purpleteam project\n  * `data`: The data you want to send\n\n### Example 2\n\nDynamically make responses and then have them `stream`ed (based on test [response function should be called, which then sends event](https://gitlab.com/binarymist/mocksse/blob/9362050dfeae6620b85a3827c802374ae3b14dd6/test/mocksse.js#L190)).\n\n```javascript\nconst { MockEvent, EventSource } = require('mocksse');\n// Or using ESM:\n// import { MockEvent, EventSource } from 'mocksse';\n\n// Instantiate a MockEvent.\nnew MockEvent({\n  url: 'http://noPlaceLikeHome:2000/your-route',\n  setInterval: [100, 200],\n  response: (mockEvent, evtSource) =\u003e {\n    const data = [\n      { lastEventId: 'event Id One', type: 'yourEvent', data: { yourProp: 'Wish I was done!' } },\n      { lastEventId: 'event Id Two', type: 'yourEvent', data: { yourProp: 'Oh, wow, nearly done!' } }\n    ];\n    // In this case we have static data, but you can build that data\n    // however you choose and then stream it when you're ready.\n    mockEvent.stream(data);\n  }\n});\n\n// Instantiate an EventSource.\nconst evtSource = new EventSource('http://noPlaceLikeHome:2000/your-route');\n\n// Listen for a \"yourEvent\" event and handle appropriately.\nevtSource.addEventListener('yourEvent', (event) =\u003e {\n  // On first invocation: event === { origin: 'http://noPlaceLikeHome:2000, lastEventId: 'event Id One', type: 'yourEvent', data: { yourProp: 'Wish I was done!' } }\n  // On second invocation: event === { origin: 'http://noPlaceLikeHome:2000, lastEventId: 'event Id Two', type: 'yourEvent', data: { yourProp: 'Oh, wow, nearly done!' } }\n});\n\n```\n\nThis uses the `response` attribute instead of the plural `responses` method.  Here you can build build a list of responses to send and then stream them when you're ready.\n\nThe `stream` method respects the `setInterval` property you specified. Also notice that `setInterval` can be set to an `int`, `float`, or `array` representing a min/max range of milliseconds.\n\nTechnically you can use any method and then call `handler.stream`. The benefit of using the `response` hook is that you get the `mockEvent` and `evtSource` object in case you need those values for anything.  Example 3 shows how you can benefit from these objects.\n\n### Example 3\n\nDynamically make responses and then `stream` them yourself within your `response` function (based on test [response function should be called, which then streams events itself](https://gitlab.com/binarymist/mocksse/blob/58439a92aacde9767d4f07a44fa59b7d657d52b8/test/mocksse.js#L219)).\n\n```javascript\nconst { MockEvent, EventSource } = require('mocksse');\n// Or using ESM:\n// import { MockEvent, EventSource } from 'mocksse';\n\n// Instantiate a MockEvent.\nnew MockEvent({\n  url: 'http://noPlaceLikeHome:2000/your-route',\n  setInterval: [100, 200],\n  response: (mockEvent, evtSource) =\u003e {\n    const data = [\n      { lastEventId: 'event Id One', type: 'yourEvent', data: { yourProp: 'Wish I was done!' } },\n      { lastEventId: 'event Id Two', type: 'yourEvent', data: { yourProp: 'Oh, wow, nearly done!' } }\n    ];\n    // In this case we have static data, but you can build that data\n    // however you choose and then stream it when you're ready.\n    const intervalId = setInterval(() =\u003e {\n      const responseData = data.shift() || clearInterval(intervalId);\n      if (responseData) mockEvent.dispatchEvent(responseData);\n    }, mockEvent.setInterval);\n  }\n});\n\n// Instantiate an EventSource.\nconst evtSource = new EventSource('http://noPlaceLikeHome:2000/your-route');\n\n// Listen for a \"yourEvent\" event and handle appropriately.\nevtSource.addEventListener('yourEvent', (event) =\u003e {\n  // On first invocation: event === { origin: 'http://noPlaceLikeHome:2000, lastEventId: 'event Id One', type: 'yourEvent', data: { yourProp: 'Wish I was done!' } }\n  // On second invocation: event === { origin: 'http://noPlaceLikeHome:2000, lastEventId: 'event Id Two', type: 'yourEvent', data: { yourProp: 'Oh, wow, nearly done!' } }\n});\n```\n\nHere we write our own `stream` method that loops through data and sends. Still respecting the `setInterval` attribute and leveraging the internal `dispatchEvent` method of the `mockEvent`.  This is not recommended, it's just used as an example to show the amount of access you have to all attributes.  Using the `dispatchEvent` method directly overwrites the `stream` attribute and will not respect other queues that maybe set by you later.\n\n### Example 4\n\nClosing the EventSource then subscribing to an event will invoke the error handler if you have subscribed to `onerror` (based on test [`closing the EventSource then subscribing to an event should invoke the error handler`](https://gitlab.com/binarymist/mocksse/blob/494675303c32cc8f5c87e20f8482e8c10bd76742/test/mocksse.js#L265)).\n\n```javascript\nconst { MockEvent, EventSource } = require('mocksse');\n// Or using ESM:\n// import { MockEvent, EventSource } from 'mocksse';\n\n// Instantiate a MockEvent.\nconst mockEvent = new MockEvent({\n  url: 'http://noPlaceLikeHome:2000/your-route',\n  verbose: true, // Feel free to turn verbosity on.\n  responses: [\n    { type: 'a message event', data: 'a short message' }\n  ]\n});\n\n// Instantiate an EventSource.\nconst evtSource = new EventSource('http://noPlaceLikeHome:2000/your-route');\n\nevtSource.close();\n\n// Listen for a \"a message event\" event and handle appropriately.\nevtSource.addEventListener('a message event', (event) =\u003e {\n  // The event handler will not be invoked on a closed EventSource.\n});\n\nevtSource.onerror = (error) =\u003e {\n  // Expect to see the following message\n  // error.message === '`EventSource` instance closed while sending.'\n};\n```\n\n## Contribution\n\nPlease open an [issue](https://gitlab.com/binarymist/mocksse/issues) to discus the proposed change before submitting a [merge request](https://gitlab.com/binarymist/mocksse/merge_requests).\n\n\n## Acknowledgements\n\nThis project is a port/rewrite of [MockEvent](https://github.com/eloyz/mockevent), which targets the browser.  \nInspiration was also taken from [eventsourcemock](https://github.com/gcedo/eventsourcemock), which is also browser focussed and very minimal.\n\n## License\n\nCopyright [Kim Carter](https://gitlab.com/binarymist) and other contributors, Licensed under [MIT](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinarymist%2Fmocksse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbinarymist%2Fmocksse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinarymist%2Fmocksse/lists"}