superwstest
Advanced tools
Comparing version 1.6.1 to 1.6.2
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("superwstest",[],t):"object"==typeof exports?exports.superwstest=t():e.superwstest=t()}(global,(function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=4)}([function(e,t){e.exports=require("blocking-queue")},function(e,t){e.exports=require("ws")},function(e,t){e.exports=require("supertest")},function(e,t){e.exports=require("fast-deep-equal")},function(e,t,n){e.exports=n(5)},function(e,t,n){"use strict";n.r(t),n.d(t,"default",(function(){return P}));var r=n(2),o=n.n(r),s=n(0),c=n.n(s),a=n(3),i=n.n(a),u=n(1),f=n.n(u);const d=/^http/;function l(e){return new Uint8Array(e)}function p({data:e,isBinary:t}){if(t)throw new Error("Expected text message, got binary");return String(e)}function g(e){return JSON.parse(p(e))}function h({data:e,isBinary:t}){if(!t)throw new Error("Expected binary message, got text");return l(e)}function w(e,t,n){return new Promise((r,o)=>{e.send(t,n,e=>{e?o(e):r()})}).catch(async t=>{if(t.message&&t.message.includes("WebSocket is not open")){const{code:t,data:n}=await e.closed;throw new Error(`Cannot send message; connection closed with ${t} "${n}"`)}})}function y(e){return"function"==typeof e?e.expectedMessage||"matching function":e instanceof Uint8Array?function(e){return`[${Buffer.from(e.buffer,e.byteOffset,e.byteLength).toString("hex").replace(/(..)(?!$)/g,"$1 ")}]`}(e):JSON.stringify(e)}const x={send:(e,t,n)=>w(e,t,n),sendText:(e,t)=>w(e,String(t)),sendJson:(e,t)=>w(e,JSON.stringify(t)),sendBinary:(e,t)=>w(e,l(t),{binary:!0}),wait:(e,t)=>new Promise(e=>setTimeout(e,t)),exec:async(e,t)=>t(e),expectMessage:async(e,t,n)=>{const r=await Promise.race([e.messages.pop(),e.closed.then(({code:e,data:t})=>{throw new Error(`Expected message ${y(n)}, but connection closed: ${e} "${t}"`)})]).then(t);if(void 0!==n)if("function"==typeof n){if(!1===n(r))throw new Error(`Expected message ${y(n)}, got ${y(r)}`)}else if(!i()(r,n))throw new Error(`Expected message ${y(n)}, got ${y(r)}`)},expectText:(e,t)=>{let n;return t instanceof RegExp?(n=e=>t.test(e),n.expectedMessage="matching "+t):n=t,x.expectMessage(e,p,n)},expectJson:(e,t)=>x.expectMessage(e,g,t),expectBinary:(e,t)=>{let n;if("function"==typeof t)n=t;else if(t){const e=l(t);n=t=>{return n=t,r=e,Buffer.from(n.buffer,n.byteOffset,n.byteLength).equals(r);var n,r},n.expectedMessage=y(e)}return x.expectMessage(e,h,n)},close:(e,t,n)=>e.close(t,n),expectClosed:async(e,t=null,n=null)=>{const{code:r,data:o}=await e.closed;if(null!==t&&r!==t)throw new Error(`Expected close code ${t}, got ${r} "${o}"`);if(null!==n&&String(o)!==n)throw new Error(`Expected close message "${n}", got ${r} "${o}"`)},expectUpgrade:async(e,t)=>{const n=await e.upgrade;if(!1===t(n))throw new Error(`Expected Upgrade matching assertion, got: status ${n.statusCode} headers ${JSON.stringify(n.headers)}`)}};function b(e){throw e.close(),new Error("Expected connection failure, but succeeded")}function m(e){return e.readyState===f.a.CONNECTING||e.readyState===f.a.OPEN}function E(e,t){const n=t.toLowerCase();return Object.keys(e).find(e=>e.toLowerCase()===n)||n}const v=new Set,$=()=>{throw new Error("WebSocket has already been established; cannot change configuration")};function O(e,t,n){"object"!=typeof t||null===t||Array.isArray(t)||(n=t,t=[]);const r={...n,headers:{...(n||{}).headers}};let o=Promise.resolve().then(()=>new Promise((n,o)=>{const s=new f.a(e,t,r);v.add(s);const a=s.close.bind(s);s.close=(...e)=>{a(...e),v.delete(s)},s.messages=new c.a;const i=new c.a,u=new c.a,d=new c.a;s.closed=u.pop(),s.firstError=i.pop().then(e=>{throw e}),s.upgrade=d.pop(),s.on("message",(e,t)=>{void 0!==t?s.messages.push({data:e,isBinary:t}):"string"==typeof e?s.messages.push({data:Buffer.from(e,"utf8"),isBinary:!1}):s.messages.push({data:e,isBinary:!0})}),s.on("error",o),s.on("close",(e,t)=>{v.delete(s),u.push({code:e,data:t})}),s.on("open",()=>{s.removeListener("error",o),s.on("error",e=>i.push(e)),n(s)}),s.on("upgrade",e=>{d.push(e)})}));const s={set:(e,t)=>"object"==typeof e?(Object.entries(e).forEach(([e,t])=>s.set(e,t)),o):(r.headers[E(r.headers,e)]=t,o),unset:e=>(delete r.headers[E(r.headers,e)],o)};function a(e){delete e.expectConnectionError,Object.keys(s).forEach(t=>{e[t]=$})}Object.assign(o,s);const i={};function u(e){return Object.assign(e,i)}const d=e=>(...t)=>(o=o.then(n=>Promise.race([e(n,...t),n.firstError]).catch(function(e){return t=>{throw m(e)&&e.close(),t}}(n)).then(()=>n)),a(o),u(o));return Object.keys(x).forEach(e=>{i[e]=d(x[e])}),o.expectConnectionError=(e=null)=>(o=o.then(b,t=>function(e,t){if(!t)return;let n=t;"number"==typeof t&&(n="Unexpected server response: "+t);const r=e.message;if(r!==n)throw new Error(`Expected connection failure with message "${n}", got "${r}"`)}(t,e)),a(o),o),u(o)}const S=new WeakMap;function j(e,t){let n=S.get(e);if(n)return void(n.shutdownDelay=Math.max(n.shutdownDelay,t));n={shutdownDelay:t},S.set(e,n);const r=new Set;e.on("connection",e=>{r.add(e),e.on("close",()=>r.delete(e))});const o=e.close.bind(e);e.close=t=>{e.address()?(!function(e,t){if(t<=0)return void[...e].forEach(e=>e.end());const n=Date.now()+t;[...e].forEach(async t=>{for(;Date.now()<n&&e.has(t);)await new Promise(e=>setTimeout(e,20));e.has(t)&&t.end()})}(r,n.shutdownDelay),n.shutdownDelay=0,o(t)):t&&t()}}const M=(e,{shutdownDelay:t=0}={})=>{if("string"!=typeof e){if(!e.address())throw new Error("Server must be listening: beforeEach((done) => server.listen(0, done));");j(e,t)}const n=o()(e);return n.ws=(t,...n)=>O(function(e,t){let n;if("string"==typeof e)n=e+t;else{if(!e.address())throw new Error("Server was closed");n=r.Test.prototype.serverAddress(e,t)}return n.replace(d,"ws")}(e,t),...n),n};M.closeAll=()=>{const e=[...v].filter(m);return v.clear(),e.forEach(e=>e.close()),e.length},M.default=M;var P=M}]).default})); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("superwstest",[],t):"object"==typeof exports?exports.superwstest=t():e.superwstest=t()}(global,(function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=4)}([function(e,t){e.exports=require("blocking-queue")},function(e,t){e.exports=require("ws")},function(e,t){e.exports=require("supertest")},function(e,t){e.exports=require("fast-deep-equal")},function(e,t,n){e.exports=n(5)},function(e,t,n){"use strict";n.r(t),n.d(t,"default",(function(){return P}));var r=n(2),o=n.n(r),s=n(0),c=n.n(s),a=n(3),i=n.n(a),u=n(1),f=n.n(u);const d=/^http/;function p(e){return new Uint8Array(e)}function l({data:e,isBinary:t}){if(t)throw new Error("Expected text message, got binary");return String(e)}function h(e){return JSON.parse(l(e))}function w({data:e,isBinary:t}){if(!t)throw new Error("Expected binary message, got text");return p(e)}function g(e,t,n){return new Promise((r,o)=>{e.send(t,n,e=>{e?o(e):r()})}).catch(async t=>{if(t.message&&t.message.includes("WebSocket is not open")){const{code:t,data:n}=await e.closed;throw new Error(`Cannot send message; connection closed with ${t} "${n}"`)}})}function y(e){return"function"==typeof e?e.expectedMessage||"matching function":e instanceof Uint8Array?function(e){return`[${Buffer.from(e.buffer,e.byteOffset,e.byteLength).toString("hex").replace(/(..)(?!$)/g,"$1 ")}]`}(e):JSON.stringify(e)}const x={send:(e,t,n)=>g(e,t,n),sendText:(e,t)=>g(e,String(t)),sendJson:(e,t)=>g(e,JSON.stringify(t)),sendBinary:(e,t)=>g(e,p(t),{binary:!0}),wait:(e,t)=>new Promise(e=>setTimeout(e,t)),exec:async(e,t)=>t(e),expectMessage:async(e,t,n)=>{const r=await Promise.race([e.messages.pop(),e.closed.then(({code:e,data:t})=>{throw new Error(`Expected message ${y(n)}, but connection closed: ${e} "${t}"`)})]).then(t);if(void 0!==n)if("function"==typeof n){if(!1===n(r))throw new Error(`Expected message ${y(n)}, got ${y(r)}`)}else if(!i()(r,n))throw new Error(`Expected message ${y(n)}, got ${y(r)}`)},expectText:(e,t)=>{let n;return t instanceof RegExp?(n=e=>t.test(e),n.expectedMessage="matching "+t):n=t,x.expectMessage(e,l,n)},expectJson:(e,t)=>x.expectMessage(e,h,t),expectBinary:(e,t)=>{let n;if("function"==typeof t)n=t;else if(t){const e=p(t);n=t=>{return n=t,r=e,Buffer.from(n.buffer,n.byteOffset,n.byteLength).equals(r);var n,r},n.expectedMessage=y(e)}return x.expectMessage(e,w,n)},close:(e,t,n)=>e.close(t,n),expectClosed:async(e,t=null,n=null)=>{const{code:r,data:o}=await e.closed;if(null!==t&&r!==t)throw new Error(`Expected close code ${t}, got ${r} "${o}"`);if(null!==n&&String(o)!==n)throw new Error(`Expected close message "${n}", got ${r} "${o}"`)},expectUpgrade:async(e,t)=>{const n=await e.upgrade;if(!1===t(n))throw new Error(`Expected Upgrade matching assertion, got: status ${n.statusCode} headers ${JSON.stringify(n.headers)}`)}};function b(e){throw e.close(),new Error("Expected connection failure, but succeeded")}function m(e){return e.readyState===f.a.CONNECTING||e.readyState===f.a.OPEN}function E(e,t){const n=t.toLowerCase();return Object.keys(e).find(e=>e.toLowerCase()===n)||n}const v=new Set,$=()=>{throw new Error("WebSocket has already been established; cannot change configuration")};function O(e,t,n){"object"!=typeof t||null===t||Array.isArray(t)||(n=t,t=[]);const r={...n,headers:{...(n||{}).headers}};let o=Promise.resolve().then(()=>new Promise((n,o)=>{const s=new f.a(e,t,r);v.add(s);const a=s.close.bind(s);s.close=(...e)=>{a(...e),v.delete(s)},s.messages=new c.a;const i=new c.a,u=new c.a,d=new c.a;s.closed=u.pop(),s.firstError=i.pop().then(e=>{throw e}),s.upgrade=d.pop(),s.on("message",(e,t)=>{void 0!==t?s.messages.push({data:e,isBinary:t}):"string"==typeof e?s.messages.push({data:Buffer.from(e,"utf8"),isBinary:!1}):s.messages.push({data:e,isBinary:!0})}),s.on("error",o),s.on("close",(e,t)=>{v.delete(s),u.push({code:e,data:t})}),s.on("open",()=>{s.removeListener("error",o),s.on("error",e=>i.push(e)),n(s)}),s.on("upgrade",e=>{d.push(e)})}));const s={set:(e,t)=>"object"==typeof e?(Object.entries(e).forEach(([e,t])=>s.set(e,t)),o):(r.headers[E(r.headers,e)]=t,o),unset:e=>(delete r.headers[E(r.headers,e)],o)};function a(e){delete e.expectConnectionError,Object.keys(s).forEach(t=>{e[t]=$})}Object.assign(o,s);const i={};function u(e){return Object.assign(e,i)}const d=e=>(...t)=>(o=o.then(n=>Promise.race([e(n,...t),n.firstError]).catch(function(e){return t=>{throw m(e)&&e.close(),t}}(n)).then(()=>n)),a(o),u(o));return Object.keys(x).forEach(e=>{i[e]=d(x[e])}),o.expectConnectionError=(e=null)=>(o=o.then(b,t=>function(e,t){if(!t)return;let n=t;"number"==typeof t&&(n="Unexpected server response: "+t);const r=e.message;if(r!==n)throw new Error(`Expected connection failure with message "${n}", got "${r}"`)}(t,e)),a(o),o),u(o)}const S=new WeakMap;function j(e,t){let n=S.get(e);if(n)return void(n.shutdownDelay=Math.max(n.shutdownDelay,t));n={shutdownDelay:t},S.set(e,n);const r=new Set;e.on("connection",e=>{r.add(e),e.on("close",()=>r.delete(e))});const o=e.close.bind(e);e.close=t=>{e.address()?(!function(e,t){if(t<=0)return void[...e].forEach(e=>e.end());const n=Date.now()+t;[...e].forEach(async t=>{for(;Date.now()<n&&e.has(t);)await new Promise(e=>setTimeout(e,20));e.has(t)&&t.end()})}(r,n.shutdownDelay),n.shutdownDelay=0,o(t)):t&&t()}}const M=(e,{shutdownDelay:t=0}={})=>{if("string"!=typeof e){if(!e.address())throw new Error("Server must be listening:\nbeforeEach((done) => server.listen(0, done));\nafterEach((done) => server.close(done));\n\nsupertest's request(app) syntax is not supported (find out more: https://github.com/davidje13/superwstest#why-isnt-requestapp-supported)");j(e,t)}const n=o()(e);return n.ws=(t,...n)=>O(function(e,t){let n;if("string"==typeof e)n=e+t;else{if(!e.address())throw new Error("Server was closed");n=r.Test.prototype.serverAddress(e,t)}return n.replace(d,"ws")}(e,t),...n),n};M.closeAll=()=>{const e=[...v].filter(m);return v.clear(),e.forEach(e=>e.close()),e.length},M.default=M;var P=M}]).default})); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "superwstest", | ||
"version": "1.6.1", | ||
"version": "1.6.2", | ||
"description": "supertest with added WebSocket capabilities", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -396,1 +396,51 @@ # SuperWSTest | ||
chain web socket actions and expectations.* | ||
## FAQ | ||
### My server is closing the connection immediately with code 1002 | ||
Your server is probably trying to indicate that you need to specify a | ||
particular sub-protocol when connecting: | ||
```javascript | ||
request(myServer) | ||
.ws('/path/ws', 'my-protocol-here') | ||
``` | ||
You will need to check the documentation for the server library you are | ||
using to find out which subprotocol is needed. If multiple sub-protocols | ||
are needed, you can provide an array of strings. | ||
### Why isn't `request(app)` supported? | ||
This project aims to be API-compatible with `supertest` wherever possible, | ||
but does not support the ability to pass an `express` app directly into | ||
`request()` (instead, the server must be started in advance and the server | ||
object passed in). The recommended approach is: | ||
```javascript | ||
beforeEach((done) => { | ||
server.listen(0, 'localhost', done); | ||
}); | ||
afterEach((done) => { | ||
server.close(done); | ||
}); | ||
``` | ||
There are several reasons for not supporting this feature: | ||
- `supertest`'s implementation | ||
[has a bug](https://github.com/visionmedia/supertest/issues/566) where it | ||
does not wait for the server to start before making requests. This can lead | ||
to flakey tests. For this reason it seems beneficial to discourage this | ||
approach in general (for both WebSocket and non-WebSocket tests). | ||
- It is not possible for this library to reliably know when a test has ended, | ||
so it is not obvious when the auto-started server should be closed. | ||
`supertest` | ||
[never closes these auto-started servers](https://github.com/visionmedia/supertest/issues/437) | ||
(leading to | ||
[a large number of servers being spawned](https://github.com/visionmedia/supertest/issues/489) | ||
during a test run), but even this approach is not viable for WebSocket | ||
testing (typical web requests are short-lived, but websockets are long-lived | ||
and any dangling connections will prevent the test process from terminating). |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50776
83
446