Skip to content

Commit 2d78112

Browse files
committed
improvement: form-data patch checks and tests
* add form-data patch checks for existing FormData prototype * update http-multipart tests for compatibility with form-data patch * linting * live tests also refactored, remain in skip state * tested on node 8
1 parent faa0338 commit 2d78112

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed

src/http.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import qs from 'qs'
33
import jsYaml from 'js-yaml'
44
import isString from 'lodash/isString'
55
import isFunction from 'lodash/isFunction'
6-
import FormData from './internal/form-data-monkey-patch';
6+
import FormData from './internal/form-data-monkey-patch'
77

88
// For testing
99
export const self = {
Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,69 @@
1-
import IsomorphicFormData from 'isomorphic-form-data';
1+
import IsomorphicFormData from 'isomorphic-form-data'
22

33
// patches FormData type by mutating it.
44
// patch :: FormData -> PatchedFormData
55
export const patch = (FormData) => {
6-
const create_entry = (field, value) => ({ name: field, value: value });
6+
const createEntry = (field, value) => ({name: field, value})
77

88
class PatchedFormData extends FormData {
99
constructor(form) {
10-
super(form);
11-
this._entryList = [];
10+
super(form)
11+
this._entryList = []
1212
}
1313

1414
append(field, value, options) {
15-
this._entryList.push(create_entry(field, value));
16-
return super.append(field, value, options);
15+
this._entryList.push(createEntry(field, value))
16+
return super.append(field, value, options)
1717
}
1818

1919
set(field, value) {
20-
const entry = create_entry(field, value);
20+
if (super.set) {
21+
return super.set(field, value)
22+
}
23+
const newEntry = createEntry(field, value)
2124

2225
this._entryList = this._entryList.filter((entry) => {
23-
return entry.name !== field;
24-
});
26+
return entry.name !== field
27+
})
2528

26-
this._entryList.push(entry);
29+
this._entryList.push(newEntry)
2730
}
2831

2932
get(field) {
33+
if (super.get) {
34+
return super.get(field)
35+
}
3036
const foundEntry = this._entryList.find((entry) => {
31-
return entry.name === field;
32-
});
37+
return entry.name === field
38+
})
3339

34-
return foundEntry === undefined ? null : foundEntry;
40+
return foundEntry === undefined ? null : foundEntry
3541
}
3642

3743
getAll(field) {
44+
if (super.getAll) {
45+
return super.getAll(field)
46+
}
3847
return this._entryList
3948
.filter((entry) => {
40-
return entry.name === field;
49+
return entry.name === field
4150
})
4251
.map((entry) => {
43-
return entry.value;
44-
});
52+
return entry.value
53+
})
4554
}
4655

4756
has(field) {
57+
if (super.has) {
58+
return super.has(field)
59+
}
4860
return this._entryList.some((entry) => {
49-
return entry.name === field;
50-
});
61+
return entry.name === field
62+
})
5163
}
5264
}
5365

54-
return PatchedFormData;
55-
};
66+
return PatchedFormData
67+
}
5668

57-
export default patch(IsomorphicFormData);
69+
export default patch(IsomorphicFormData)

test/http-multipart.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,15 @@ describe('buildRequest - openapi 2.0', () => {
5252
test.skip('should (Live) POST multipart-formdata with entry item entries', () => {
5353
return fetch('http://localhost:3300/api/v1/formdata', { // eslint-disable-line no-undef
5454
method: 'POST',
55-
body: req.body.stream, // per formdata-node docs
56-
headers: req.body.headers // per formdata-node docs
55+
body: req.body
5756
})
5857
.then((res) => {
5958
return res.json()
6059
})
6160
.then((json) => {
62-
expect(json.email.length).toEqual(2)
63-
expect(json.email[0]).toEqual('person1')
64-
expect(json.email[1]).toEqual('person2')
61+
expect(json.data.email.length).toEqual(2)
62+
expect(json.data.email[0]).toEqual('person1')
63+
expect(json.data.email[1]).toEqual('person2')
6564
})
6665
})
6766

@@ -84,8 +83,8 @@ describe('buildRequest - openapi 2.0', () => {
8483

8584
return fetch('http://localhost:3300/api/v1/formdata', { // eslint-disable-line no-undef
8685
method: 'POST',
87-
body: req.body.stream, // per formdata-node docs
88-
headers: req.body.headers // per formdata-node docs
86+
body: req.body,
87+
headers: req.headers
8988
})
9089
.then((res) => {
9190
return res.json()
@@ -94,10 +93,10 @@ describe('buildRequest - openapi 2.0', () => {
9493
expect(json.data.email.length).toEqual(2)
9594
expect(json.data.email[0]).toEqual('person1')
9695
expect(json.data.email[1]).toEqual('person2')
97-
// duck typing that fetch received a formdata-node Stream instead of plain object
96+
// duck typing that fetch received a FormData instance instead of plain object
9897
const lastOptions = fetchMock.lastOptions()
9998
expect(lastOptions.body.readable).toEqual(true)
100-
expect(lastOptions.body._readableState).toBeDefined()
99+
// expect(lastOptions.body._streams).toBeDefined()
101100
})
102101
})
103102
})
@@ -139,16 +138,15 @@ describe('buildRequest - openapi 3.0', () => {
139138
test.skip('should (Live) POST multipart-formdata with entry item entries', () => {
140139
return fetch('http://localhost:3300/api/v1/formdata', { // eslint-disable-line no-undef
141140
method: 'POST',
142-
body: req.body.stream, // per formdata-node docs
143-
headers: req.body.headers // per formdata-node docs
141+
body: req.body
144142
})
145143
.then((res) => {
146144
return res.json()
147145
})
148146
.then((json) => {
149-
expect(json.email.length).toEqual(2)
150-
expect(json.email[0]).toEqual('person1')
151-
expect(json.email[1]).toEqual('person2')
147+
expect(json.data.email.length).toEqual(2)
148+
expect(json.data.email[0]).toEqual('person1')
149+
expect(json.data.email[1]).toEqual('person2')
152150
})
153151
})
154152

@@ -171,8 +169,7 @@ describe('buildRequest - openapi 3.0', () => {
171169

172170
return fetch('http://localhost:3300/api/v1/formdata', { // eslint-disable-line no-undef
173171
method: 'POST',
174-
body: req.body.stream, // per formdata-node docs
175-
headers: req.body.headers // per formdata-node docs
172+
body: req.body,
176173
})
177174
.then((res) => {
178175
return res.json()
@@ -181,10 +178,10 @@ describe('buildRequest - openapi 3.0', () => {
181178
expect(json.data.email.length).toEqual(2)
182179
expect(json.data.email[0]).toEqual('person1')
183180
expect(json.data.email[1]).toEqual('person2')
184-
// duck typing that fetch received a formdata-node Stream instead of plain object
181+
// duck typing that fetch received a FormData instance instead of plain object
185182
const lastOptions = fetchMock.lastOptions()
186183
expect(lastOptions.body.readable).toEqual(true)
187-
expect(lastOptions.body._readableState).toBeDefined()
184+
// expect(lastOptions.body._streams).toBeDefined()
188185
})
189186
})
190187
})

0 commit comments

Comments
 (0)