@ffflorian/mock-udp 1.10.0 → 1.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -18,12 +18,12 @@
18
18
  "name": "@ffflorian/mock-udp",
19
19
  "repository": "https://github.com/ffflorian/node-packages/tree/main/packages/mock-udp",
20
20
  "scripts": {
21
- "build": "tsc -p tsconfig.json",
21
+ "build": "tsc -p tsconfig.build.json",
22
22
  "clean": "rimraf dist",
23
23
  "dist": "yarn clean && yarn build",
24
24
  "test": "vitest run"
25
25
  },
26
26
  "type": "module",
27
- "version": "1.10.0",
28
- "gitHead": "558b5c962cd6ad6e42e7c4917294cfddd2e60599"
27
+ "version": "1.10.2",
28
+ "gitHead": "0aaf2fa002773109356aa85ec77c5859032830b0"
29
29
  }
@@ -1 +0,0 @@
1
- export {};
@@ -1,144 +0,0 @@
1
- /* eslint-disable no-magic-numbers */
2
- import * as dgram from 'node:dgram';
3
- import { assert, expect, describe, test, beforeEach } from 'vitest';
4
- import * as mockudp from './index.js';
5
- const buffer = Buffer.from('hello world');
6
- describe('mock-udp.intercept', () => {
7
- test('should have punched Socket.prototype.send in the face', () => {
8
- mockudp.intercept();
9
- expect(mockudp.isMocked()).toBe(true);
10
- mockudp.revert();
11
- });
12
- });
13
- describe('mock-udp.revert', () => {
14
- test('should revert back to the unpunched state', () => {
15
- mockudp.intercept();
16
- mockudp.revert();
17
- expect(mockudp.isMocked()).not.toBe(true);
18
- });
19
- });
20
- describe('mock-udp.add', () => {
21
- test('should return a new, unused Scope', () => {
22
- const scope = mockudp.add('');
23
- expect(scope.done()).toBe(false);
24
- mockudp.clean();
25
- });
26
- });
27
- describe('mock-udp.clean', () => {
28
- test('should clean all interceptions', () => {
29
- const range = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
30
- range.forEach(index => mockudp.add(`localhost:100${index}`));
31
- mockudp.intercept();
32
- mockudp.clean();
33
- const client = dgram.createSocket('udp4');
34
- range.forEach(index => {
35
- try {
36
- client.send(buffer, 0, buffer.length, 1000 + index, 'localhost');
37
- assert.fail();
38
- }
39
- catch (_a) { }
40
- });
41
- });
42
- });
43
- describe('mock-udp.overriddenSocketSend', () => {
44
- beforeEach(() => {
45
- mockudp.intercept();
46
- mockudp.clean();
47
- });
48
- test('should intercept a basic UDP request', () => {
49
- return new Promise(done => {
50
- const scope = mockudp.add('localhost:1000');
51
- const client = dgram.createSocket('udp4');
52
- client.send(buffer, 0, buffer.length, 1000, 'localhost', () => {
53
- scope.done();
54
- done(void 0);
55
- });
56
- });
57
- });
58
- test('should not throw an exception with a missing callback', () => {
59
- mockudp.add('localhost:1000');
60
- const client = dgram.createSocket('udp4');
61
- client.send(buffer, 0, buffer.length, 1000, 'localhost');
62
- });
63
- test('should return the correct number of bytes to the callback', () => {
64
- return new Promise(done => {
65
- mockudp.add('localhost:1000');
66
- const client = dgram.createSocket('udp4');
67
- client.send(buffer, 0, 5, 1000, 'localhost', (_, bytes) => {
68
- expect(bytes).toBe(5);
69
- done(void 0);
70
- });
71
- });
72
- });
73
- test('should return a scope with a correct buffer', () => {
74
- return new Promise(done => {
75
- const scope = mockudp.add('localhost:1000');
76
- const client = dgram.createSocket('udp4');
77
- client.send(buffer, 1, 6, 1000, 'localhost', () => {
78
- if (!scope.buffer) {
79
- return assert.fail();
80
- }
81
- expect(scope.buffer.toString()).toBe('ello w');
82
- done(void 0);
83
- });
84
- });
85
- });
86
- test('should handle two UDP intercepts per request', () => {
87
- return new Promise(done => {
88
- const scope1 = mockudp.add('localhost:1000');
89
- const scope2 = mockudp.add('localhost:1000');
90
- const client = dgram.createSocket('udp4');
91
- client.send(buffer, 0, buffer.length, 1000, 'localhost', () => {
92
- scope1.done();
93
- scope2.done();
94
- done(void 0);
95
- });
96
- });
97
- });
98
- test('should throw an error when reusing an intercept', () => {
99
- return new Promise(done => {
100
- const scope = mockudp.add('localhost:1000');
101
- const client = dgram.createSocket('udp4');
102
- client.send(buffer, 0, buffer.length, 1000, 'localhost', () => {
103
- scope.done();
104
- try {
105
- client.send(buffer, 0, buffer.length, 1000, 'localhost');
106
- assert.fail();
107
- }
108
- catch (_a) {
109
- done(void 0);
110
- }
111
- });
112
- });
113
- });
114
- test('should throw an error if offset is equal to the length of the buffer', () => {
115
- const scope = mockudp.add('localhost:1000');
116
- const client = dgram.createSocket('udp4');
117
- try {
118
- client.send(buffer, buffer.length, buffer.length, 1000, 'localhost');
119
- assert.fail();
120
- }
121
- catch (_a) { }
122
- expect(scope.done()).toBe(false);
123
- });
124
- test('should throw an error if offset is greater than the length of the buffer', () => {
125
- const scope = mockudp.add('localhost:1000');
126
- const client = dgram.createSocket('udp4');
127
- try {
128
- client.send(buffer, buffer.length + 1, buffer.length, 1000, 'localhost');
129
- assert.fail();
130
- }
131
- catch (_a) { }
132
- expect(scope.done()).toBe(false);
133
- });
134
- test('should throw an error if the length is greater than the length of the buffer', () => {
135
- const scope = mockudp.add('localhost:1000');
136
- const client = dgram.createSocket('udp4');
137
- try {
138
- client.send(buffer, 0, buffer.length + 1, 1000, 'localhost');
139
- assert.fail();
140
- }
141
- catch (_a) { }
142
- expect(scope.done()).toBe(false);
143
- });
144
- });