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

Commit

Permalink
Merge pull request #25 from manosim/link-encodings
Browse files Browse the repository at this point in the history
Link / Encoding
  • Loading branch information
tomchristie authored Jan 18, 2017
2 parents 733cb05 + 7fb0f9a commit 78fe002
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/codecs/corejson.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function primitiveToNode (data, baseUrl) {
let field = new document.Field(name, required, location, fieldDescription)
fields.push(field)
}
return new document.Link(url, method, fields, title, description)
return new document.Link(url, method, 'application/json', fields, title, description)
} else if (isObject) {
// Object
let content = {}
Expand Down
3 changes: 2 additions & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Document {
}

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

this.url = url
this.method = method
this.encoding = encoding
this.fields = fields
this.title = title
this.description = description
Expand Down
24 changes: 22 additions & 2 deletions lib/transports/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,29 @@ class HTTPTransport {
}

let options = {method: method, headers: {}}

if (hasBody) {
options.body = JSON.stringify(formParams)
options.headers['Content-Type'] = 'application/json'
if (link.encoding === 'application/json') {
options.body = JSON.stringify(formParams)
options.headers = {
'Content-Type': 'application/json'
}
} else if (link.encoding === 'multipart/form-data') {
// FIXME!
} else if (link.encoding === 'application/x-www-form-urlencoded') {
var formBody = []
for (var paramKey in formParams) {
var encodedKey = encodeURIComponent(paramKey)
var encodedValue = encodeURIComponent(formParams[paramKey])
formBody.push(encodedKey + '=' + encodedValue)
}
formBody = formBody.join('&')

options.body = formBody
options.headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
}

if (this.csrf) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "coreapi",
"version": "0.0.17",
"version": "0.0.18",
"description": "Javascript client library for Core API",
"main": "lib/index.js",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions tests/__helpers__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ const mockedFetch = function (responseBody, contentType, statusCode = 200) {

const echo = function (url, options = {}) {
const method = options.method
const headers = JSON.stringify(options.headers)
const headers = options.headers
const body = options.body

return new Promise((resolve, reject) => {
const textPromise = () => {
return new Promise((resolve, reject) => {
let result
if (body) {
result = `{"url": "${url}", "method": "${method}", "headers": ${headers}, "body": ${body}}`
result = JSON.stringify({url: url, method: method, headers: headers, body: body})
} else {
result = `{"url": "${url}", "method": "${method}", "headers": ${headers}}`
result = JSON.stringify({url: url, method: method, headers: headers})
}
process.nextTick(
resolve(result)
Expand Down
2 changes: 1 addition & 1 deletion tests/codecs/corejson.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Test the CoreJSON Codec', function () {
const node = codec.decode(text)

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

it('should test decoding a document (including a nested link)', function () {
Expand Down
46 changes: 38 additions & 8 deletions tests/transports/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,36 @@ describe('Test the HTTPTransport', function () {
.then(res => expect(res).toEqual({text: 'Hello, world'}))
})

xit('should check the action function of an HTTP transport (multipart/form-data)', function () {
const url = 'http://www.example.com/'
const link = new document.Link(url, 'get', 'multipart/form-data')
const transport = new transports.HTTPTransport(testUtils.mockedFetch('{"text": "Hello, world"}', 'application/json'))

return transport.action(link, decoders)
.then(res => expect(res).toEqual({text: 'Hello, world'}))
})

it('should check the action function of an HTTP transport (application/x-www-form-urlencoded)', function () {
const url = 'http://www.example.com/'
const fields = [new document.Field('firstField', true, 'form'), new document.Field('secondField', true, 'form')]
const link = new document.Link(url, 'post', 'application/x-www-form-urlencoded', fields)
const transport = new transports.HTTPTransport(null, testUtils.echo)
const params = {
firstField: 'hello',
secondField: 'world'
}

return transport.action(link, decoders, params)
.then(res => expect(res).toEqual({
url: 'http://www.example.com/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: 'firstField=hello&secondField=world'
}))
})

it('should check the action function of an HTTP transport (network fail)', function () {
const url = 'http://www.example.com/'
const link = new document.Link(url, 'get')
Expand All @@ -42,7 +72,7 @@ describe('Test the HTTPTransport', function () {
it('should check the action function of an HTTP transport (json) with query params', function () {
const url = 'http://www.example.com/'
const fields = [new document.Field('page', false, 'query')]
const link = new document.Link(url, 'get', fields)
const link = new document.Link(url, 'get', 'application/json', fields)
const transport = new transports.HTTPTransport(null, testUtils.echo)
const params = {
page: 23
Expand All @@ -57,7 +87,7 @@ describe('Test the HTTPTransport', function () {
it('should check the action function of an HTTP transport (json) with path params', function () {
const url = 'http://www.example.com/{user}/'
const fields = [new document.Field('user', true, 'path')]
const link = new document.Link(url, 'get', fields)
const link = new document.Link(url, 'get', 'application/json', fields)
const transport = new transports.HTTPTransport(null, testUtils.echo)
const params = {
user: 23
Expand Down Expand Up @@ -107,37 +137,37 @@ describe('Test the HTTPTransport', function () {
it('should check the action function of an HTTP transport (json) with post request and form parameters', function () {
const url = 'http://www.example.com/'
const fields = [new document.Field('hello', true, 'form')]
const link = new document.Link(url, 'post', fields)
const link = new document.Link(url, 'post', 'application/json', fields)
const transport = new transports.HTTPTransport(null, testUtils.echo)
const params = {
hello: 'world'
}

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

it('should check the action function of an HTTP transport (json) with post request and a body parameter', function () {
const url = 'http://www.example.com/'
const fields = [new document.Field('hello', true, 'body')]
const link = new document.Link(url, 'post', fields)
const link = new document.Link(url, 'post', 'application/json', fields)
const transport = new transports.HTTPTransport(null, testUtils.echo)
const params = {
hello: 'world'
}

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

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

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

Expand Down

0 comments on commit 78fe002

Please sign in to comment.