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

support multipart/form-data (e.g. file uploads) #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/codecs/corejson.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function primitiveToNode (data, baseUrl) {
const title = getString(data, 'title')
const description = getString(data, 'description')
const fieldsData = getArray(data, 'fields')
const encoding = getString(data, 'encoding') || 'application/json'
var fields = []
for (let idx = 0, len = fieldsData.length; idx < len; idx++) {
let value = fieldsData[idx]
Expand All @@ -83,7 +84,7 @@ function primitiveToNode (data, baseUrl) {
let field = new document.Field(name, required, location, fieldDescription)
fields.push(field)
}
return new document.Link(url, method, 'application/json', fields, title, description)
return new document.Link(url, method, encoding, fields, title, description)
} else if (isObject) {
// Object
let content = {}
Expand Down
16 changes: 16 additions & 0 deletions tests/codecs/corejson.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ describe('Test the CoreJSON Codec', function () {
expect(node.fields).toEqual([new document.Field('foo', true)])
})

it('should test decoding a link without declared encoding', function () {
const text = '{"_type": "link", "url": "http://example.com/", "fields": [{"name": "foo", "required": true}]}'
const node = codec.decode(text)
expect(node instanceof document.Link).toBeTruthy()
expect(node.fields).toEqual([new document.Field('foo', true)])
expect(node.encoding).toEqual('application/json')
})

it('should test decoding a link with declared encoding', function () {
const text = '{"_type": "link", "url": "http://example.com/", "encoding": "multipart/form-data", "fields": [{"name": "foo", "required": true}]}'
const node = codec.decode(text)
expect(node instanceof document.Link).toBeTruthy()
expect(node.fields).toEqual([new document.Field('foo', true)])
expect(node.encoding).toEqual('multipart/form-data')
})

it('should test decoding a primitive', function () {
const text = '123'
const node = codec.decode(text)
Expand Down