Skip to content

Commit 6ae196d

Browse files
committed
fix: remove unused request dependency
1 parent 31e2ff9 commit 6ae196d

File tree

1 file changed

+64
-29
lines changed

1 file changed

+64
-29
lines changed

src/watch_test.ts

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { PassThrough } from 'stream';
66
import { KubeConfig } from './config';
77
import { Cluster, Context, User } from './config_types';
88
import { Watch } from './watch';
9+
import { IncomingMessage } from 'http';
910

1011
const server = 'http://foo.company.com';
1112

@@ -60,6 +61,7 @@ describe('Watch', () => {
6061
await watch.watch(
6162
path,
6263
{},
64+
// tslint:disable-next-line:no-empty
6365
(phase: string, obj: string) => {},
6466
(err: any) => {
6567
doneCalled = true;
@@ -96,13 +98,18 @@ describe('Watch', () => {
9698

9799
const [scope] = systemUnderTest();
98100

101+
let response: IncomingMessage | undefined;
102+
99103
const s = scope
100104
.get(path)
101105
.query({
102106
watch: 'true',
103107
a: 'b',
104108
})
105-
.reply(200, () => {
109+
.reply(200, function (): PassThrough {
110+
this.req.on('response', (r) => {
111+
response = r;
112+
});
106113
stream.push(JSON.stringify(obj1) + '\n');
107114
stream.push(JSON.stringify(obj2) + '\n');
108115
return stream;
@@ -149,27 +156,20 @@ describe('Watch', () => {
149156

150157
expect(doneCalled).to.equal(0);
151158

152-
// TODO check after node-fetch fix
153-
// https://github.com/node-fetch/node-fetch/issues/1231
154-
// https://github.com/node-fetch/node-fetch/issues/1721
155-
156-
// const errIn = { error: 'err' };
157-
// stream.emit('error', errIn);
158-
// expect(doneErr).to.deep.equal(errIn);
159-
// await donePromise;
160-
// expect(doneCalled).to.equal(1);
161-
162-
stream.end();
159+
const errIn = new Error('err');
160+
(response as IncomingMessage).socket.destroy(errIn);
163161

164162
await donePromise;
165163

166164
expect(doneCalled).to.equal(1);
165+
expect(doneErr).to.deep.equal(errIn);
166+
167167
s.done();
168+
169+
stream.destroy();
168170
});
169171

170-
// https://github.com/node-fetch/node-fetch/issues/1231
171-
// https://github.com/node-fetch/node-fetch/issues/1721
172-
it.skip('should handle errors correctly', async () => {
172+
it('should handle server errors correctly', async () => {
173173
const kc = new KubeConfig();
174174
Object.assign(kc, fakeConfig);
175175
const watch = new Watch(kc);
@@ -181,15 +181,18 @@ describe('Watch', () => {
181181
},
182182
};
183183

184-
const errIn = { error: 'err' };
185-
186184
const stream = new PassThrough();
187185

188186
const [scope] = systemUnderTest();
189187

190188
const path = '/some/path/to/object?watch=true';
191189

192-
const s = scope.get(path).reply(200, () => {
190+
let response: IncomingMessage | undefined;
191+
192+
const s = scope.get(path).reply(200, function (): PassThrough {
193+
this.req.on('response', (r) => {
194+
response = r;
195+
});
193196
stream.push(JSON.stringify(obj1) + '\n');
194197
return stream;
195198
});
@@ -198,6 +201,11 @@ describe('Watch', () => {
198201
const receivedObjects: string[] = [];
199202
const doneErr: any[] = [];
200203

204+
let handledAllObjectsResolve: any;
205+
const handledAllObjectsPromise = new Promise((resolve) => {
206+
handledAllObjectsResolve = resolve;
207+
});
208+
201209
let doneResolve: any;
202210
const donePromise = new Promise((resolve) => {
203211
doneResolve = resolve;
@@ -209,29 +217,37 @@ describe('Watch', () => {
209217
(phase: string, obj: string) => {
210218
receivedTypes.push(phase);
211219
receivedObjects.push(obj);
220+
if (receivedObjects.length === 1) {
221+
handledAllObjectsResolve();
222+
}
212223
},
213224
(err: any) => {
214225
doneErr.push(err);
215226
doneResolve();
216227
},
217228
);
218229

219-
stream.emit('error', errIn);
220-
221-
await donePromise;
230+
await handledAllObjectsPromise;
222231

223232
expect(receivedTypes).to.deep.equal([obj1.type]);
224233
expect(receivedObjects).to.deep.equal([obj1.object]);
225234

235+
expect(doneErr.length).to.equal(0);
236+
237+
const errIn = new Error('err');
238+
(response as IncomingMessage).socket.destroy(errIn);
239+
240+
await donePromise;
241+
226242
expect(doneErr.length).to.equal(1);
227243
expect(doneErr[0]).to.deep.equal(errIn);
228244

229245
s.done();
246+
247+
stream.destroy();
230248
});
231249

232-
// https://github.com/node-fetch/node-fetch/issues/1231
233-
// https://github.com/node-fetch/node-fetch/issues/1721
234-
it.skip('should handle server side close correctly', async () => {
250+
it('should handle server side close correctly', async () => {
235251
const kc = new KubeConfig();
236252
Object.assign(kc, fakeConfig);
237253
const watch = new Watch(kc);
@@ -249,7 +265,12 @@ describe('Watch', () => {
249265

250266
const path = '/some/path/to/object?watch=true';
251267

252-
const s = scope.get(path).reply(200, () => {
268+
let response: IncomingMessage | undefined;
269+
270+
const s = scope.get(path).reply(200, function (): PassThrough {
271+
this.req.on('response', (r) => {
272+
response = r;
273+
});
253274
stream.push(JSON.stringify(obj1) + '\n');
254275
return stream;
255276
});
@@ -258,6 +279,11 @@ describe('Watch', () => {
258279
const receivedObjects: string[] = [];
259280
const doneErr: any[] = [];
260281

282+
let handledAllObjectsResolve: any;
283+
const handledAllObjectsPromise = new Promise((resolve) => {
284+
handledAllObjectsResolve = resolve;
285+
});
286+
261287
let doneResolve: any;
262288
const donePromise = new Promise((resolve) => {
263289
doneResolve = resolve;
@@ -269,24 +295,33 @@ describe('Watch', () => {
269295
(phase: string, obj: string) => {
270296
receivedTypes.push(phase);
271297
receivedObjects.push(obj);
298+
if (receivedObjects.length === 1) {
299+
handledAllObjectsResolve();
300+
}
272301
},
273302
(err: any) => {
274303
doneErr.push(err);
275304
doneResolve();
276305
},
277306
);
278307

279-
stream.emit('close');
280-
281-
await donePromise;
308+
await handledAllObjectsPromise;
282309

283310
expect(receivedTypes).to.deep.equal([obj1.type]);
284311
expect(receivedObjects).to.deep.equal([obj1.object]);
285312

313+
expect(doneErr.length).to.equal(0);
314+
315+
stream.end();
316+
317+
await donePromise;
318+
286319
expect(doneErr.length).to.equal(1);
287-
expect(doneErr[0]).to.be.null;
320+
expect(doneErr[0]).to.equal(null);
288321

289322
s.done();
323+
324+
stream.destroy();
290325
});
291326

292327
it('should ignore JSON parse errors', async () => {

0 commit comments

Comments
 (0)