Skip to content

Commit

Permalink
test: split tests between v20 and lower
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Aug 30, 2023
1 parent e44b078 commit 75f6a2c
Showing 1 changed file with 84 additions and 31 deletions.
115 changes: 84 additions & 31 deletions test/http2.js
Expand Up @@ -11,7 +11,9 @@ const pem = require('https-pem')

const { Client } = require('..')

plan(17)
const isGreaterThanv20 = Number(process.version.slice(1).split('.')[0]) >= 20

plan(18)

test('Should support H2 connection', async t => {
const body = []
Expand Down Expand Up @@ -210,41 +212,92 @@ test('Should throw if bad maxConcurrentStreams has been pased', async t => {
}
})

test('Request should fail if allowH2 is false and server advertises h2 only', async t => {
const server = createSecureServer(
{
...pem,
allowHTTP1: false,
ALPNProtocols: ['http/1.1']
},
(req, res) => {
t.fail('Should not create a valid h2 stream')
}
)
test(
'Request should fail if allowH2 is false and server advertises h1 only',
{ skip: isGreaterThanv20 },
async t => {
const server = createSecureServer(
{
...pem,
allowHTTP1: false,
ALPNProtocols: ['http/1.1']
},
(req, res) => {
t.fail('Should not create a valid h2 stream')
}
)

server.listen(0)
await once(server, 'listening')
server.listen(0)
await once(server, 'listening')

const client = new Client(`https://localhost:${server.address().port}`, {
allowH2: false,
connect: {
rejectUnauthorized: false
}
})
const client = new Client(`https://localhost:${server.address().port}`, {
allowH2: false,
connect: {
rejectUnauthorized: false
}
})

t.teardown(server.close.bind(server))
t.teardown(client.close.bind(client))
t.teardown(server.close.bind(server))
t.teardown(client.close.bind(client))

const response = await client.request({
path: '/',
method: 'GET',
headers: {
'x-my-header': 'foo'
}
})
const response = await client.request({
path: '/',
method: 'GET',
headers: {
'x-my-header': 'foo'
}
})

t.equal(response.statusCode, 403)
})
t.equal(response.statusCode, 403)
}
)

test(
'[v20] Request should fail if allowH2 is false and server advertises h1 only',
{ skip: !isGreaterThanv20 },
async t => {
const server = createSecureServer(
{
...pem,
allowHTTP1: false,
ALPNProtocols: ['http/1.1']
},
(req, res) => {
t.fail('Should not create a valid h2 stream')
}
)

server.listen(0)
await once(server, 'listening')

const client = new Client(`https://localhost:${server.address().port}`, {
allowH2: false,
connect: {
rejectUnauthorized: false
}
})

t.teardown(server.close.bind(server))
t.teardown(client.close.bind(client))
t.plan(2)

try {
await client.request({
path: '/',
method: 'GET',
headers: {
'x-my-header': 'foo'
}
})
} catch (error) {
t.equal(
error.message,
'Client network socket disconnected before secure TLS connection was established'
)
t.equal(error.code, 'ECONNRESET')
}
}
)

test('Should handle h2 continue', async t => {
const requestBody = []
Expand Down

0 comments on commit 75f6a2c

Please sign in to comment.