Skip to content

Commit ea6c877

Browse files
committed
add more tests
1 parent b6152f4 commit ea6c877

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

test/Component.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {expect} from "chai";
22
import {Multipart, Component} from "../dist/index.js";
3+
import {describe} from "mocha";
34

45
describe("Component", () => {
56

test/Multipart.test.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Multipart, Component} from "../dist/index.js";
2-
import { expect } from "chai";
1+
import {Multipart, Component} from "../dist/index.js";
2+
import {expect} from "chai";
3+
import {describe} from "mocha";
34

45
describe("Multipart", function () {
56
describe("constructor", function () {
67
it("should initialize with default boundary and mediaType", function () {
7-
const component = new Component({ "content-type": "text/plain" }, new TextEncoder().encode("foo bar"));
8+
const component = new Component({"content-type": "text/plain"}, new TextEncoder().encode("foo bar"));
89
const multipart = new Multipart([component]);
910

1011
expect(multipart.boundary).to.be.an.instanceof(Uint8Array);
@@ -14,7 +15,7 @@ describe("Multipart", function () {
1415
it("should accept a custom boundary and mediaType", function () {
1516
const boundary = "my-custom-boundary";
1617
const mediaType = "Multipart/form-data";
17-
const component = new Component({ "x-foo": "bar" }, new TextEncoder().encode("custom content"));
18+
const component = new Component({"x-foo": "bar"}, new TextEncoder().encode("custom content"));
1819
const multipart = new Multipart([component], boundary, mediaType);
1920

2021
expect(new TextDecoder().decode(multipart.boundary)).to.equal(boundary);
@@ -32,8 +33,8 @@ describe("Multipart", function () {
3233
describe("parse", function () {
3334
it("should parse Multipart data correctly", function () {
3435
const boundary = "my-boundary";
35-
const component1 = new Component({ "x-foo": "bar" }, new TextEncoder().encode("Component1 content"));
36-
const component2 = new Component({ "content-type": "text/plain" }, new TextEncoder().encode("Component2 content"));
36+
const component1 = new Component({"x-foo": "bar"}, new TextEncoder().encode("Component1 content"));
37+
const component2 = new Component({"content-type": "text/plain"}, new TextEncoder().encode("Component2 content"));
3738
const multipart = new Multipart([component1, component2], boundary);
3839

3940
const multipartBytes = multipart.bytes();
@@ -90,10 +91,10 @@ describe("Multipart", function () {
9091

9192
it("should handle nested multiparts", function () {
9293
const components = [
93-
new Component({ "x-foo": "bar" }, new TextEncoder().encode("foo bar")),
94+
new Component({"x-foo": "bar"}, new TextEncoder().encode("foo bar")),
9495
new Multipart([
95-
new Component({ "content-type": "text/plain" }, new TextEncoder().encode("nested Component 1")),
96-
new Component({ "content-type": "application/json" }, new TextEncoder().encode(JSON.stringify({ foo: "bar" })))
96+
new Component({"content-type": "text/plain"}, new TextEncoder().encode("nested Component 1")),
97+
new Component({"content-type": "application/json"}, new TextEncoder().encode(JSON.stringify({foo: "bar"})))
9798
], "inner-boundary")
9899
];
99100
const multipart = new Multipart(components, "outer-boundary");
@@ -112,7 +113,7 @@ describe("Multipart", function () {
112113
expect(parsedInnerMultipart.parts[0].headers.get("content-type")).to.equal("text/plain");
113114
expect(new TextDecoder().decode(parsedInnerMultipart.parts[0].body)).to.equal("nested Component 1");
114115
expect(parsedInnerMultipart.parts[1].headers.get("content-type")).to.equal("application/json");
115-
expect(new TextDecoder().decode(parsedInnerMultipart.parts[1].body)).to.equal(JSON.stringify({ foo: "bar" }));
116+
expect(new TextDecoder().decode(parsedInnerMultipart.parts[1].body)).to.equal(JSON.stringify({foo: "bar"}));
116117
});
117118

118119
it("should handle malformed Multipart data", function () {
@@ -217,6 +218,25 @@ describe("Multipart", function () {
217218
});
218219
});
219220

221+
describe("part", function () {
222+
it("should create Multipart from Part", function () {
223+
const multipart = new Multipart([
224+
new Component({"content-type": "text/plain", "x-foo": "bar"}, new TextEncoder().encode("foo bar")),
225+
new Component({}, new TextEncoder().encode("test content"))
226+
]);
227+
const part = new Component({"Content-Type": multipart.headers.get("content-type")}, multipart.bytes());
228+
229+
const parsedMultipart = Multipart.part(part);
230+
expect(parsedMultipart).to.be.an.instanceof(Multipart);
231+
expect(parsedMultipart.parts.length).to.equal(2);
232+
expect(parsedMultipart.parts[0].headers.get("content-type")).to.equal("text/plain");
233+
expect(parsedMultipart.parts[0].headers.get("x-foo")).to.equal("bar");
234+
expect(new TextDecoder().decode(parsedMultipart.parts[0].body)).to.equal("foo bar");
235+
expect(parsedMultipart.parts[1].headers.get("content-type")).to.equal(null);
236+
expect(new TextDecoder().decode(parsedMultipart.parts[1].body)).to.equal("test content");
237+
});
238+
});
239+
220240
describe("formData", function () {
221241
it("should correctly create Multipart from FormData", async function () {
222242
const formData = new FormData();
@@ -259,7 +279,7 @@ describe("Multipart", function () {
259279
});
260280

261281
describe("#formData", function () {
262-
it ("should correctly return the FormData of the Multipart", async function () {
282+
it("should correctly return the FormData of the Multipart", async function () {
263283
const formData = new FormData();
264284
formData.append("foo", "bar");
265285
formData.append("bar", "baz");
@@ -278,7 +298,7 @@ describe("Multipart", function () {
278298
expect(new TextDecoder().decode(await file.arrayBuffer())).to.equal("console.log('hello world');");
279299
});
280300

281-
it("should handle empty FormData multipart", async function (){
301+
it("should handle empty FormData multipart", async function () {
282302
const multipart = await Multipart.formData(new FormData());
283303
const formData = multipart.formData();
284304
expect(formData).to.be.an.instanceof(FormData);
@@ -289,7 +309,7 @@ describe("Multipart", function () {
289309
describe("#body", function () {
290310
it("should correctly return the body of the Multipart", function () {
291311
const boundary = "test-boundary";
292-
const component = new Component({ "content-type": "text/plain" }, new TextEncoder().encode("test body"));
312+
const component = new Component({"content-type": "text/plain"}, new TextEncoder().encode("test body"));
293313
const multipart = new Multipart([component], boundary);
294314

295315
const body = multipart.body;
@@ -327,7 +347,7 @@ describe("Multipart", function () {
327347
describe("#bytes", function () {
328348
it("should correctly return the bytes of the Multipart", function () {
329349
const boundary = "test-boundary";
330-
const component = new Component({ "x-foo": "bar" }, new TextEncoder().encode("test content"));
350+
const component = new Component({"x-foo": "bar"}, new TextEncoder().encode("test content"));
331351
const multipart = new Multipart([component], boundary);
332352

333353
const bytes = multipart.bytes();

0 commit comments

Comments
 (0)