Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #50 from mulesoft-labs/23_support_mediaTypeExtension
Browse files Browse the repository at this point in the history
Fixes #23 add support for mediatypeextension
  • Loading branch information
jstoiko authored Apr 4, 2018
2 parents 8b65548 + 9577b0d commit 7311a36
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
7 changes: 6 additions & 1 deletion osprey-mock-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ function handler (method) {
return function (req, res) {
var negotiator = new Negotiator(req)
var type = negotiator.mediaType(types)
var body = bodies[type]
if (req.params && (req.params.mediaTypeExtension || req.params.ext)) {
var ext = req.params.mediaTypeExtension || req.params.ext
ext = ext.slice(1)
type = 'application/' + ext
}
var body = bodies[type] || {}

res.statusCode = statusCode
setHeaders(res, headers)
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/example.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resource>
<stringProperty>foo</stringProperty>
<numberProperty>23</numberProperty>
</resource>
26 changes: 26 additions & 0 deletions test/fixtures/example10.raml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ mediaType: application/json
headers:
foo:
default: test

/defaultmediatype:
get:
responses:
Expand All @@ -95,3 +96,28 @@ mediaType: application/json
example:
stringProperty: foo
numberProperty: 23

/mediatypeextension{mediaTypeExtension}:
get:
responses:
200:
body:
application/json:
example:
stringProperty: foo
numberProperty: 23
application/xml:
example: !include ./example.xml

/ext{ext}:
get:
responses:
200:
body:
application/json:
example:
stringProperty: foo
numberProperty: 23
application/xml:
example: !include ./example.xml

47 changes: 37 additions & 10 deletions test/test10.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ describe('osprey mock service v1.0', function () {
url: '/api/boolean'
}
)
.use(server(http))
.then(function (res) {
expect(JSON.parse(res.body)).to.equal(true)
expect(res.status).to.equal(200)
})
.use(server(http))
.then(function (res) {
expect(JSON.parse(res.body)).to.equal(true)
expect(res.status).to.equal(200)
})
})

it('should respond with multiple examples', function () {
Expand Down Expand Up @@ -169,11 +169,38 @@ describe('osprey mock service v1.0', function () {
url: '/api/defaultmediatype'
}
)
.use(server(http))
.then(function (res) {
expect(JSON.parse(res.body))
.to.deep.equal({stringProperty: 'foo', numberProperty: 23})
})
.use(server(http))
.then(function (res) {
expect(JSON.parse(res.body))
.to.deep.equal({stringProperty: 'foo', numberProperty: 23})
})
})

it('should respect mediaTypeExtensions', function () {
return popsicle.default(
{
method: 'GET',
url: '/api/mediatypeextension.xml'
}
)
.use(server(http))
.then(function (res) {
expect(res.body).to.contain('<resource>', '<stringProperty>', '<numberProperty>')
})
})

it('should respect ext', function () {
return popsicle.default(
{
method: 'GET',
url: '/api/ext.json'
}
)
.use(server(http))
.then(function (res) {
expect(JSON.parse(res.body))
.to.deep.equal({stringProperty: 'foo', numberProperty: 23})
})
})
})
})

0 comments on commit 7311a36

Please sign in to comment.