Skip to content

Commit

Permalink
refactor: set allowh2 to false
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Aug 16, 2023
1 parent a589568 commit 2c64140
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 70 deletions.
2 changes: 1 addition & 1 deletion lib/core/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, ...o
const options = { path: socketPath, ...opts }
const sessionCache = new SessionCache(maxCachedSessions == null ? 100 : maxCachedSessions)
timeout = timeout == null ? 10e3 : timeout
allowH2 = allowH2 != null ? allowH2 : true
allowH2 = allowH2 != null ? allowH2 : false
return function connect ({ hostname, host, protocol, port, servername, localAddress, httpSocket }, callback) {
let socket
if (protocol === 'https:') {
Expand Down
152 changes: 83 additions & 69 deletions test/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ test('Should support H2 connection', async t => {
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.plan(6)
Expand Down Expand Up @@ -82,7 +83,8 @@ test('Should support H2 connection (headers as array)', async t => {
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.plan(7)
Expand Down Expand Up @@ -133,7 +135,8 @@ test('Should support H2 GOAWAY (server-side)', async t => {
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.plan(9)
Expand Down Expand Up @@ -242,7 +245,8 @@ test('Should handle h2 continue', async t => {
connect: {
rejectUnauthorized: false
},
expectContinue: true
expectContinue: true,
allowH2: true
})

t.teardown(server.close.bind(server))
Expand Down Expand Up @@ -291,7 +295,8 @@ test('Dispatcher#Stream', t => {
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.teardown(server.close.bind(server))
Expand Down Expand Up @@ -339,7 +344,8 @@ test('Dispatcher#Pipeline', t => {
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.teardown(server.close.bind(server))
Expand Down Expand Up @@ -397,7 +403,8 @@ test('Dispatcher#Connect', t => {
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.teardown(server.close.bind(server))
Expand Down Expand Up @@ -443,7 +450,8 @@ test('Dispatcher#Upgrade', t => {
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.teardown(server.close.bind(server))
Expand Down Expand Up @@ -471,7 +479,8 @@ test('Dispatcher#destroy', async t => {
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.plan(4)
Expand Down Expand Up @@ -551,7 +560,8 @@ test('Should handle h2 request with body (string or buffer) - dispatch', t => {
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.teardown(server.close.bind(server))
Expand Down Expand Up @@ -628,7 +638,8 @@ test('Should handle h2 request with body (stream)', async t => {
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.teardown(server.close.bind(server))
Expand Down Expand Up @@ -696,7 +707,8 @@ test('Should handle h2 request with body (iterable)', async t => {
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.teardown(server.close.bind(server))
Expand Down Expand Up @@ -757,7 +769,8 @@ test('Should handle h2 request with body (Blob)', { skip: !Blob }, async t => {
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.teardown(server.close.bind(server))
Expand Down Expand Up @@ -822,7 +835,8 @@ test(
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.teardown(server.close.bind(server))
Expand Down Expand Up @@ -877,7 +891,8 @@ test('[Fetch] Should handle h2 request with body (string or buffer)', async t =>
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

const response = await fetch(
Expand Down Expand Up @@ -933,7 +948,8 @@ test('[Fetch] Should handle h2 request with body (stream)', async t => {
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.teardown(server.close.bind(server))
Expand Down Expand Up @@ -963,70 +979,67 @@ test('[Fetch] Should handle h2 request with body (stream)', async t => {
t.equal(Buffer.concat(requestChunks).toString('utf-8'), expectedBody)
})

test(
'Should handle h2 request with body (Blob)',
{ skip: !Blob },
async t => {
const server = createSecureServer(pem)
const expectedBody = 'asd'
const requestChunks = []
const body = new Blob(['asd'], {
type: 'text/plain'
})

server.on('stream', async (stream, headers) => {
t.equal(headers[':method'], 'POST')
t.equal(headers[':path'], '/')
t.equal(headers[':scheme'], 'https')
test('Should handle h2 request with body (Blob)', { skip: !Blob }, async t => {
const server = createSecureServer(pem)
const expectedBody = 'asd'
const requestChunks = []
const body = new Blob(['asd'], {
type: 'text/plain'
})

stream.on('data', chunk => requestChunks.push(chunk))
server.on('stream', async (stream, headers) => {
t.equal(headers[':method'], 'POST')
t.equal(headers[':path'], '/')
t.equal(headers[':scheme'], 'https')

stream.respond({
'content-type': 'text/plain; charset=utf-8',
'x-custom-h2': headers['x-my-header'],
':status': 200
})
stream.on('data', chunk => requestChunks.push(chunk))

stream.end('hello h2!')
stream.respond({
'content-type': 'text/plain; charset=utf-8',
'x-custom-h2': headers['x-my-header'],
':status': 200
})

t.plan(8)
stream.end('hello h2!')
})

server.listen(0)
await once(server, 'listening')
t.plan(8)

const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
})
server.listen(0)
await once(server, 'listening')

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

const response = await fetch(
`https://localhost:${server.address().port}/`,
// Needs to be passed to disable the reject unauthorized
{
body,
method: 'POST',
dispatcher: client,
headers: {
'x-my-header': 'foo',
'content-type': 'text-plain'
}
t.teardown(server.close.bind(server))
t.teardown(client.close.bind(client))

const response = await fetch(
`https://localhost:${server.address().port}/`,
// Needs to be passed to disable the reject unauthorized
{
body,
method: 'POST',
dispatcher: client,
headers: {
'x-my-header': 'foo',
'content-type': 'text-plain'
}
)
}
)

const responseBody = await response.arrayBuffer()
const responseBody = await response.arrayBuffer()

t.equal(response.status, 200)
t.equal(response.headers.get('content-type'), 'text/plain; charset=utf-8')
t.equal(response.headers.get('x-custom-h2'), 'foo')
t.same(new TextDecoder().decode(responseBody).toString(), 'hello h2!')
t.equal(Buffer.concat(requestChunks).toString('utf-8'), expectedBody)
}
)
t.equal(response.status, 200)
t.equal(response.headers.get('content-type'), 'text/plain; charset=utf-8')
t.equal(response.headers.get('x-custom-h2'), 'foo')
t.same(new TextDecoder().decode(responseBody).toString(), 'hello h2!')
t.equal(Buffer.concat(requestChunks).toString('utf-8'), expectedBody)
})

test(
'Should handle h2 request with body (Blob:ArrayBuffer)',
Expand Down Expand Up @@ -1065,7 +1078,8 @@ test(
const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
}
},
allowH2: true
})

t.teardown(server.close.bind(server))
Expand Down

0 comments on commit 2c64140

Please sign in to comment.