From 24e1db3df775c03209ac9c5fd0a1ea6dad57faed Mon Sep 17 00:00:00 2001 From: Chris Vigelius Date: Thu, 16 Nov 2017 11:51:50 +0100 Subject: [PATCH] respect declared encoding of link --- lib/codecs/corejson.js | 3 ++- tests/codecs/corejson.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/codecs/corejson.js b/lib/codecs/corejson.js index d1d8a67..2d511f2 100644 --- a/lib/codecs/corejson.js +++ b/lib/codecs/corejson.js @@ -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] @@ -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 = {} diff --git a/tests/codecs/corejson.js b/tests/codecs/corejson.js index c50df1d..0ba0482 100644 --- a/tests/codecs/corejson.js +++ b/tests/codecs/corejson.js @@ -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)