Skip to content

Commit

Permalink
Support FormData without known length (#2120)
Browse files Browse the repository at this point in the history
  • Loading branch information
octet-stream authored Sep 2, 2022
1 parent 5c2ff68 commit 850773c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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'];
}

options.body = encoder.encode();
}
Expand Down
7 changes: 4 additions & 3 deletions test/helpers/create-https-test-server.ts
Original file line number Diff line number Diff line change
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;
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 850773c

Please sign in to comment.