Branch data Line data Source code
1 [ + ]: 1 : import assert from 'node:assert/strict';
2 : 1 : import { FormData } from '../../../../lib/whatwg-xhr.js';
3 : 1 :
4 [ + ]: 1 : export default (activeURL) => {
5 : 1 :
6 : 1 : const formData = new FormData();
7 : 1 :
8 [ + ]: 1 : (() => {
9 : 1 : const value = new Blob();
10 : 1 : formData.set("blob-1", value);
11 : 1 :
12 : 1 : const blob1 = formData.get("blob-1");
13 : 1 :
14 : 1 : assert.notStrictEqual(blob1, value);
15 : 1 :
16 : 1 : assert.strictEqual(blob1.constructor.name, "File");
17 : 1 : assert.strictEqual(blob1.name, "blob");
18 : 1 : assert.strictEqual(blob1.type, "");
19 : 1 : assert.strictEqual(formData.get("blob-1") === formData.get("blob-1"), true, "should return the same value when get the same blob entry from FormData");
20 : 1 :
21 : 1 : assert(Math.abs(blob1.lastModified - Date.now()) < 200, "lastModified should be now");
22 : 1 : })( "blob without type");
23 : 1 :
24 [ + ]: 1 : (() => {
25 : 1 : const value = new Blob([], { type: "text/plain" });
26 : 1 : formData.set("blob-2", value);
27 : 1 :
28 : 1 : const blob2 = formData.get("blob-2");
29 : 1 :
30 : 1 : assert.notStrictEqual(blob2, value);
31 : 1 :
32 : 1 : assert.strictEqual(blob2.constructor.name, "File");
33 : 1 : assert.strictEqual(blob2.name, "blob");
34 : 1 : assert.strictEqual(blob2.type, "text/plain");
35 : 1 :
36 : 1 : assert(Math.abs(blob2.lastModified - Date.now()) < 200, "lastModified should be now");
37 : 1 : })( "blob with type");
38 : 1 :
39 [ + ]: 1 : (() => {
40 : 1 : const value = new Blob();
41 : 1 : formData.set("blob-3", value, "custom name");
42 : 1 :
43 : 1 : const blob3 = formData.get("blob-3");
44 : 1 :
45 : 1 : assert.notStrictEqual(blob3, value);
46 : 1 :
47 : 1 : assert.strictEqual(blob3.constructor.name, "File");
48 : 1 : assert.strictEqual(blob3.name, "custom name");
49 : 1 : assert.strictEqual(blob3.type, "");
50 : 1 :
51 : 1 : assert(Math.abs(blob3.lastModified - Date.now()) < 200, "lastModified should be now");
52 : 1 : })( "blob with custom name");
53 : 1 :
54 [ + ]: 1 : (() => {
55 : 1 : const value = new File([], "name");
56 : 1 : formData.set("file-1", value);
57 : 1 :
58 : 1 : const file1 = formData.get("file-1");
59 : 1 :
60 : 1 : assert.strictEqual(file1, value);
61 : 1 : assert.strictEqual(file1.constructor.name, "File");
62 : 1 : assert.strictEqual(file1.name, "name");
63 : 1 : assert.strictEqual(file1.type, "");
64 : 1 :
65 : 1 : assert(Math.abs(file1.lastModified - Date.now()) < 200, "lastModified should be now");
66 : 1 : })( "file without lastModified or custom name");
67 : 1 :
68 [ + ]: 1 : (() => {
69 : 1 : const value = new File([], "name", { lastModified: 123 });
70 : 1 : formData.set("file-2", value, "custom name");
71 : 1 :
72 : 1 : const file2 = formData.get("file-2");
73 : 1 :
74 : 1 : assert.notStrictEqual(file2, value);
75 : 1 :
76 : 1 : assert.strictEqual(file2.constructor.name, "File");
77 : 1 : assert.strictEqual(file2.name, "custom name");
78 : 1 : assert.strictEqual(file2.type, "");
79 : 1 : assert.strictEqual(file2.lastModified, 123, "lastModified should be 123");
80 : 1 : })( "file with lastModified and custom name");
81 : 1 :
82 : 1 : }
83 : 1 :
84 : 1 : /*
85 : 1 : * set-blob.any.js
86 : 1 : *
87 : 1 :
88 : 1 : // META: title=formData.set(blob) and formData.set(file)
89 : 1 :
90 : 1 : "use strict";
91 : 1 :
92 : 1 : const formData = new FormData();
93 : 1 :
94 : 1 : test(() => {
95 : 1 : const value = new Blob();
96 : 1 : formData.set("blob-1", value);
97 : 1 : const blob1 = formData.get("blob-1");
98 : 1 : assert_not_equals(blob1, value);
99 : 1 : assert_equals(blob1.constructor.name, "File");
100 : 1 : assert_equals(blob1.name, "blob");
101 : 1 : assert_equals(blob1.type, "");
102 : 1 : assert_equals(formData.get("blob-1") === formData.get("blob-1"), true, "should return the same value when get the same blob entry from FormData");
103 : 1 : assert_less_than(Math.abs(blob1.lastModified - Date.now()), 200, "lastModified should be now");
104 : 1 : }, "blob without type");
105 : 1 :
106 : 1 : test(() => {
107 : 1 : const value = new Blob([], { type: "text/plain" });
108 : 1 : formData.set("blob-2", value);
109 : 1 : const blob2 = formData.get("blob-2");
110 : 1 : assert_not_equals(blob2, value);
111 : 1 : assert_equals(blob2.constructor.name, "File");
112 : 1 : assert_equals(blob2.name, "blob");
113 : 1 : assert_equals(blob2.type, "text/plain");
114 : 1 : assert_less_than(Math.abs(blob2.lastModified - Date.now()), 200, "lastModified should be now");
115 : 1 : }, "blob with type");
116 : 1 :
117 : 1 : test(() => {
118 : 1 : const value = new Blob();
119 : 1 : formData.set("blob-3", value, "custom name");
120 : 1 : const blob3 = formData.get("blob-3");
121 : 1 : assert_not_equals(blob3, value);
122 : 1 : assert_equals(blob3.constructor.name, "File");
123 : 1 : assert_equals(blob3.name, "custom name");
124 : 1 : assert_equals(blob3.type, "");
125 : 1 : assert_less_than(Math.abs(blob3.lastModified - Date.now()), 200, "lastModified should be now");
126 : 1 : }, "blob with custom name");
127 : 1 :
128 : 1 : test(() => {
129 : 1 : const value = new File([], "name");
130 : 1 : formData.set("file-1", value);
131 : 1 : const file1 = formData.get("file-1");
132 : 1 : assert_equals(file1, value);
133 : 1 : assert_equals(file1.constructor.name, "File");
134 : 1 : assert_equals(file1.name, "name");
135 : 1 : assert_equals(file1.type, "");
136 : 1 : assert_less_than(Math.abs(file1.lastModified - Date.now()), 200, "lastModified should be now");
137 : 1 : }, "file without lastModified or custom name");
138 : 1 :
139 : 1 : test(() => {
140 : 1 : const value = new File([], "name", { lastModified: 123 });
141 : 1 : formData.set("file-2", value, "custom name");
142 : 1 : const file2 = formData.get("file-2");
143 : 1 : assert_not_equals(file2, value);
144 : 1 : assert_equals(file2.constructor.name, "File");
145 : 1 : assert_equals(file2.name, "custom name");
146 : 1 : assert_equals(file2.type, "");
147 : 1 : assert_equals(file2.lastModified, 123, "lastModified should be 123");
148 : 1 : }, "file with lastModified and custom name");
149 : 1 :
150 : 1 : *
151 : 1 : *
152 : 1 : */
|