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

Support FormData without known length #2120

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -51,7 +51,7 @@
"cacheable-lookup": "^6.0.4",
"cacheable-request": "^7.0.2",
"decompress-response": "^6.0.0",
"form-data-encoder": "^2.0.1",
"form-data-encoder": "^2.1.0",
"get-stream": "^6.0.1",
"http2-wrapper": "^2.1.10",
"lowercase-keys": "^3.0.0",
Expand Down
4 changes: 3 additions & 1 deletion source/core/index.ts
Expand Up @@ -574,7 +574,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {
headers['content-type'] = encoder.headers['Content-Type'];
}

headers['content-length'] = encoder.headers['Content-Length'];
if ('Content-Length' in encoder.headers) {
headers['content-length'] = encoder.headers['Content-Length'];
}
szmarczak marked this conversation as resolved.
Show resolved Hide resolved

options.body = encoder.encode();
}
Expand Down
7 changes: 4 additions & 3 deletions test/helpers/create-https-test-server.ts
Expand Up @@ -5,6 +5,7 @@ import type {SecureContextOptions} from 'tls';
import express from 'express';
import pify from 'pify';
import pem from 'pem';
import type {CreateCsr, CreateCertificate} from '../types/pem.js';

export type HttpsServerOptions = {
commonName?: string;
Expand All @@ -25,8 +26,8 @@ export type ExtendedHttpsTestServer = {
} & express.Express;

const createHttpsTestServer = async (options: HttpsServerOptions = {}): Promise<ExtendedHttpsTestServer> => {
const createCsr = pify(pem.createCSR);
const createCertificate = pify(pem.createCertificate);
const createCsr = pify(pem.createCSR as CreateCsr);
const createCertificate = pify(pem.createCertificate as CreateCertificate);

const caCsrResult = await createCsr({commonName: 'authority'});
const caResult = await createCertificate({
Expand Down Expand Up @@ -68,7 +69,7 @@ const createHttpsTestServer = async (options: HttpsServerOptions = {}): Promise<

await pify(server.https.listen.bind(server.https))();

server.caKey = caKey;
server.caKey = caKey as any;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure what the exact meaning of this code, so I just added as any in case if this assignment was without type casting to Buffer by intention. Or should I convert it to Buffer?

server.caCert = caCert;
server.port = (server.https.address() as net.AddressInfo).port;
server.url = `https://localhost:${(server.port)}`;
Expand Down
7 changes: 4 additions & 3 deletions test/https.ts
Expand Up @@ -6,10 +6,11 @@ import pify from 'pify';
import pem from 'pem';
import got from '../source/index.js';
import {withHttpsServer} from './helpers/with-server.js';
import type {CreatePrivateKey, CreateCsr, CreateCertificate} from './types/pem.js';

const createPrivateKey = pify(pem.createPrivateKey);
const createCsr = pify(pem.createCSR);
const createCertificate = pify(pem.createCertificate);
const createPrivateKey = pify(pem.createPrivateKey as CreatePrivateKey);
const createCsr = pify(pem.createCSR as CreateCsr);
const createCertificate = pify(pem.createCertificate as CreateCertificate);
const createPkcs12 = pify(pem.createPkcs12);

test('https request without ca', withHttpsServer(), async (t, server, got) => {
Expand Down
30 changes: 30 additions & 0 deletions test/post.ts
Expand Up @@ -361,6 +361,36 @@ test('body - sends files with spec-compliant FormData', withServer, async (t, se
t.deepEqual(body, expected);
});

test('body - sends form-data with without known length', withServer, async (t, server, got) => {
server.post('/', echoMultipartBody);
const fullPath = path.resolve('test/fixtures/ok');

function getFileStream() {
const fileStream = fs.createReadStream(fullPath);
const passThrough = new stream.PassThrough();
fileStream.pipe(passThrough);
return passThrough;
}

const expected = {
file: await fsPromises.readFile(fullPath, 'utf8'),
};

const form = new FormDataNode();
form.set('file', {
[Symbol.toStringTag]: 'File',
type: 'text/plain',
name: 'file.txt',
stream() {
return getFileStream();
},
});

const body = await got.post({body: form}).json<typeof expected>();

t.deepEqual(body, expected);
});

test('throws on upload error', withServer, async (t, server, got) => {
server.post('/', defaultEndpoint);

Expand Down
7 changes: 7 additions & 0 deletions test/types/pem.ts
@@ -0,0 +1,7 @@
import type {CertificateCreationOptions, CertificateCreationResult, PrivateKeyCreationOptions, CSRCreationOptions, Callback} from 'pem';

export type CreateCertificate = (options: CertificateCreationOptions, callback: Callback<CertificateCreationResult>) => void;

export type CreateCsr = (options: CSRCreationOptions, callback: Callback<{csr: string; clientKey: string}>) => void;

export type CreatePrivateKey = (keyBitsize: number, options: PrivateKeyCreationOptions, callback: Callback<{key: string}>) => void;