Skip to content

Commit

Permalink
Merge pull request #19 from hisco/fix-abort
Browse files Browse the repository at this point in the history
synced interface with http1
  • Loading branch information
hisco committed Jul 31, 2019
2 parents 6231573 + 06e6557 commit b83d1d8
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ deploy:
provider: npm
email: hisco@googlegroups.com
api_key:
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=
secure: OaMiQbh8vZLWjQ3uoALTwSFiWO0lOKFZUR6m2+g00UwIgukXev9CF/WQGWeW3Z4Gj57fZyuyHTys7NsGNmfSvwPqm63HTLGTy8aUuG7xVEDdjcGepqPf19J0s0TGW1Y7vxzvwkBDmV3x9qBFGwmjRjsQ+vQ/jQ2XX5DlCy2NqSyTwcGDsMM09worRAmfc/Q5PGn+sngl0LQFd0vSVWAI9M5kztUTxl2xQFH80Nt6j11r8R1+aqkHhHuJLuYHZGAV9Xf6VjrJ3e2b29SeeTmii1EnphJsljHzjoEpluuGwfQIdOEpbSfXqVxQR1RyFfVR/KE0kJppKLucykPz4PA+e5Ak9hTFt5D44/wHDsDNPpGAlLCdKSzkPciPk3gPHR310m0B5Mb01fAEkCZ9AEQuc/Q7rJtthE9eriK7741Zin5rtxfInMyrwTfVpKyuwBoqlMZjBzuo9EfnaUXZ0VrccX9cu0yz4qXGzziYoMmBDZ/wYaXnzWT9d6Q95nFxetC9l7KoLBUQM/TtEhmiPTJcd3hj75/cuAA6/XWTs6yQ7il5DgstvnHPxbpCnH/eedFTubRv5amSmI97r3als0B4m7bCWFG3M8ZJB1WovZ+Rymzw0Fqb9nt4eeiJE1m1qqZLUn0/eo4T0wbAUyPx2Zq0RKFvjqKfRaQXDE/TeJSGOfc=
on:
tags: true
repo: hisco/http2-client
46 changes: 23 additions & 23 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@ const {DebounceTimers , assertIsObject , ERR_INVALID_ARG_TYPE} = require('./util
const {initializeTLSOptions } = require('./request-options');
const http = require('http');
const https = require('https');
const {Stream} = require('stream');
function addFunctions(container , obj){
const proto = obj.prototype;
Object.keys(proto).forEach((name)=>{
if (container.indexOf(name)!=-1)
return;
if (name.indexOf('_')!=0 && typeof proto[name] == 'function'){
container.push(name);
}
})
}
const STUBBED_METHODS_NAME = [
]
//We need to proxy all v1 function
addFunctions(STUBBED_METHODS_NAME, http.ClientRequest);
addFunctions(STUBBED_METHODS_NAME, http.OutgoingMessage);
addFunctions(STUBBED_METHODS_NAME, EventEmitter);
addFunctions(STUBBED_METHODS_NAME, Stream);

const STUBBED_METHODS_NAME = [
'emit',
'write',
'end',
'pipe',
'removeListener',
'removeListeners',
'setTimeout',
'setEncoding',
'close',
'abort',
'priority',
'sendTrailers',
'setNoDelay',
'setSocketKeepAlive',
'clearTimeout'
];
const PROPERTIES_TO_PROXY = [
'httpVersionMajor',
'httpVersionMinor',
Expand All @@ -38,9 +39,11 @@ function ClientRequest(){
this[$stubs] = [];
for (var i=0;i<STUBBED_METHODS_NAME.length;i++){
let name = STUBBED_METHODS_NAME[i];
this[name] = function method(){
return this.genericStubber(name , arguments);
}.bind(this);
if (!ClientRequest.prototype[name]){
this[name] = function method(){
return this.genericStubber(name , arguments);
}.bind(this);
}
}

var requestOptions , cb,url , args;
Expand Down Expand Up @@ -151,9 +154,6 @@ ClientRequest.prototype = {
this.genericStubber('once' , arguments);

},
abort(cb){
this.close(0x08 , cb);
},
take(stream){
//We forward all functions to the stream
for (var i=0;i<STUBBED_METHODS_NAME.length;i++){
Expand Down
214 changes: 212 additions & 2 deletions test/e2e/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,18 @@ const onHttpServerReady = new Promise((resolve , reject)=>{
}
});
const onHTTP2ServerReady = new Promise((resolve , reject)=>{
http2Debug = new Http2Debug;
class HTTP2StubServer extends Http2Debug{
onStream(stream , headers){
const args = arguments;
if (headers[':path'].indexOf('delay')!=-1){
setTimeout(()=> super.onStream.apply(this,args),100 )
}
else{
super.onStream.apply(this,args);
}
}
}
http2Debug = new HTTP2StubServer;
http2Debug.createServer((err)=>{
if (err)
return reject(err);
Expand Down Expand Up @@ -258,7 +269,13 @@ describe('e2e' , ()=>{
})
req.end();
req.setTimeout(1,()=>{
req.abort()
try{
req.abort();
resolve();
}
catch(err){
reject(err)
}
})
})
});
Expand Down Expand Up @@ -294,6 +311,36 @@ describe('e2e' , ()=>{
req.end();
})
});

it('Should be able to abort immediately' , ()=>{
return new Promise((resolve , reject)=>{
const req = request({
path : '/delay',
host : SERVER_HOST,
port : HTTP2_PORT,
method : 'delete',
headers : {
'tesT-me' :'90'
}
} , (res)=>{
reject(new Error('Rejected request shouldn\'t respond'))
});
req.on('error' , (err)=>{
try{
expect(err.code).eq('ECONNRESET');
resolve();
}
catch(err){
reject(err);
}
})
req.end();
req.setTimeout(1,()=>{
req.abort()
})
})
});

it('Should be able to make request with request options and url as string' , ()=>{
return new Promise((resolve , reject)=>{
const req = request(HTTP2_URL , {
Expand Down Expand Up @@ -342,6 +389,169 @@ describe('e2e' , ()=>{
});
});
});

describe('validate http1 interface as assumed' , ()=>{
describe('http1' , ()=>{
it('Should be able to make request with request options as a string' , ()=>{
return new Promise((resolve , reject)=>{
const req = require('http').request(`${HTTP_URL}/test1` , (res)=>{
getBody(res)
.then((bodyRaw)=>{
const json = JSON.parse(bodyRaw);
expect(res.statusCode).eq(200);
expect(json.path).eq('/test1');
expect(json.method).eq('GET');
resolve();
})
.catch((err)=>{
reject(err)
})
});
req.end();
})
});
/*
For now disabled the following test after verified that
It's working on various machines and on travis only it doesn't.
If someone have idea why only travis then help is welcome.
*/
// it('Should be able to make request with request options and url as a string' , ()=>{
// return new Promise((resolve , reject)=>{
// const req = require('http').request(
// `${HTTP_URL}/test1` ,
// { method : 'POST'},
// (res)=>{
// getBody(res)
// .then((bodyRaw)=>{
// const json = JSON.parse(bodyRaw);
// expect(res.statusCode).eq(200);
// expect(json.path).eq('/test1');
// expect(json.method).eq('POST');
// expect(json.body.test).eq(1);
// resolve();
// })
// .catch((err)=>{
// reject(err)
// })
// });
// req.write('{"test":1}')
// req.end();
// })
// });
it('Should be able to make request with request options and method lowercase' , ()=>{
return new Promise((resolve , reject)=>{
const req = require('http').request({
path : '/test1',
host : SERVER_HOST,
port : HTTP_PORT,
method : 'post'
} , (res)=>{
getBody(res)
.then((bodyRaw)=>{
const json = JSON.parse(bodyRaw);
expect(res.statusCode).eq(200);
expect(json.path).eq('/test1');
expect(json.method).eq('POST');
resolve();
})
.catch((err)=>{
reject(err)
})
});
req.end();
})
});
it('Should be able to make request with request options and headers' , ()=>{
return new Promise((resolve , reject)=>{
const req = require('http').request({
path : '/test1',
host : SERVER_HOST,
port : HTTP_PORT,
method : 'delete',
headers : {
'tesT-me' :'90'
}
} , (res)=>{
getBody(res)
.then((bodyRaw)=>{
const json = JSON.parse(bodyRaw);
expect(json.headers['test-me']).eq('90');
expect(res.statusCode).eq(200);
expect(json.path).eq('/test1');
expect(json.method).eq('DELETE');
resolve();
})
.catch((err)=>{
reject(err)
})
});
req.end();
})
});
it('Should be able to abort immediately' , ()=>{
return new Promise((resolve , reject)=>{
const req = require('http').request({
path : '/delay',
host : SERVER_HOST,
port : HTTP_PORT,
method : 'delete',
headers : {
'tesT-me' :'90'
}
} , (res)=>{
reject(new Error('Rejected request shouldn\'t respond'))
});
req.on('error' , (err)=>{
try{
expect(err.code).eq('ECONNRESET');
resolve();
}
catch(err){
reject(err);
}
})
req.end();
req.setTimeout(1,()=>{
req.abort()
})
})
});
it('Should emit' , ()=>{
return new Promise((resolve , reject)=>{
const req = require('http').request({
path : '/delay',
host : SERVER_HOST,
port : HTTP_PORT,
method : 'delete',
headers : {
'tesT-me' :'90'
}
} , (res)=>{
reject(new Error('Rejected request shouldn\'t respond'))
});
req.on('error' , (err)=>{
try{
expect(err.code).eq('ECONNRESET');
resolve();
}
catch(err){
reject(err);
}
})
req.end();
req.setTimeout(1,()=>{
try{
req.abort();
resolve();
}
catch(err){
reject(err)
}
})
})
});
});
});

describe('get' , ()=>{
before(()=>{
Expand Down

0 comments on commit b83d1d8

Please sign in to comment.