Skip to content

Commit 2086ef4

Browse files
Merge pull request core-api#29 from manosim/multipart-form
Support Multipart Form (Browser only)
2 parents 467d532 + 349043f commit 2086ef4

3 files changed

Lines changed: 43 additions & 11 deletions

File tree

lib/transports/http.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class HTTPTransport {
1818
this.schemes = ['http', 'https']
1919
this.csrf = options.csrf
2020
this.fetch = options.fetch || fetch
21+
this.FormData = options.FormData || window.FormData
2122
}
2223

2324
action (link, decoders, params = {}) {
@@ -71,12 +72,17 @@ class HTTPTransport {
7172
'Content-Type': 'application/json'
7273
}
7374
} else if (link.encoding === 'multipart/form-data') {
74-
// FIXME!
75+
let form = new this.FormData()
76+
77+
for (let paramKey in formParams) {
78+
form.append(paramKey, formParams[paramKey])
79+
}
80+
options.body = form
7581
} else if (link.encoding === 'application/x-www-form-urlencoded') {
76-
var formBody = []
77-
for (var paramKey in formParams) {
78-
var encodedKey = encodeURIComponent(paramKey)
79-
var encodedValue = encodeURIComponent(formParams[paramKey])
82+
let formBody = []
83+
for (let paramKey in formParams) {
84+
const encodedKey = encodeURIComponent(paramKey)
85+
const encodedValue = encodeURIComponent(formParams[paramKey])
8086
formBody.push(encodedKey + '=' + encodedValue)
8187
}
8288
formBody = formBody.join('&')

tests/__helpers__/utils.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
class MockedFormData {
2+
constructor () {
3+
this.params = []
4+
}
5+
6+
append (key, value) {
7+
this.params.push([key, value])
8+
}
9+
}
10+
111
const mockedFetch = function (responseBody, contentType, statusCode = 200) {
212
return (url) => {
313
return new Promise((resolve, reject) => {
@@ -36,7 +46,9 @@ const echo = function (url, options = {}) {
3646
const textPromise = () => {
3747
return new Promise((resolve, reject) => {
3848
let result
39-
if (body) {
49+
if (body instanceof MockedFormData) {
50+
result = JSON.stringify({url: url, method: method, headers: headers, form: body.params})
51+
} else if (body) {
4052
result = JSON.stringify({url: url, method: method, headers: headers, body: body})
4153
} else {
4254
result = JSON.stringify({url: url, method: method, headers: headers})
@@ -65,6 +77,7 @@ const echo = function (url, options = {}) {
6577
}
6678

6779
module.exports = {
80+
MockedFormData: MockedFormData,
6881
mockedFetch: mockedFetch,
6982
echo: echo
7083
}

tests/transports/http.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,26 @@ describe('Test the HTTPTransport', function () {
3131
.then(res => expect(res).toEqual({text: 'Hello, world'}))
3232
})
3333

34-
xit('should check the action function of an HTTP transport (multipart/form-data)', function () {
34+
it('should check the action function of an HTTP transport (multipart/form-data)', function () {
3535
const url = 'http://www.example.com/'
36-
const link = new document.Link(url, 'get', 'multipart/form-data')
37-
const transport = new transports.HTTPTransport(testUtils.mockedFetch('{"text": "Hello, world"}', 'application/json'))
36+
const fields = [new document.Field('firstField', true, 'form'), new document.Field('secondField', true, 'form')]
37+
const link = new document.Link(url, 'post', 'multipart/form-data', fields)
38+
const transport = new transports.HTTPTransport({
39+
fetch: testUtils.echo,
40+
FormData: testUtils.MockedFormData
41+
})
42+
const params = {
43+
firstField: 'hello',
44+
secondField: 'world'
45+
}
3846

39-
return transport.action(link, decoders)
40-
.then(res => expect(res).toEqual({text: 'Hello, world'}))
47+
return transport.action(link, decoders, params)
48+
.then(res => expect(res).toEqual({
49+
url: 'http://www.example.com/',
50+
method: 'POST',
51+
headers: {},
52+
form: [['firstField', 'hello'], ['secondField', 'world']]
53+
}))
4154
})
4255

4356
it('should check the action function of an HTTP transport (application/x-www-form-urlencoded)', function () {

0 commit comments

Comments
 (0)