forked from nodejs/undici
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: move fetch tests to fetch folder
- Loading branch information
1 parent
37c73ba
commit 224c26e
Showing
2 changed files
with
258 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
'use strict' | ||
|
||
const { createSecureServer } = require('node:http2') | ||
const { createReadStream, readFileSync } = require('node:fs') | ||
const { once } = require('node:events') | ||
const { Blob } = require('node:buffer') | ||
|
||
const { test, plan } = require('tap') | ||
const pem = require('https-pem') | ||
|
||
const { Client, fetch } = require('../..') | ||
|
||
plan(4) | ||
|
||
test('[Fetch] Should handle h2 request with body (string or buffer)', async t => { | ||
const server = createSecureServer(pem) | ||
const expectedBody = 'hello from client!' | ||
const expectedRequestBody = 'hello h2!' | ||
const requestBody = [] | ||
|
||
server.on('stream', async (stream, headers) => { | ||
stream.on('data', chunk => requestBody.push(chunk)) | ||
|
||
stream.respond({ | ||
'content-type': 'text/plain; charset=utf-8', | ||
'x-custom-h2': headers['x-my-header'], | ||
':status': 200 | ||
}) | ||
|
||
stream.end(expectedRequestBody) | ||
}) | ||
|
||
t.plan(2) | ||
|
||
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: 'POST', | ||
dispatcher: client, | ||
headers: { | ||
'x-my-header': 'foo', | ||
'content-type': 'text-plain' | ||
}, | ||
body: expectedBody | ||
} | ||
) | ||
|
||
const responseBody = await response.text() | ||
|
||
t.teardown(server.close.bind(server)) | ||
t.teardown(client.close.bind(client)) | ||
|
||
t.equal(Buffer.concat(requestBody).toString('utf-8'), expectedBody) | ||
t.equal(responseBody, expectedRequestBody) | ||
}) | ||
|
||
test('[Fetch] Should handle h2 request with body (stream)', async t => { | ||
const server = createSecureServer(pem) | ||
const expectedBody = readFileSync(__filename, 'utf-8') | ||
const stream = createReadStream(__filename) | ||
const requestChunks = [] | ||
|
||
server.on('stream', async (stream, headers) => { | ||
t.equal(headers[':method'], 'PUT') | ||
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!') | ||
}) | ||
|
||
t.plan(8) | ||
|
||
server.listen(0) | ||
await once(server, 'listening') | ||
|
||
const client = new Client(`https://localhost:${server.address().port}`, { | ||
connect: { | ||
rejectUnauthorized: false | ||
}, | ||
allowH2: true | ||
}) | ||
|
||
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 | ||
{ | ||
method: 'PUT', | ||
dispatcher: client, | ||
headers: { | ||
'x-my-header': 'foo', | ||
'content-type': 'text-plain' | ||
}, | ||
body: stream, | ||
duplex: 'half' | ||
} | ||
) | ||
|
||
const responseBody = await response.text() | ||
|
||
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.equal(responseBody, 'hello h2!') | ||
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') | ||
|
||
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!') | ||
}) | ||
|
||
t.plan(8) | ||
|
||
server.listen(0) | ||
await once(server, 'listening') | ||
|
||
const client = new Client(`https://localhost:${server.address().port}`, { | ||
connect: { | ||
rejectUnauthorized: false | ||
}, | ||
allowH2: true | ||
}) | ||
|
||
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() | ||
|
||
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)', | ||
{ skip: !Blob }, | ||
async t => { | ||
const server = createSecureServer(pem) | ||
const expectedBody = 'hello' | ||
const requestChunks = [] | ||
const expectedResponseBody = { hello: 'h2' } | ||
const buf = Buffer.from(expectedBody) | ||
const body = new ArrayBuffer(buf.byteLength) | ||
|
||
buf.copy(new Uint8Array(body)) | ||
|
||
server.on('stream', async (stream, headers) => { | ||
t.equal(headers[':method'], 'PUT') | ||
t.equal(headers[':path'], '/') | ||
t.equal(headers[':scheme'], 'https') | ||
|
||
stream.on('data', chunk => requestChunks.push(chunk)) | ||
|
||
stream.respond({ | ||
'content-type': 'application/json', | ||
'x-custom-h2': headers['x-my-header'], | ||
':status': 200 | ||
}) | ||
|
||
stream.end(JSON.stringify(expectedResponseBody)) | ||
}) | ||
|
||
t.plan(8) | ||
|
||
server.listen(0) | ||
await once(server, 'listening') | ||
|
||
const client = new Client(`https://localhost:${server.address().port}`, { | ||
connect: { | ||
rejectUnauthorized: false | ||
}, | ||
allowH2: true | ||
}) | ||
|
||
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: 'PUT', | ||
dispatcher: client, | ||
headers: { | ||
'x-my-header': 'foo', | ||
'content-type': 'text-plain' | ||
} | ||
} | ||
) | ||
|
||
const responseBody = await response.json() | ||
|
||
t.equal(response.status, 200) | ||
t.equal(response.headers.get('content-type'), 'application/json') | ||
t.equal(response.headers.get('x-custom-h2'), 'foo') | ||
t.same(responseBody, expectedResponseBody) | ||
t.equal(Buffer.concat(requestChunks).toString('utf-8'), expectedBody) | ||
} | ||
) |
Oops, something went wrong.