Skip to content

Commit

Permalink
Added tests for http-2 specific error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
parthverma1 committed Aug 14, 2024
1 parent 632f6bf commit 5595da0
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tests/test-errors-http2-auto-specific.js
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()
})
})
73 changes: 73 additions & 0 deletions tests/test-errors-http2-specific.js
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()
})
})

0 comments on commit 5595da0

Please sign in to comment.