diff --git a/lib/client.js b/lib/client.js index 688df9e6156..f237fe02ab8 100644 --- a/lib/client.js +++ b/lib/client.js @@ -569,7 +569,10 @@ class Parser { /* istanbul ignore else: difficult to make a test case for */ if (ptr) { const len = new Uint8Array(llhttp.memory.buffer, ptr).indexOf(0) - message = Buffer.from(llhttp.memory.buffer, ptr, len).toString() + message = + 'Response does not match the HTTP/1.1 protocol (' + + Buffer.from(llhttp.memory.buffer, ptr, len).toString() + + ')' } throw new HTTPParserError(message, constants.ERROR[ret], data.slice(offset)) } diff --git a/test/http2.js b/test/http2.js new file mode 100644 index 00000000000..ab8752a7816 --- /dev/null +++ b/test/http2.js @@ -0,0 +1,32 @@ +'use strict' + +const { test } = require('tap') +const { Client, errors } = require('..') +const { createSecureServer } = require('http2') +const pem = require('https-pem') + +test('throw http2 not supported error', (t) => { + t.plan(1) + + const server = createSecureServer({ key: pem.key, cert: pem.cert }, (req, res) => { + res.stream.respond({ 'content-type': 'text/plain' }) + res.stream.end('hello') + }).on('unknownProtocol', (socket) => { + // continue sending data in http2 to our http1.1 client to trigger error + socket.write('PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n') + }) + t.teardown(server.close.bind(server)) + + server.listen(0, () => { + const client = new Client(`https://localhost:${server.address().port}`, { + tls: { + rejectUnauthorized: false + } + }) + t.teardown(client.close.bind(client)) + + client.request({ path: '/', method: 'GET' }, (err, data) => { + t.type(err, errors.HTTPParserError) + }) + }) +}) diff --git a/test/parser-issues.js b/test/parser-issues.js index 7f5bfbb5f99..b98edf159ef 100644 --- a/test/parser-issues.js +++ b/test/parser-issues.js @@ -1,6 +1,6 @@ const net = require('net') const { test } = require('tap') -const { Client } = require('..') +const { Client, errors } = require('..') test('https://github.com/mcollina/undici/issues/268', (t) => { t.plan(2) @@ -40,7 +40,7 @@ test('https://github.com/mcollina/undici/issues/268', (t) => { }) test('parser fail', (t) => { - t.plan(3) + t.plan(2) const server = net.createServer(socket => { socket.write('HTT/1.1 200 OK\r\n') @@ -56,8 +56,7 @@ test('parser fail', (t) => { path: '/' }, (err, data) => { t.ok(err) - t.equal(err.code, 'HPE_INVALID_CONSTANT') - t.equal(err.message, 'Expected HTTP/') + t.type(err, errors.HTTPParserError) }) }) })