superwstest
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -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 M}));var r=n(2),o=n.n(r),s=n(0),c=n.n(s),i=n(3),u=n.n(i),f=n(1),a=n.n(f);const l=/^http/;function d(e){return new Uint8Array(e)}function p(e){if("string"!=typeof e)throw new Error("Expected text message, got "+typeof e);return e}function w(e){return JSON.parse(p(e))}function g(e){if("string"==typeof e)throw new Error("Expected binary message, got text");return d(e)}function y(e,t,n){e.send(t,n,e=>{if(e)throw e})}function h(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)=>y(e,t,n),sendText:(e,t)=>y(e,String(t)),sendJson:(e,t)=>y(e,JSON.stringify(t)),sendBinary:(e,t)=>y(e,d(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(()=>{throw new Error(`Expected message ${h(n)}, but connection closed`)})]).then(t);if(void 0!==n)if("function"==typeof n){if(!1===n(r))throw new Error(`Expected message ${h(n)}, got ${h(r)}`)}else if(!u()(r,n))throw new Error(`Expected message ${h(n)}, got ${h(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,w,t),expectBinary:(e,t)=>{let n;if("function"==typeof t)n=t;else if(t){const e=d(t);n=t=>{return n=t,r=e,Buffer.from(n.buffer,n.byteOffset,n.byteLength).equals(r);var n,r},n.expectedMessage=h(e)}return x.expectMessage(e,g,n)},close:(e,t,n)=>e.close(t,n),expectClosed:async(e,t=null,n=null)=>{const{code:r,message:o}=await e.closed;if(null!==t&&r!==t)throw new Error(`Expected close code ${t}, got ${r}`);if(null!==n&&o!==n)throw new Error(`Expected close message "${n}", got "${o}"`)}};function m(e){throw e.close(),new Error("Expected connection failure, but succeeded")}function b(e){return e.readyState===a.a.CONNECTING||e.readyState===a.a.OPEN}const E=new Set;function v(e,t,n){let r=new Promise((r,o)=>{const s=new a.a(e,t,n);E.add(s);const i=s.close.bind(s);s.close=(...e)=>{i(...e),E.delete(s)},s.messages=new c.a;const u=new c.a,f=new c.a;s.closed=f.pop(),s.firstError=u.pop().then(e=>{throw e}),s.on("message",e=>s.messages.push(e)),s.on("error",o),s.on("close",(e,t)=>{E.delete(s),f.push({code:e,message:t})}),s.on("open",()=>{s.removeListener("error",o),s.on("error",e=>u.push(e)),r(s)})});const o={};function s(e){return Object.assign(e,o)}const i=e=>(...t)=>(r=r.then(n=>Promise.race([e(n,...t),n.firstError]).catch(function(e){return t=>{throw b(e)&&e.close(),t}}(n)).then(()=>n)),delete r.expectConnectionError,s(r));return Object.keys(x).forEach(e=>{o[e]=i(x[e])}),r.expectConnectionError=(e=null)=>(r=r.then(m,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)),delete r.expectConnectionError,r),s(r)}const O=new WeakMap;function S(e,t){let n=O.get(e);if(n)return void(n.shutdownDelay=Math.max(n.shutdownDelay,t));n={shutdownDelay:t},O.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 $=(e,{shutdownDelay:t=0}={})=>{if("string"!=typeof e){if(!e.address())throw new Error("Server must be listening: beforeEach((done) => server.listen(0, done));");S(e,t)}const n=o()(e);return n.ws=(t,...n)=>v(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(l,"ws")}(e,t),...n),n};$.closeAll=()=>{const e=[...E].filter(b);return E.clear(),e.forEach(e=>e.close()),e.length};var M=$}])})); | ||
!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 M}));var r=n(2),o=n.n(r),s=n(0),c=n.n(s),i=n(3),u=n.n(i),a=n(1),f=n.n(a);const p=/^http/;function d(e){return new Uint8Array(e)}function l(e){if("string"!=typeof e)throw new Error("Expected text message, got "+typeof e);return e}function w(e){return JSON.parse(l(e))}function g(e){if("string"==typeof e)throw new Error("Expected binary message, got text");return d(e)}function y(e,t,n){e.send(t,n,e=>{if(e)throw e})}function h(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)=>y(e,t,n),sendText:(e,t)=>y(e,String(t)),sendJson:(e,t)=>y(e,JSON.stringify(t)),sendBinary:(e,t)=>y(e,d(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(()=>{throw new Error(`Expected message ${h(n)}, but connection closed`)})]).then(t);if(void 0!==n)if("function"==typeof n){if(!1===n(r))throw new Error(`Expected message ${h(n)}, got ${h(r)}`)}else if(!u()(r,n))throw new Error(`Expected message ${h(n)}, got ${h(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,w,t),expectBinary:(e,t)=>{let n;if("function"==typeof t)n=t;else if(t){const e=d(t);n=t=>{return n=t,r=e,Buffer.from(n.buffer,n.byteOffset,n.byteLength).equals(r);var n,r},n.expectedMessage=h(e)}return x.expectMessage(e,g,n)},close:(e,t,n)=>e.close(t,n),expectClosed:async(e,t=null,n=null)=>{const{code:r,message:o}=await e.closed;if(null!==t&&r!==t)throw new Error(`Expected close code ${t}, got ${r}`);if(null!==n&&o!==n)throw new Error(`Expected close message "${n}", got "${o}"`)},expectUpgrade:async(e,t)=>{if(!t(await e.upgrade))throw new Error("Upgrade assertion returned false")}};function m(e){throw e.close(),new Error("Expected connection failure, but succeeded")}function b(e){return e.readyState===f.a.CONNECTING||e.readyState===f.a.OPEN}const E=new Set;function v(e,t,n){let r=new Promise((r,o)=>{const s=new f.a(e,t,n);E.add(s);const i=s.close.bind(s);s.close=(...e)=>{i(...e),E.delete(s)},s.messages=new c.a;const u=new c.a,a=new c.a,p=new c.a;s.closed=a.pop(),s.firstError=u.pop().then(e=>{throw e}),s.upgrade=p.pop(),s.on("message",e=>s.messages.push(e)),s.on("error",o),s.on("close",(e,t)=>{E.delete(s),a.push({code:e,message:t})}),s.on("open",()=>{s.removeListener("error",o),s.on("error",e=>u.push(e)),r(s)}),s.on("upgrade",e=>{p.push(e)})});const o={};function s(e){return Object.assign(e,o)}const i=e=>(...t)=>(r=r.then(n=>Promise.race([e(n,...t),n.firstError]).catch(function(e){return t=>{throw b(e)&&e.close(),t}}(n)).then(()=>n)),delete r.expectConnectionError,s(r));return Object.keys(x).forEach(e=>{o[e]=i(x[e])}),r.expectConnectionError=(e=null)=>(r=r.then(m,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)),delete r.expectConnectionError,r),s(r)}const O=new WeakMap;function S(e,t){let n=O.get(e);if(n)return void(n.shutdownDelay=Math.max(n.shutdownDelay,t));n={shutdownDelay:t},O.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 $=(e,{shutdownDelay:t=0}={})=>{if("string"!=typeof e){if(!e.address())throw new Error("Server must be listening: beforeEach((done) => server.listen(0, done));");S(e,t)}const n=o()(e);return n.ws=(t,...n)=>v(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(p,"ws")}(e,t),...n),n};$.closeAll=()=>{const e=[...E].filter(b);return E.clear(),e.forEach(e=>e.close()),e.length};var M=$}])})); | ||
//# sourceMappingURL=index.js.map |
declare module 'superwstest' { | ||
import { Server, ClientRequestArgs } from 'http'; | ||
import { Server, ClientRequestArgs, IncomingMessage } from 'http'; | ||
import WebSocket from 'ws'; | ||
@@ -39,2 +39,4 @@ import { SuperTest, Test } from 'supertest'; | ||
expectUpgrade(test: (request: IncomingMessage) => boolean): this; | ||
expectConnectionError(expectedCode?: number | null): Promise<WebSocket>; | ||
@@ -41,0 +43,0 @@ } |
{ | ||
"name": "superwstest", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "supertest with added WebSocket capabilities", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -112,2 +112,3 @@ # SuperWSTest | ||
- [.expectConnectionError([expectedStatusCode])](#expectconnectionerrorexpectedstatuscode) | ||
- [.expectUpgrade(fn)](#expectupgradefn) | ||
- [.wait(milliseconds)](#waitmilliseconds) | ||
@@ -283,2 +284,13 @@ - [.exec(fn)](#execfn) | ||
### `.expectUpgrade(fn)` | ||
Run a check against the Upgrade response. If the predicate returns true | ||
the check passes, or if false, the check fails. Useful for making | ||
arbitrary assertions about parts of the Upgrade response, such as headers. | ||
```javascript | ||
request(server).ws('...') | ||
.expectUpgrade((req) => (req.headers['set-cookie'] === 'foo=bar')); | ||
``` | ||
### `.wait(milliseconds)` | ||
@@ -285,0 +297,0 @@ |
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
40916
71
319