forked from request/request
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for http-2 specific error handling
- Loading branch information
1 parent
632f6bf
commit 5595da0
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict' | ||
|
||
var request = require('../index').defaults({ protocolVersion: 'auto' }) | ||
var tape = require('tape') | ||
var server = require('./server') | ||
var destroyable = require('server-destroy') | ||
|
||
const s = server.createHttp2Server(); | ||
|
||
destroyable(s) | ||
|
||
tape('setup', function (t) { | ||
s.listen(0, function () { | ||
t.end() | ||
}) | ||
}); | ||
|
||
function addTest (errorCode, data = {}) { | ||
tape('test ' + errorCode, function (t) { | ||
s.on('/' + errorCode, function (req, res) { | ||
if (errorCode === 0) { | ||
res.end(); | ||
return; | ||
} | ||
res.stream.close(errorCode); | ||
}) | ||
data.uri = s.url + '/' + errorCode | ||
request( | ||
{ ...data, strictSSL: false, protocolVersion: 'auto' }, | ||
function (err) { | ||
if (errorCode === 0){ | ||
t.equal(err, null) | ||
t.end() | ||
return | ||
} | ||
if(errorCode === 8){ | ||
t.equal(err.message, `HTTP/2 Stream closed with error code NGHTTP2_CANCEL`) | ||
t.end() | ||
return | ||
} | ||
t.equal(err.message, `Stream closed with error code ${errorCodes[errorCode]}`) | ||
t.end() | ||
} | ||
) | ||
}) | ||
} | ||
|
||
const errorCodes = [ | ||
"NGHTTP2_NO_ERROR", | ||
"NGHTTP2_PROTOCOL_ERROR", | ||
"NGHTTP2_INTERNAL_ERROR", | ||
"NGHTTP2_FLOW_CONTROL_ERROR", | ||
"NGHTTP2_SETTINGS_TIMEOUT", | ||
"NGHTTP2_STREAM_CLOSED", | ||
"NGHTTP2_FRAME_SIZE_ERROR", | ||
"NGHTTP2_REFUSED_STREAM", | ||
"NGHTTP2_CANCEL", | ||
"NGHTTP2_COMPRESSION_ERROR", | ||
"NGHTTP2_CONNECT_ERROR", | ||
"NGHTTP2_ENHANCE_YOUR_CALM", | ||
"NGHTTP2_INADEQUATE_SECURITY", | ||
"NGHTTP2_HTTP_1_1_REQUIRED" | ||
] | ||
|
||
for(let i = 0; i < errorCodes.length; i++){ | ||
addTest(i); | ||
} | ||
|
||
tape('cleanup', function(t){ | ||
s.destroy(function(){ | ||
t.end() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict' | ||
|
||
var request = require('../index').defaults({ protocolVersion: 'http2' }) | ||
var tape = require('tape') | ||
var server = require('./server') | ||
var destroyable = require('server-destroy') | ||
|
||
const s = server.createHttp2Server(); | ||
|
||
destroyable(s) | ||
|
||
tape('setup', function (t) { | ||
s.listen(0, function () { | ||
t.end() | ||
}) | ||
}); | ||
|
||
function addTest (errorCode, data = {}) { | ||
tape('test ' + errorCode, function (t) { | ||
s.on('/' + errorCode, function (req, res) { | ||
if (errorCode === 0) { | ||
res.end(); | ||
return; | ||
} | ||
res.stream.close(errorCode); | ||
}) | ||
data.uri = s.url + '/' + errorCode | ||
request( | ||
{ ...data, strictSSL: false, protocolVersion: 'http2' }, | ||
function (err, resp, body) { | ||
if (errorCode === 0){ | ||
t.equal(err, null) | ||
t.end() | ||
return | ||
} | ||
if(errorCode === 8){ | ||
t.equal(err.message, `HTTP/2 Stream closed with error code NGHTTP2_CANCEL`) | ||
t.end() | ||
return | ||
} | ||
t.equal(err.message, `Stream closed with error code ${errorCodes[errorCode]}`) | ||
t.end() | ||
} | ||
) | ||
}) | ||
} | ||
|
||
const errorCodes = [ | ||
"NGHTTP2_NO_ERROR", | ||
"NGHTTP2_PROTOCOL_ERROR", | ||
"NGHTTP2_INTERNAL_ERROR", | ||
"NGHTTP2_FLOW_CONTROL_ERROR", | ||
"NGHTTP2_SETTINGS_TIMEOUT", | ||
"NGHTTP2_STREAM_CLOSED", | ||
"NGHTTP2_FRAME_SIZE_ERROR", | ||
"NGHTTP2_REFUSED_STREAM", | ||
"NGHTTP2_CANCEL", | ||
"NGHTTP2_COMPRESSION_ERROR", | ||
"NGHTTP2_CONNECT_ERROR", | ||
"NGHTTP2_ENHANCE_YOUR_CALM", | ||
"NGHTTP2_INADEQUATE_SECURITY", | ||
"NGHTTP2_HTTP_1_1_REQUIRED" | ||
] | ||
|
||
for(let i = 0; i < errorCodes.length; i++){ | ||
addTest(i); | ||
} | ||
|
||
tape('cleanup', function(t){ | ||
s.destroy(function(){ | ||
t.end() | ||
}) | ||
}) |