@@ -6,6 +6,7 @@ import { PassThrough } from 'stream';
6
6
import { KubeConfig } from './config' ;
7
7
import { Cluster , Context , User } from './config_types' ;
8
8
import { Watch } from './watch' ;
9
+ import { IncomingMessage } from 'http' ;
9
10
10
11
const server = 'http://foo.company.com' ;
11
12
@@ -60,6 +61,7 @@ describe('Watch', () => {
60
61
await watch . watch (
61
62
path ,
62
63
{ } ,
64
+ // tslint:disable-next-line:no-empty
63
65
( phase : string , obj : string ) => { } ,
64
66
( err : any ) => {
65
67
doneCalled = true ;
@@ -96,13 +98,18 @@ describe('Watch', () => {
96
98
97
99
const [ scope ] = systemUnderTest ( ) ;
98
100
101
+ let response : IncomingMessage | undefined ;
102
+
99
103
const s = scope
100
104
. get ( path )
101
105
. query ( {
102
106
watch : 'true' ,
103
107
a : 'b' ,
104
108
} )
105
- . reply ( 200 , ( ) => {
109
+ . reply ( 200 , function ( ) : PassThrough {
110
+ this . req . on ( 'response' , ( r ) => {
111
+ response = r ;
112
+ } ) ;
106
113
stream . push ( JSON . stringify ( obj1 ) + '\n' ) ;
107
114
stream . push ( JSON . stringify ( obj2 ) + '\n' ) ;
108
115
return stream ;
@@ -149,27 +156,20 @@ describe('Watch', () => {
149
156
150
157
expect ( doneCalled ) . to . equal ( 0 ) ;
151
158
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 ) ;
163
161
164
162
await donePromise ;
165
163
166
164
expect ( doneCalled ) . to . equal ( 1 ) ;
165
+ expect ( doneErr ) . to . deep . equal ( errIn ) ;
166
+
167
167
s . done ( ) ;
168
+
169
+ stream . destroy ( ) ;
168
170
} ) ;
169
171
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 ( ) => {
173
173
const kc = new KubeConfig ( ) ;
174
174
Object . assign ( kc , fakeConfig ) ;
175
175
const watch = new Watch ( kc ) ;
@@ -181,15 +181,18 @@ describe('Watch', () => {
181
181
} ,
182
182
} ;
183
183
184
- const errIn = { error : 'err' } ;
185
-
186
184
const stream = new PassThrough ( ) ;
187
185
188
186
const [ scope ] = systemUnderTest ( ) ;
189
187
190
188
const path = '/some/path/to/object?watch=true' ;
191
189
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
+ } ) ;
193
196
stream . push ( JSON . stringify ( obj1 ) + '\n' ) ;
194
197
return stream ;
195
198
} ) ;
@@ -198,6 +201,11 @@ describe('Watch', () => {
198
201
const receivedObjects : string [ ] = [ ] ;
199
202
const doneErr : any [ ] = [ ] ;
200
203
204
+ let handledAllObjectsResolve : any ;
205
+ const handledAllObjectsPromise = new Promise ( ( resolve ) => {
206
+ handledAllObjectsResolve = resolve ;
207
+ } ) ;
208
+
201
209
let doneResolve : any ;
202
210
const donePromise = new Promise ( ( resolve ) => {
203
211
doneResolve = resolve ;
@@ -209,29 +217,37 @@ describe('Watch', () => {
209
217
( phase : string , obj : string ) => {
210
218
receivedTypes . push ( phase ) ;
211
219
receivedObjects . push ( obj ) ;
220
+ if ( receivedObjects . length === 1 ) {
221
+ handledAllObjectsResolve ( ) ;
222
+ }
212
223
} ,
213
224
( err : any ) => {
214
225
doneErr . push ( err ) ;
215
226
doneResolve ( ) ;
216
227
} ,
217
228
) ;
218
229
219
- stream . emit ( 'error' , errIn ) ;
220
-
221
- await donePromise ;
230
+ await handledAllObjectsPromise ;
222
231
223
232
expect ( receivedTypes ) . to . deep . equal ( [ obj1 . type ] ) ;
224
233
expect ( receivedObjects ) . to . deep . equal ( [ obj1 . object ] ) ;
225
234
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
+
226
242
expect ( doneErr . length ) . to . equal ( 1 ) ;
227
243
expect ( doneErr [ 0 ] ) . to . deep . equal ( errIn ) ;
228
244
229
245
s . done ( ) ;
246
+
247
+ stream . destroy ( ) ;
230
248
} ) ;
231
249
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 ( ) => {
235
251
const kc = new KubeConfig ( ) ;
236
252
Object . assign ( kc , fakeConfig ) ;
237
253
const watch = new Watch ( kc ) ;
@@ -249,7 +265,12 @@ describe('Watch', () => {
249
265
250
266
const path = '/some/path/to/object?watch=true' ;
251
267
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
+ } ) ;
253
274
stream . push ( JSON . stringify ( obj1 ) + '\n' ) ;
254
275
return stream ;
255
276
} ) ;
@@ -258,6 +279,11 @@ describe('Watch', () => {
258
279
const receivedObjects : string [ ] = [ ] ;
259
280
const doneErr : any [ ] = [ ] ;
260
281
282
+ let handledAllObjectsResolve : any ;
283
+ const handledAllObjectsPromise = new Promise ( ( resolve ) => {
284
+ handledAllObjectsResolve = resolve ;
285
+ } ) ;
286
+
261
287
let doneResolve : any ;
262
288
const donePromise = new Promise ( ( resolve ) => {
263
289
doneResolve = resolve ;
@@ -269,24 +295,33 @@ describe('Watch', () => {
269
295
( phase : string , obj : string ) => {
270
296
receivedTypes . push ( phase ) ;
271
297
receivedObjects . push ( obj ) ;
298
+ if ( receivedObjects . length === 1 ) {
299
+ handledAllObjectsResolve ( ) ;
300
+ }
272
301
} ,
273
302
( err : any ) => {
274
303
doneErr . push ( err ) ;
275
304
doneResolve ( ) ;
276
305
} ,
277
306
) ;
278
307
279
- stream . emit ( 'close' ) ;
280
-
281
- await donePromise ;
308
+ await handledAllObjectsPromise ;
282
309
283
310
expect ( receivedTypes ) . to . deep . equal ( [ obj1 . type ] ) ;
284
311
expect ( receivedObjects ) . to . deep . equal ( [ obj1 . object ] ) ;
285
312
313
+ expect ( doneErr . length ) . to . equal ( 0 ) ;
314
+
315
+ stream . end ( ) ;
316
+
317
+ await donePromise ;
318
+
286
319
expect ( doneErr . length ) . to . equal ( 1 ) ;
287
- expect ( doneErr [ 0 ] ) . to . be . null ;
320
+ expect ( doneErr [ 0 ] ) . to . equal ( null ) ;
288
321
289
322
s . done ( ) ;
323
+
324
+ stream . destroy ( ) ;
290
325
} ) ;
291
326
292
327
it ( 'should ignore JSON parse errors' , async ( ) => {
0 commit comments