Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix Fetch/HTTP2 tests #2263

Merged
merged 4 commits into from
Sep 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions test/fetch/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,59 @@ const pem = require('https-pem')

const { Client, fetch } = require('../..')

plan(4)
const nodeVersion = Number(process.version.split('v')[1].split('.')[0])

plan(5)

test('[Fetch] Simple GET with h2', async t => {
const server = createSecureServer(pem)
const expectedRequestBody = 'hello h2!'

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

stream.end(expectedRequestBody)
})

t.plan(3)

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

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
{
method: 'GET',
dispatcher: client,
headers: {
'x-my-header': 'foo',
'content-type': 'text-plain'
}
}
)

const responseBody = await response.text()

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

t.equal(responseBody, expectedRequestBody)
t.equal(response.headers.get('x-method'), 'GET')
t.equal(response.headers.get('x-custom-h2'), 'foo')
})

test('[Fetch] Should handle h2 request with body (string or buffer)', async t => {
const server = createSecureServer(pem)
Expand Down Expand Up @@ -67,7 +119,7 @@ test('[Fetch] Should handle h2 request with body (string or buffer)', async t =>
})

// Skipping for now, there is something odd in the way the body is handled
test('[Fetch] Should handle h2 request with body (stream)', { skip: true }, async t => {
test('[Fetch] Should handle h2 request with body (stream)', { skip: nodeVersion === 16 }, async t => {
const server = createSecureServer(pem)
const expectedBody = readFileSync(__filename, 'utf-8')
const stream = createReadStream(__filename)
Expand All @@ -78,15 +130,17 @@ test('[Fetch] Should handle h2 request with body (stream)', { skip: true }, asyn
t.equal(headers[':path'], '/')
t.equal(headers[':scheme'], 'https')

stream.on('data', chunk => requestChunks.push(chunk))

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

stream.end('hello h2!')

for await (const chunk of stream) {
requestChunks.push(chunk)
}
})

t.plan(8)
Expand Down
Loading