8000 Support x-www-form-urlencoded encoding · core-api/javascript-client@1ca608e · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit 1ca608e

Browse files
committed
Support x-www-form-urlencoded encoding
1 parent 0f13985 commit 1ca608e

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

lib/transports/http.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,28 @@ class HTTPTransport {
6161
}
6262

6363
let options = {method: link.method}
64+
6465
if (hasBody) {
65-
options.body = JSON.stringify(formParams)
66-
options.headers = {
67-
'Content-Type': 'application/json'
66+
if (link.encoding === 'application/json') {
67+
options.body = JSON.stringify(formParams)
68+
options.headers = {
69+
'Content-Type': 'application/json'
70+
}
71+
} else if (link.encoding === 'multipart/form-data') {
72+
// FIXME!
73+
} else if (link.encoding === 'application/x-www-form-urlencoded') {
74+
var formBody = []
75+
for (var paramKey in formParams) {
76+
var encodedKey = encodeURIComponent(paramKey)
77+
var encodedValue = encodeURIComponent(formParams[paramKey])
78+
formBody.push(encodedKey + '=' + encodedValue)
79+
}
80+
formBody = formBody.join('&')
81+
82+
options.body = formBody
83+
options.headers = {
84+
'Content-Type': 'application/x-www-form-urlencoded'
85+
}
6886
}
6987
}
7088

tests/__helpers__/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ const echo = function (url, options = {}) {
3636
return new Promise((resolve, reject) => {
3737
let result
3838
if (body) {
39-
result = `{"url": "${url}", "method": "${method}", "body": ${body}}`
39+
result = JSON.stringify({url: url, method: method, body: body})
4040
} else {
41-
result = `{"url": "${url}", "method": "${method}"}`
41+
result = JSON.stringify({url: url, method: method})
4242
}
4343
process.nextTick(
4444
resolve(result)

tests/transports/http.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,33 @@ describe('Test the HTTPTransport', function () {
2727
.then(res => expect(res).toEqual({text: 'Hello, world'}))
2828
})
2929

30+
xit('should check the action function of an HTTP transport (multipart/form-data)', function () {
31+
const url = 'http://www.example.com/'
32+
const link = new document.Link(url, 'get', 'multipart/form-data')
33+
const transport = new transports.HTTPTransport(testUtils.mockedFetch('{"text": "Hello, world"}', 'application/json'))
34+
35+
return transport.action(link, decoders)
36+
.then(res => expect(res).toEqual({text: 'Hello, world'}))
37+
})
38+
39+
it('should check the action function of an HTTP transport (application/x-www-form-urlencoded)', function () {
40+
const url = 'http://www.example.com/'
41+
const fields = [new document.Field('firstField', true, 'form'), new document.Field('secondField', true, 'form')]
42+
const link = new document.Link(url, 'post', 'application/x-www-form-urlencoded', fields)
43+
const transport = new transports.HTTPTransport(testUtils.echo)
44+
const params = {
45+
firstField: 'hello',
46+
secondField: 'world'
47+
}
48+
49+
return transport.action(link, decoders, params)
50+
.then(res => expect(res).toEqual({
51+
body: 'firstField=hello&secondField=world',
52+
method: 'post',
53+
url: 'http://www.example.com/'
54+
}))
55+
})
56+
3057
it('should check the action function of an HTTP transport (network fail)', function () {
3158
const url = 'http://www.example.com/'
3259
const link = new document.Link(url, 'get')
@@ -87,7 +114,7 @@ describe('Test the HTTPTransport', function () {
87114

88115
return transport.action(link, decoders, params)
89116
.then((res) => {
90-
expect(res).toEqual({url: 'http://www.example.com/', metho 6D4E d: 'post', body: {hello: 'world'}})
117+
expect(res).toEqual({url: 'http://www.example.com/', method: 'post', body: JSON.stringify({hello: 'world'})})
91118
})
92119
})
93120

@@ -102,7 +129,7 @@ describe('Test the HTTPTransport', function () {
102129

103130
return transport.action(link, decoders, params)
104131
.then((res) => {
105-
expect(res).toEqual({url: 'http://www.example.com/', method: 'post', body: 'world'})
132+
expect(res).toEqual({url: 'http://www.example.com/', method: 'post', body: JSON.stringify('world')})
106133
})
107134
})
108135

0 commit comments

Comments
 (0)
0