10000 Merge pull request #25 from manosim/link-encodings · core-api/javascript-client@78fe002 · 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 78fe002

Browse files
authored
Merge pull request #25 from manosim/link-encodings
Link / Encoding
2 parents 733cb05 + 7fb0f9a commit 78fe002

File tree

7 files changed

+68
-17
lines changed

7 files changed

+68
-17
lines changed

lib/codecs/corejson.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function primitiveToNode (data, baseUrl) {
8383
let field = new document.Field(name, required, location, fieldDescription)
8484
fields.push(field)
8585
}
86-
return new document.Link(url, method, fields, title, description)
86+
return new document.Link(url, method, 'application/json', fields, title, description)
8787
} else if (isObject) {
8888
// Object
8989
let content = {}

lib/document.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Document {
88
}
99

1010
class Link {
11-
constructor (url, method, fields = [], title = '', description = '') {
11+
constructor (url, method, encoding = 'application/json', fields = [], title = '', description = '') {
1212
if (url === undefined) {
1313
throw new Error('url argument is required')
1414
}
@@ -19,6 +19,7 @@ class Link {
1919

2020
this.url = url
2121
this.method = method
22+
this.encoding = encoding
2223
this.fields = fields
2324
this.title = title
2425
this.description = description

lib/transports/http.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,29 @@ class HTTPTransport {
6363
}
6464

6565
let options = {method: method, headers: {}}
66+
6667
if (hasBody) {
67-
options.body = JSON.stringify(formParams)
68-
options.headers['Content-Type'] = 'application/json'
68+
if (link.encoding === 'application/json') {
69+
options.body = JSON.stringify(formParams)
70+
options.headers = {
71+
'Content-Type': 'application/json'
72+
}
73+
} else if (link.encoding === 'multipart/form-data') {
74+
// FIXME!
75+
} 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])
80+
formBody.push(encodedKey + '=' + encodedValue)
81+
}
82+
formBody = formBody.join('&')
83+
84+
options.body = formBody
85+
options.headers = {
86+
'Content-Type': 'application/x-www-form-urlencoded'
87+
}
88+
}
6989
}
7090

7191
if (this.csrf) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "coreapi",
3-
"version": "0.0.17",
3+
"version": "0.0.18",
44
"description": "Javascript client library for Core API",
55
"main": "lib/index.js",
66
"scripts": {

tests/__helpers__/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ const mockedFetch = function (responseBody, contentType, statusCode = 200) {
2929

3030
const echo = function (url, options = {}) {
3131
const method = options.method
32-
const headers = JSON.stringify(options.headers)
32+
const headers = options.headers
3333
const body = options.body
3434

3535
return new Promise((resolve, reject) => {
3636
const textPromise = () => {
3737
return new Promise((resolve, reject) => {
3838
let result
3939
if (body) {
40-
result = `{"url": "${url}", "method": "${method}", "headers": ${headers}, "body": ${body}}`
40+
result = JSON.stringify({url: url, method: method, headers: headers, body: body})
4141
} else {
42-
result = `{"url": "${url}", "method": "${method}", "headers": ${headers}}`
42+
result = JSON.stringify({url: url, method: method, headers: headers})
4343
}
4444
process.nextTick(
4545
resolve(result)

tests/codecs/corejson.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('Test the CoreJSON Codec', function () {
3535
const node = codec.decode(text)
3636

3737
expect(node instanceof document.Document).toBeTruthy()
38-
expect(node.content).toEqual({link: new document.Link('http://example.com/', 'get', [new document.Field('page', false, 'query')])})
38+
expect(node.content).toEqual({link: new document.Link('http://example.com/', 'get', 'application/json', [new document.Field('page', false, 'query')])})
3939
})
4040

4141
it('should test decoding a document (including a nested link)', function () {

tests/transports/http.js

Lines changed: 38 additions & 8 deletions
const params = {
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,36 @@ 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(null, 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+
url: 'http://www.example.com/',
52+
method: 'POST',
53+
headers: {
54+
'Content-Type': 'application/x-www-form-urlencoded'
55+
},
56+
body: 'firstField=hello&secondField=world'
57+
}))
58+
})
59+
3060
it('should check the action function of an HTTP transport (network fail)', function () {
3161
const url = 'http://www.example.com/'
3262
const link = new document.Link(url, 'get')
@@ -42,7 +72,7 @@ describe('Test the HTTPTransport', function () {
4272
it('should check the action function of an HTTP transport (json) with query params', function () {
4373
const url = 'http://www.example.com/'
4474
const fields = [new document.Field('page', false, 'query')]
45-
const link = new document.Link(url, 'get', fields)
75+
const link = new document.Link(url, 'get', 'application/json', fields)
4676
const transport = new transports.HTTPTransport(null, testUtils.echo)
4777
const params = {
4878
page: 23
@@ -57,7 +87,7 @@ describe('Test the HTTPTransport', function () {
5787
it('should check the action function of an HTTP transport (json) with path params', function () {
5888
const url = 'http://www.example.com/{user}/'
5989
const fields = [new document.Field('user', true, 'path')]
60-
const link = new document.Link(url, 'get', fields)
90+
const link = new document.Link(url, 'get', 'application/json', fields)
6191
const transport = new transports.HTTPTransport(null, testUtils.echo)
6292
const params = {
6393
user: 23
@@ -107,37 +137,37 @@ describe('Test the HTTPTransport', function () {
107137
it('should check the action function of an HTTP transport (json) with post request and form parameters', function () {
108138
const url = 'http://www.example.com/'
109139
const fields = [new document.Field('hello', true, 'form')]
110-
const link = new document.Link(url, 'post', fields)
140+
const link = new document.Link(url, 'post', 'application/json', fields)
111141
const transport = new transports.HTTPTransport(null, testUtils.echo)
112142
const params = {
113143
hello: 'world'
114144
}
115145

116146
return transport.action(link, decoders, params)
117147
.then((res) => {
118-
expect(res).toEqual({url: 'http://www.example.com/', method: 'POST', headers: {'Content-Type': 'application/json'}, body: {hello: 'world'}})
148+
expect(res).toEqual({url: 'http://www.example.com/', method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({hello: 'world'})})
119149
})
120150
})
121151

122152
it('should check the action function of an HTTP transport (json) with post request and a body parameter', function () {
123153
const url = 'http://www.example.com/'
124154
const fields = [new document.Field('hello', true, 'body')]
125-
const link = new document.Link(url, 'post', fields)
155+
const link = new document.Link(url, 'post', 'application/json', fields)
126156
const transport = new transports.HTTPTransport(null, testUtils.echo)
127157
128158
hello: 'world'
129159
}
130160

131161
return transport.action(link, decoders, params)
132162
.then((res) => {
133-
expect(res).toEqual({url: 'http://www.example.com/', method: 'POST', headers: {'Content-Type': 'application/json'}, body: 'world'})
163+
expect(res).toEqual({url: 'http://www.example.com/', method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify('world')})
134164
})
135165
})
136166

137167
it('should check the action function of an HTTP transport (json) with missing optional query params', function () {
138168
const url = 'http://www.example.com/'
139169
const fields = [new document.Field('page', false, 'query')]
140-
const link = new document.Link(url, 'get', fields)
170+
const link = new document.Link(url, 'get', 'application/json', fields)
141171
const transport = new transports.HTTPTransport(null, testUtils.echo)
142172
const params = {}
143173

@@ -150,7 +180,7 @@ describe('Test the HTTPTransport', function () {
150180
it('should check the action function of an HTTP transport (json) with missing required parameter', function () {
151181
const url = 'http://www.example.com/{user}/'
152182
const fields = [new document.Field('user', true, 'path')]
153-
const link = new document.Link(url, 'get', fields)
183+
const link = new document.Link(url, 'get', 'application/json', fields)
154184
const transport = new transports.HTTPTransport(null, testUtils.echo)
155185
const params = {}
156186

0 commit comments

Comments
 (0)
0