forked from node-fetch/node-fetch
/
form-data.js
93 lines (72 loc) · 2.63 KB
/
form-data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {FormData as FormDataNode} from 'formdata-node';
import chai from 'chai';
import {Request, Response, FormData, Blob} from '../src/index.js';
const {expect} = chai;
describe('FormData', () => {
it('Consume empty URLSearchParams as FormData', async () => {
const res = new Response(new URLSearchParams());
const fd = await res.formData();
expect(fd).to.be.instanceOf(FormData);
});
it('Consume empty URLSearchParams as FormData', async () => {
const req = new Request('about:blank', {
method: 'POST',
body: new URLSearchParams()
});
const fd = await req.formData();
expect(fd).to.be.instanceOf(FormData);
});
it('Consume empty response.formData() as FormData', async () => {
const res = new Response(new FormData());
const fd = await res.formData();
expect(fd).to.be.instanceOf(FormData);
});
it('Consume empty response.formData() as FormData', async () => {
const res = new Response(new FormData());
const fd = await res.formData();
expect(fd).to.be.instanceOf(FormData);
});
it('Consume empty request.formData() as FormData', async () => {
const req = new Request('about:blank', {
method: 'POST',
body: new FormData()
});
const fd = await req.formData();
expect(fd).to.be.instanceOf(FormData);
});
it('Consume URLSearchParams with entries as FormData', async () => {
const res = new Response(new URLSearchParams({foo: 'bar'}));
const fd = await res.formData();
expect(fd.get('foo')).to.be.equal('bar');
});
it('should return a length for empty form-data', async () => {
const form = new FormData();
const ab = await new Request('http://a', {
method: 'post',
body: form
}).arrayBuffer();
expect(ab.byteLength).to.be.greaterThan(30);
});
it('should add a Blob field\'s size to the FormData length', async () => {
const form = new FormData();
const string = 'Hello, world!';
form.set('field', string);
const fd = await new Request('about:blank', {method: 'POST', body: form}).formData();
expect(fd.get('field')).to.equal(string);
});
it('should return a length for a Blob field', async () => {
const form = new FormData();
const blob = new Blob(['Hello, world!'], {type: 'text/plain'});
form.set('blob', blob);
const fd = await new Response(form).formData();
expect(fd.get('blob').size).to.equal(13);
});
it('FormData-node still works thanks to symbol.hasInstance', async () => {
const form = new FormDataNode();
form.append('file', new Blob(['abc'], {type: 'text/html'}));
const res = new Response(form);
const fd = await res.formData();
expect(await fd.get('file').text()).to.equal('abc');
expect(fd.get('file').type).to.equal('text/html');
});
});