Skip to content

Commit

Permalink
Merge pull request #79 from eXa-online/enhanced-content-types
Browse files Browse the repository at this point in the history
Add enhanced media-type support for JSON payloads
  • Loading branch information
plroebuck committed Feb 19, 2016
2 parents b19c832 + 14f6242 commit 86f2e05
Show file tree
Hide file tree
Showing 4 changed files with 458 additions and 8 deletions.
23 changes: 17 additions & 6 deletions lib/add-tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,31 @@ addTests = (raml, tests, hooks, parent, callback, testFactory) ->
test.request.path = path
test.request.method = method
test.request.headers = parseHeaders(api.headers)
if api.body?['application/json']
test.request.headers['Content-Type'] = 'application/json'

# select compatible content-type in request body (to support vendor tree types, i.e. application/vnd.api+json)
contentType = (type for type of api.body when type.match(/^application\/(.*\+)?json/i))?[0]
if contentType
test.request.headers['Content-Type'] = contentType
try
test.request.body = JSON.parse api.body['application/json']?.example
test.request.body = JSON.parse api.body[contentType]?.example
catch
console.warn "invalid request example of #{test.name}"
test.request.params = params

# Update test.response
test.response.status = status
test.response.schema = null
if (res?.body?['application/json']?.schema)
test.response.schema = parseSchema res.body['application/json'].schema


if res?.body
# expect content-type of response body to be identical to request body
if contentType && res.body[contentType]?.schema
test.response.schema = parseSchema res.body[contentType].schema
# otherwise filter in responses section for compatible content-types (vendor tree, i.e. application/vnd.api+json)
else
contentType = (type for type of res.body when type.match(/^application\/(.*\+)?json/i))?[0]
if res.body[contentType]?.schema
test.response.schema = parseSchema res.body[contentType].schema

callback()
, (err) ->
return callback(err) if err
Expand Down
43 changes: 43 additions & 0 deletions test/fixtures/vendor-content-type.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#%RAML 0.8

title: World Music API
baseUri: http://example.api.com/{version}
version: v1

/{songId}:
uriParameters:
songId:
example: "mike-a-beautiful-day"
patch:
body:
application/vnd.api+json:
schema: |
{ "$schema": "http://json-schema.org/schema",
"type": "object",
"description": "A canonical song",
"properties": {
"title": { "type": "string" },
"artist": { "type": "string" }
},
"required": [ "title", "artist" ]
}
example: |
{ "title": "A Beautiful Day", "artist": "Mike" }
text/plain:
responses:
200:
body:
application/vnd.api+json:
schema: |
{ "$schema": "http://json-schema.org/schema",
"type": "object",
"description": "A canonical song",
"properties": {
"title": { "type": "string" },
"artist": { "type": "string" }
},
"required": [ "title", "artist" ]
}
example: |
{ "title": "A Beautiful Day", "artist": "Mike" }
text/plain:
47 changes: 45 additions & 2 deletions test/unit/add-tests-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe '#addTests', ->

it 'should setup test.response', ->
res = tests[0].response

assert.equal res.status, 200
assert.equal res.schema?.properties?.chick?.type, "string"
assert.isNull res.headers
Expand Down Expand Up @@ -192,7 +192,7 @@ describe '#addTests', ->

it 'should setup test.response', ->
res = tests[0].response

assert.equal res.status, 200
assert.equal res.schema?.properties?.type["$ref"], "type2"
assert.isNull res.headers
Expand Down Expand Up @@ -312,3 +312,46 @@ describe '#addTests', ->
assert.lengthOf tests, 1
assert.equal tests[0].name, 'POST /machines -> 204'

describe 'when raml contains vendor specifc JSON content-types', ->
tests = []
testFactory = new TestFactory()
callback = ''

before (done) ->
ramlParser.loadFile("#{__dirname}/../fixtures/vendor-content-type.raml")
.then (data) ->
callback = sinon.stub()
callback.returns(done())

addTests data, tests, hooks, callback, testFactory
, done
after ->
tests = []

it 'should run callback', ->
assert.ok callback.called

it 'should added a test', ->
assert.lengthOf tests, 1

it 'should setup test.request of PATCH', ->
req = tests[0].request

assert.equal req.path, '/{songId}'
assert.deepEqual req.params,
songId: 'mike-a-beautiful-day'
assert.deepEqual req.query, {}
assert.deepEqual req.headers,
'Content-Type': 'application/vnd.api+json'
assert.deepEqual req.body,
title: 'A Beautiful Day'
artist: 'Mike'
assert.equal req.method, 'PATCH'

it 'should setup test.response of PATCH', ->
res = tests[0].response

assert.equal res.status, 200
schema = res.schema
assert.equal schema.properties.title.type, 'string'
assert.equal schema.properties.artist.type, 'string'
Loading

0 comments on commit 86f2e05

Please sign in to comment.