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

Fix http2 fetch test #2253

Merged
merged 7 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions test/fetch/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { createSecureServer } = require('node:http2')
const { createReadStream, readFileSync } = require('node:fs')
const { once } = require('node:events')
const { Blob } = require('node:buffer')
const { Readable } = require('node:stream')

const { test, plan } = require('tap')
const pem = require('https-pem')
Expand Down Expand Up @@ -65,7 +66,8 @@ test('[Fetch] Should handle h2 request with body (string or buffer)', async t =>
t.equal(responseBody, expectedRequestBody)
})

test('[Fetch] Should handle h2 request with body (stream)', 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 => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@metcoder95 @KhafraDev I'm skipping this now, but this needs some investigation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it is kinda of flaky. I couldn't reproduce it all the time locally. I'll focus on that.

I can try to move it to use an AsyncIterator instead as I believe the way I set the stream to handle the data is the culprit. I'll take a look at it

const server = createSecureServer(pem)
const expectedBody = readFileSync(__filename, 'utf-8')
const stream = createReadStream(__filename)
Expand Down Expand Up @@ -112,7 +114,7 @@ test('[Fetch] Should handle h2 request with body (stream)', async t => {
'x-my-header': 'foo',
'content-type': 'text-plain'
},
body: stream,
body: Readable.toWeb(stream),
duplex: 'half'
}
)
Expand Down
42 changes: 21 additions & 21 deletions test/redirect-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ for (const factory of [
(server, opts) => new undici.Pool(`http://${server}`, opts),
(server, opts) => new undici.Client(`http://${server}`, opts)
]) {
const request = (server, opts, ...args) => {
const request = (t, server, opts, ...args) => {
const dispatcher = factory(server, opts)
t.teardown(() => dispatcher.close())
return undici.request(args[0], { ...args[1], dispatcher }, args[2])
.finally(() => dispatcher.destroy())
}

t.test('should always have a history with the final URL even if no redirections were followed', async t => {
const server = await startRedirectingServer(t)

const { statusCode, headers, body: bodyStream, context: { history } } = await request(server, undefined, `http://${server}/200?key=value`, {
const { statusCode, headers, body: bodyStream, context: { history } } = await request(t, server, undefined, `http://${server}/200?key=value`, {
maxRedirections: 10
})

Expand All @@ -43,7 +43,7 @@ for (const factory of [
t.test('should not follow redirection by default if not using RedirectAgent', async t => {
const server = await startRedirectingServer(t)

const { statusCode, headers, body: bodyStream } = await request(server, undefined, `http://${server}`)
const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}`)
const body = await bodyStream.text()

t.equal(statusCode, 302)
Expand All @@ -54,7 +54,7 @@ for (const factory of [
t.test('should follow redirection after a HTTP 300', async t => {
const server = await startRedirectingServer(t)

const { statusCode, headers, body: bodyStream, context: { history } } = await request(server, undefined, `http://${server}/300?key=value`, {
const { statusCode, headers, body: bodyStream, context: { history } } = await request(t, server, undefined, `http://${server}/300?key=value`, {
maxRedirections: 10
})

Expand All @@ -76,7 +76,7 @@ for (const factory of [
t.test('should follow redirection after a HTTP 300 default', async t => {
const server = await startRedirectingServer(t)

const { statusCode, headers, body: bodyStream, context: { history } } = await request(server, { maxRedirections: 10 }, `http://${server}/300?key=value`)
const { statusCode, headers, body: bodyStream, context: { history } } = await request(t, server, { maxRedirections: 10 }, `http://${server}/300?key=value`)
const body = await bodyStream.text()

t.equal(statusCode, 200)
Expand All @@ -95,7 +95,7 @@ for (const factory of [
t.test('should follow redirection after a HTTP 301', async t => {
const server = await startRedirectingServer(t)

const { statusCode, headers, body: bodyStream } = await request(server, undefined, `http://${server}/301`, {
const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/301`, {
method: 'POST',
body: 'REQUEST',
maxRedirections: 10
Expand All @@ -111,7 +111,7 @@ for (const factory of [
t.test('should follow redirection after a HTTP 302', async t => {
const server = await startRedirectingServer(t)

const { statusCode, headers, body: bodyStream } = await request(server, undefined, `http://${server}/302`, {
const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/302`, {
method: 'PUT',
body: Buffer.from('REQUEST'),
maxRedirections: 10
Expand All @@ -127,7 +127,7 @@ for (const factory of [
t.test('should follow redirection after a HTTP 303 changing method to GET', async t => {
const server = await startRedirectingServer(t)

const { statusCode, headers, body: bodyStream } = await request(server, undefined, `http://${server}/303`, {
const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/303`, {
method: 'PATCH',
body: 'REQUEST',
maxRedirections: 10
Expand All @@ -143,7 +143,7 @@ for (const factory of [
t.test('should remove Host and request body related headers when following HTTP 303 (array)', async t => {
const server = await startRedirectingServer(t)

const { statusCode, headers, body: bodyStream } = await request(server, undefined, `http://${server}/303`, {
const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/303`, {
method: 'PATCH',
headers: [
'Content-Encoding',
Expand Down Expand Up @@ -174,7 +174,7 @@ for (const factory of [
t.test('should remove Host and request body related headers when following HTTP 303 (object)', async t => {
const server = await startRedirectingServer(t)

const { statusCode, headers, body: bodyStream } = await request(server, undefined, `http://${server}/303`, {
const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/303`, {
method: 'PATCH',
headers: {
'Content-Encoding': 'gzip',
Expand All @@ -198,7 +198,7 @@ for (const factory of [
t.test('should follow redirection after a HTTP 307', async t => {
const server = await startRedirectingServer(t)

const { statusCode, headers, body: bodyStream } = await request(server, undefined, `http://${server}/307`, {
const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/307`, {
method: 'DELETE',
maxRedirections: 10
})
Expand All @@ -213,7 +213,7 @@ for (const factory of [
t.test('should follow redirection after a HTTP 308', async t => {
const server = await startRedirectingServer(t)

const { statusCode, headers, body: bodyStream } = await request(server, undefined, `http://${server}/308`, {
const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/308`, {
method: 'OPTIONS',
maxRedirections: 10
})
Expand All @@ -228,7 +228,7 @@ for (const factory of [
t.test('should ignore HTTP 3xx response bodies', async t => {
const server = await startRedirectingWithBodyServer(t)

const { statusCode, headers, body: bodyStream, context: { history } } = await request(server, undefined, `http://${server}/`, {
const { statusCode, headers, body: bodyStream, context: { history } } = await request(t, server, undefined, `http://${server}/`, {
maxRedirections: 10
})

Expand All @@ -243,7 +243,7 @@ for (const factory of [
t.test('should ignore query after redirection', async t => {
const server = await startRedirectingWithQueryParams(t)

const { statusCode, headers, context: { history } } = await request(server, undefined, `http://${server}/`, {
const { statusCode, headers, context: { history } } = await request(t, server, undefined, `http://${server}/`, {
maxRedirections: 10,
query: { param1: 'first' }
})
Expand All @@ -256,7 +256,7 @@ for (const factory of [
t.test('should follow a redirect chain up to the allowed number of times', async t => {
const server = await startRedirectingServer(t)

const { statusCode, headers, body: bodyStream, context: { history } } = await request(server, undefined, `http://${server}/300`, {
const { statusCode, headers, body: bodyStream, context: { history } } = await request(t, server, undefined, `http://${server}/300`, {
maxRedirections: 2
})

Expand All @@ -273,7 +273,7 @@ for (const factory of [
const server = await startRedirectingWithoutLocationServer(t)

for (const code of redirectCodes) {
const { statusCode, headers, body: bodyStream } = await request(server, undefined, `http://${server}/${code}`, {
const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/${code}`, {
maxRedirections: 10
})

Expand All @@ -287,7 +287,7 @@ for (const factory of [

t.test('should not allow invalid maxRedirections arguments', async t => {
try {
await request('localhost', undefined, 'http://localhost', {
await request(t, 'localhost', undefined, 'http://localhost', {
method: 'GET',
maxRedirections: 'INVALID'
})
Expand All @@ -300,7 +300,7 @@ for (const factory of [

t.test('should not allow invalid maxRedirections arguments default', async t => {
try {
await request('localhost', {
await request(t, 'localhost', {
maxRedirections: 'INVALID'
}, 'http://localhost', {
method: 'GET'
Expand All @@ -315,7 +315,7 @@ for (const factory of [
t.test('should not follow redirects when using ReadableStream request bodies', { skip: nodeMajor < 16 }, async t => {
const server = await startRedirectingServer(t)

const { statusCode, headers, body: bodyStream } = await request(server, undefined, `http://${server}/301`, {
const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/301`, {
method: 'POST',
body: createReadableStream('REQUEST'),
maxRedirections: 10
Expand All @@ -331,7 +331,7 @@ for (const factory of [
t.test('should not follow redirects when using Readable request bodies', async t => {
const server = await startRedirectingServer(t)

const { statusCode, headers, body: bodyStream } = await request(server, undefined, `http://${server}/301`, {
const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/301`, {
method: 'POST',
body: createReadable('REQUEST'),
maxRedirections: 10
Expand Down
4 changes: 3 additions & 1 deletion test/utils/redirecting-servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

const { createServer } = require('http')

const isNode20 = process.version.startsWith('v20.')

function close (server) {
return function () {
return new Promise(resolve => {
if (typeof server.closeAllConnections === 'function') {
if (isNode20) {
server.closeAllConnections()
}
server.close(resolve)
Expand Down
Loading