Skip to content

Commit 7605daa

Browse files
committed
synced interface with http1
1 parent 71171b9 commit 7605daa

3 files changed

Lines changed: 228 additions & 26 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ deploy:
1515
provider: npm
1616
email: hisco@googlegroups.com
1717
api_key:
18-
secure: nRUpJEI+FDAse92QoPxaJMPpKzaoAZXyt7h2isaJrDCanuAw+vQL7cJDsTWCYEqtezduiUJ+CmxnhVnJKoqAyA1QyTpqrlHQ+wyMMXFkv1iAs37f+h9F/3dPSiwI06DJJiZ5BLh8RFcBQjYQRDotfr39vaj3SbGmQKNhIoOfQJlBQK5Xn0EKMOJepPKLKs/YTcjhwqGlyC+DaPRiuzRwFq6hJaGM4HU+avHh6jC7hm5oqnQY3EsM7SSmXSc6ZynY+lX4lONdMVl2AJpxeMwXIbphRfHfZB9vESJ2/MfAos25NnTna9OAt7m9VHunmKviGRxyqDbRuLcizs+LBHp3xCvW3cF2CJGi4K6hAN/AtCJhHCMwy5kCIamgcW9cBbJhOeJyXs9CjZhFMIznr7BD8wBiEXkieqHKCmiL0i8xBLE/hact1ZTd8H2cE/x/dDzm2lywnqa/Taud5xmblTjCtu09xFEeye2ON8xYSnrVaqKxxn26mNkejG5RIbG0a+54VrEkYIMbJT1x81S7tcW6dva4ZeqrrbcfbJrnB7Ldn7D4oM5Cv+X5+RBXU1zRX+mmP50S0OQvJevVCpV35VgTMmRbAkTLUEUk9JnNBE6+0UUYs0hwv0h5LzCietOGS3Lrw+rjRBYmxL7OvQqVO6JIzQ4bEA5vDa0lzQ4ECNP2JKM=
18+
secure: OaMiQbh8vZLWjQ3uoALTwSFiWO0lOKFZUR6m2+g00UwIgukXev9CF/WQGWeW3Z4Gj57fZyuyHTys7NsGNmfSvwPqm63HTLGTy8aUuG7xVEDdjcGepqPf19J0s0TGW1Y7vxzvwkBDmV3x9qBFGwmjRjsQ+vQ/jQ2XX5DlCy2NqSyTwcGDsMM09worRAmfc/Q5PGn+sngl0LQFd0vSVWAI9M5kztUTxl2xQFH80Nt6j11r8R1+aqkHhHuJLuYHZGAV9Xf6VjrJ3e2b29SeeTmii1EnphJsljHzjoEpluuGwfQIdOEpbSfXqVxQR1RyFfVR/KE0kJppKLucykPz4PA+e5Ak9hTFt5D44/wHDsDNPpGAlLCdKSzkPciPk3gPHR310m0B5Mb01fAEkCZ9AEQuc/Q7rJtthE9eriK7741Zin5rtxfInMyrwTfVpKyuwBoqlMZjBzuo9EfnaUXZ0VrccX9cu0yz4qXGzziYoMmBDZ/wYaXnzWT9d6Q95nFxetC9l7KoLBUQM/TtEhmiPTJcd3hj75/cuAA6/XWTs6yQ7il5DgstvnHPxbpCnH/eedFTubRv5amSmI97r3als0B4m7bCWFG3M8ZJB1WovZ+Rymzw0Fqb9nt4eeiJE1m1qqZLUn0/eo4T0wbAUyPx2Zq0RKFvjqKfRaQXDE/TeJSGOfc=
1919
on:
2020
tags: true
2121
repo: hisco/http2-client

lib/request.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,25 @@ const {DebounceTimers , assertIsObject , ERR_INVALID_ARG_TYPE} = require('./util
66
const {initializeTLSOptions } = require('./request-options');
77
const http = require('http');
88
const https = require('https');
9+
const {Stream} = require('stream');
10+
function addFunctions(container , obj){
11+
const proto = obj.prototype;
12+
Object.keys(proto).forEach((name)=>{
13+
if (container.indexOf(name)!=-1)
14+
return;
15+
if (name.indexOf('_')!=0 && typeof proto[name] == 'function'){
16+
container.push(name);
17+
}
18+
})
19+
}
20+
const STUBBED_METHODS_NAME = [
21+
]
22+
//We need to proxy all v1 function
23+
addFunctions(STUBBED_METHODS_NAME, http.ClientRequest);
24+
addFunctions(STUBBED_METHODS_NAME, http.OutgoingMessage);
25+
addFunctions(STUBBED_METHODS_NAME, EventEmitter);
26+
addFunctions(STUBBED_METHODS_NAME, Stream);
927

10-
const STUBBED_METHODS_NAME = [
11-
'emit',
12-
'write',
13-
'end',
14-
'pipe',
15-
'removeListener',
16-
'removeListeners',
17-
'setTimeout',
18-
'setEncoding',
19-
'close',
20-
'abort',
21-
'priority',
22-
'sendTrailers',
23-
'setNoDelay',
24-
'setSocketKeepAlive',
25-
'clearTimeout'
26-
];
2728
const PROPERTIES_TO_PROXY = [
2829
'httpVersionMajor',
2930
'httpVersionMinor',
@@ -36,9 +37,11 @@ function ClientRequest(){
3637
this[$stubs] = [];
3738
for (var i=0;i<STUBBED_METHODS_NAME.length;i++){
3839
let name = STUBBED_METHODS_NAME[i];
39-
this[name] = function method(){
40-
return this.genericStubber(name , arguments);
41-
}.bind(this);
40+
if (!ClientRequest.prototype[name]){
41+
this[name] = function method(){
42+
return this.genericStubber(name , arguments);
43+
}.bind(this);
44+
}
4245
}
4346

4447
var requestOptions , cb,url , args;
@@ -149,9 +152,6 @@ ClientRequest.prototype = {
149152
this.genericStubber('once' , arguments);
150153

151154
},
152-
abort(cb){
153-
this.close(0x08 , cb);
154-
},
155155
take(stream){
156156
//We forward all functions to the stream
157157
for (var i=0;i<STUBBED_METHODS_NAME.length;i++){

test/e2e/request.js

Lines changed: 204 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,18 @@ const onHttpServerReady = new Promise((resolve , reject)=>{
5858
}
5959
});
6060
const onHTTP2ServerReady = new Promise((resolve , reject)=>{
61-
http2Debug = new Http2Debug;
61+
class HTTP2StubServer extends Http2Debug{
62+
onStream(stream , headers){
63+
const args = arguments;
64+
if (headers[':path'].indexOf('delay')!=-1){
65+
setTimeout(()=> super.onStream.apply(this,args),100 )
66+
}
67+
else{
68+
super.onStream.apply(this,args);
69+
}
70+
}
71+
}
72+
http2Debug = new HTTP2StubServer;
6273
http2Debug.createServer((err)=>{
6374
if (err)
6475
return reject(err);
@@ -258,7 +269,13 @@ describe('e2e' , ()=>{
258269
})
259270
req.end();
260271
req.setTimeout(1,()=>{
261-
req.abort()
272+
try{
273+
req.abort();
274+
resolve();
275+
}
276+
catch(err){
277+
reject(err)
278+
}
262279
})
263280
})
264281
});
@@ -294,6 +311,36 @@ describe('e2e' , ()=>{
294311
req.end();
295312
})
296313
});
314+
315+
it('Should be able to abort immediately' , ()=>{
316+
return new Promise((resolve , reject)=>{
317+
const req = request({
318+
path : '/delay',
319+
host : SERVER_HOST,
320+
port : HTTP2_PORT,
321+
method : 'delete',
322+
headers : {
323+
'tesT-me' :'90'
324+
}
325+
} , (res)=>{
326+
reject(new Error('Rejected request shouldn\'t respond'))
327+
});
328+
req.on('error' , (err)=>{
329+
try{
330+
expect(err.code).eq('ECONNRESET');
331+
resolve();
332+
}
333+
catch(err){
334+
reject(err);
335+
}
336+
})
337+
req.end();
338+
req.setTimeout(1,()=>{
339+
req.abort()
340+
})
341+
})
342+
});
343+
297344
it('Should be able to make request with request options and url as string' , ()=>{
298345
return new Promise((resolve , reject)=>{
299346
const req = request(HTTP2_URL , {
@@ -342,6 +389,161 @@ describe('e2e' , ()=>{
342389
});
343390
});
344391
});
392+
393+
describe('validate http1 interface as assumed' , ()=>{
394+
describe('http1' , ()=>{
395+
it('Should be able to make request with request options as a string' , ()=>{
396+
return new Promise((resolve , reject)=>{
397+
const req = require('http').request(`${HTTP_URL}/test1` , (res)=>{
398+
getBody(res)
399+
.then((bodyRaw)=>{
400+
const json = JSON.parse(bodyRaw);
401+
expect(res.statusCode).eq(200);
402+
expect(json.path).eq('/test1');
403+
expect(json.method).eq('GET');
404+
resolve();
405+
})
406+
.catch((err)=>{
407+
reject(err)
408+
})
409+
});
410+
req.end();
411+
})
412+
});
413+
it('Should be able to make request with request options and url as a string' , ()=>{
414+
return new Promise((resolve , reject)=>{
415+
const req = require('http').request(`${HTTP_URL}/test1` , { method : 'POST'}, (res)=>{
416+
getBody(res)
417+
.then((bodyRaw)=>{
418+
const json = JSON.parse(bodyRaw);
419+
expect(res.statusCode).eq(200);
420+
expect(json.path).eq('/test1');
421+
expect(json.method).eq('POST');
422+
expect(json.body.test).eq(1);
423+
resolve();
424+
})
425+
.catch((err)=>{
426+
reject(err)
427+
})
428+
});
429+
req.write('{"test":1}')
430+
req.end();
431+
})
432+
});
433+
it('Should be able to make request with request options and method lowercase' , ()=>{
434+
return new Promise((resolve , reject)=>{
435+
const req = require('http').request({
436+
path : '/test1',
437+
host : SERVER_HOST,
438+
port : HTTP_PORT,
439+
method : 'post'
440+
} , (res)=>{
441+
getBody(res)
442+
.then((bodyRaw)=>{
443+
const json = JSON.parse(bodyRaw);
444+
expect(res.statusCode).eq(200);
445+
expect(json.path).eq('/test1');
446+
expect(json.method).eq('POST');
447+
resolve();
448+
})
449+
.catch((err)=>{
450+
reject(err)
451+
})
452+
});
453+
req.end();
454+
})
455+
});
456+
it('Should be able to make request with request options and headers' , ()=>{
457+
return new Promise((resolve , reject)=>{
458+
const req = require('http').request({
459+
path : '/test1',
460+
host : SERVER_HOST,
461+
port : HTTP_PORT,
462+
method : 'delete',
463+
headers : {
464+
'tesT-me' :'90'
465+
}
466+
} , (res)=>{
467+
getBody(res)
468+
.then((bodyRaw)=>{
469+
const json = JSON.parse(bodyRaw);
470+
expect(json.headers['test-me']).eq('90');
471+
expect(res.statusCode).eq(200);
472+
expect(json.path).eq('/test1');
473+
expect(json.method).eq('DELETE');
474+
resolve();
475+
})
476+
.catch((err)=>{
477+
reject(err)
478+
})
479+
});
480+
req.end();
481+
})
482+
});
483+
it('Should be able to abort immediately' , ()=>{
484+
return new Promise((resolve , reject)=>{
485+
const req = require('http').request({
486+
path : '/delay',
487+
host : SERVER_HOST,
488+
port : HTTP_PORT,
489+
method : 'delete',
490+
headers : {
491+
'tesT-me' :'90'
492+
}
493+
} , (res)=>{
494+
reject(new Error('Rejected request shouldn\'t respond'))
495+
});
496+
req.on('error' , (err)=>{
497+
try{
498+
expect(err.code).eq('ECONNRESET');
499+
resolve();
500+
}
501+
catch(err){
502+
reject(err);
503+
}
504+
})
505+
req.end();
506+
req.setTimeout(1,()=>{
507+
req.abort()
508+
})
509+
})
510+
});
511+
it('Should emit' , ()=>{
512+
return new Promise((resolve , reject)=>{
513+
const req = require('http').request({
514+
path : '/delay',
515+
host : SERVER_HOST,
516+
port : HTTP_PORT,
517+
method : 'delete',
518+
headers : {
519+
'tesT-me' :'90'
520+
}
521+
} , (res)=>{
522+
reject(new Error('Rejected request shouldn\'t respond'))
523+
});
524+
req.on('error' , (err)=>{
525+
try{
526+
expect(err.code).eq('ECONNRESET');
527+
resolve();
528+
}
529+
catch(err){
530+
reject(err);
531+
}
532+
})
533+
req.end();
534+
req.setTimeout(1,()=>{
535+
try{
536+
req.abort();
537+
resolve();
538+
}
539+
catch(err){
540+
reject(err)
541+
}
542+
})
543+
})
544+
});
545+
});
546+
});
345547

346548
describe('get' , ()=>{
347549
before(()=>{

0 commit comments

Comments
 (0)