Skip to content

Commit

Permalink
Merge a44ee58 into 7ab02af
Browse files Browse the repository at this point in the history
  • Loading branch information
noahmulfinger committed Aug 9, 2018
2 parents 7ab02af + a44ee58 commit 8058168
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
10 changes: 8 additions & 2 deletions packages/arcgis-rest-request/src/utils/encode-form-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ export function encodeFormData(params: any): FormData | string {
if (useFormData) {
const formData = new FormData();
Object.keys(newParams).forEach((key: any) => {
if (key === "file" && newParams[key].name) {
// Pass on the file's name if provided to override defaults such as "blob"
if (typeof File !== "undefined" && newParams[key] instanceof File) {
// Pass on the file's name to override defaults such as "blob"
formData.append(key, newParams[key], newParams[key].name);
} else if (
typeof Blob !== "undefined" &&
newParams[key] instanceof Blob
) {
// Pass on the key as the file name to override defaults such as "blob"
formData.append(key, newParams[key], key);
} else {
formData.append(key, newParams[key]);
}
Expand Down
27 changes: 25 additions & 2 deletions packages/arcgis-rest-request/test/utils/encode-form-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,40 @@ import {
import { attachmentFile } from "../../../arcgis-rest-feature-service/test/attachments.test";

describe("encodeFormData", () => {
it("should encode in form data for multipart requests", () => {
it("should encode in form data for multipart file requests", () => {
const binaryObj =
typeof File !== "undefined"
? new File(["foo"], "foo.txt", {
type: "text/plain"
})
: new Buffer("");
: Buffer.from("");

const formData = encodeFormData({ binary: binaryObj });

expect(formData instanceof FormData).toBeTruthy();

if (formData instanceof FormData && formData.get) {
expect(formData.get("binary") instanceof File).toBeTruthy();
expect((formData.get("binary") as File).name).toBe("foo.txt");
}
});

it("should encode in form data for multipart blob requests", () => {
const binaryObj =
typeof Blob !== "undefined"
? new Blob([], {
type: "text/plain"
})
: Buffer.from("");

const formData = encodeFormData({ binary: binaryObj });

expect(formData instanceof FormData).toBeTruthy();

if (formData instanceof FormData && formData.get) {
expect(formData.get("binary") instanceof File).toBeTruthy();
expect((formData.get("binary") as File).name).toBe("binary");
}
});

it("should encode as query string for basic types", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe("processParams", () => {
? new File(["foo"], "foo.txt", {
type: "text/plain"
})
: new Buffer("");
: Buffer.from("");

expect(
requiresFormData({
Expand All @@ -178,7 +178,7 @@ describe("processParams", () => {
? new File(["foo"], "foo.txt", {
type: "text/plain"
})
: new Buffer("");
: Buffer.from("");

expect(
requiresFormData({
Expand Down

0 comments on commit 8058168

Please sign in to comment.