From 34bc4ec5a6a1db5200508c7c98834fd8e10a3605 Mon Sep 17 00:00:00 2001 From: brevity Date: Thu, 22 Mar 2018 14:38:20 -0400 Subject: [PATCH 1/3] Fixes #23 add support for mediatypeextension --- osprey-mock-service.js | 6 +++++- test/fixtures/example.xml | 4 ++++ test/fixtures/example10.raml | 15 ++++++++++++++- test/test10.js | 27 +++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/example.xml diff --git a/osprey-mock-service.js b/osprey-mock-service.js index e2f8f5f..35805b7 100644 --- a/osprey-mock-service.js +++ b/osprey-mock-service.js @@ -115,7 +115,11 @@ 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) { + var ext = req.params.mediaTypeExtension.slice(1) + type = 'application/' + ext + } + var body = bodies[type] || {} res.statusCode = statusCode setHeaders(res, headers) diff --git a/test/fixtures/example.xml b/test/fixtures/example.xml new file mode 100644 index 0000000..51c3697 --- /dev/null +++ b/test/fixtures/example.xml @@ -0,0 +1,4 @@ + + foo + 23 + diff --git a/test/fixtures/example10.raml b/test/fixtures/example10.raml index 77f3af6..cd0c593 100644 --- a/test/fixtures/example10.raml +++ b/test/fixtures/example10.raml @@ -76,4 +76,17 @@ baseUri: http://example.com/api 200: headers: foo: - default: test \ No newline at end of file + default: test + + +/extension{mediaTypeExtension}: + get: + responses: + 200: + body: + application/json: + example: + stringProperty: foo + numberProperty: 23 + application/xml: + example: !include ./example.xml diff --git a/test/test10.js b/test/test10.js index 4e29120..c54e005 100644 --- a/test/test10.js +++ b/test/test10.js @@ -147,5 +147,32 @@ describe('osprey mock service v1.0', function () { expect(res.headers.foo).to.be.oneOf(['bar', 'foo', 'random', 'another']) }) }) + + it('should respect mediaTypeExtensions', function () { + return popsicle.default( + { + method: 'GET', + url: '/api/extension.xml' + } + ) + .use(server(http)) + .then(function (res) { + expect(res.body).to.contain('', '', '') + }) + }) + + it('should respect mediaTypeExtensions', function () { + return popsicle.default( + { + method: 'GET', + url: '/api/extension.json' + } + ) + .use(server(http)) + .then(function (res) { + expect(JSON.parse(res.body)) + .to.deep.equal({stringProperty: 'foo', numberProperty: 23}) + }) + }) }) }) From 91a4d60c943bfed8f2c1711d50a2ac2a4d487d13 Mon Sep 17 00:00:00 2001 From: brevity Date: Tue, 3 Apr 2018 19:55:56 -0400 Subject: [PATCH 2/3] add {ext} for raml1.0 --- osprey-mock-service.js | 5 +++-- test/fixtures/example10.raml | 14 +++++++++++++- test/test10.js | 6 +++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/osprey-mock-service.js b/osprey-mock-service.js index 35805b7..aca9e31 100644 --- a/osprey-mock-service.js +++ b/osprey-mock-service.js @@ -115,8 +115,9 @@ function handler (method) { return function (req, res) { var negotiator = new Negotiator(req) var type = negotiator.mediaType(types) - if (req.params && req.params.mediaTypeExtension) { - var ext = req.params.mediaTypeExtension.slice(1) + 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] || {} diff --git a/test/fixtures/example10.raml b/test/fixtures/example10.raml index cd0c593..db9eaa3 100644 --- a/test/fixtures/example10.raml +++ b/test/fixtures/example10.raml @@ -79,7 +79,19 @@ baseUri: http://example.com/api default: test -/extension{mediaTypeExtension}: +/mediatypeextension{mediaTypeExtension}: + get: + responses: + 200: + body: + application/json: + example: + stringProperty: foo + numberProperty: 23 + application/xml: + example: !include ./example.xml + +/ext{ext}: get: responses: 200: diff --git a/test/test10.js b/test/test10.js index c54e005..760c0fc 100644 --- a/test/test10.js +++ b/test/test10.js @@ -152,7 +152,7 @@ describe('osprey mock service v1.0', function () { return popsicle.default( { method: 'GET', - url: '/api/extension.xml' + url: '/api/mediatypeextension.xml' } ) .use(server(http)) @@ -161,11 +161,11 @@ describe('osprey mock service v1.0', function () { }) }) - it('should respect mediaTypeExtensions', function () { + it('should respect ext', function () { return popsicle.default( { method: 'GET', - url: '/api/extension.json' + url: '/api/ext.json' } ) .use(server(http)) From 9577b0d474a798c176dcd17353f6c8c40eda448a Mon Sep 17 00:00:00 2001 From: Jonathan Stoikovitch Date: Tue, 3 Apr 2018 18:01:41 -0700 Subject: [PATCH 3/3] adding missing test case --- test/fixtures/example10.raml | 9 +++++++++ test/test10.js | 38 ++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/test/fixtures/example10.raml b/test/fixtures/example10.raml index dbf2ba3..d4e7632 100644 --- a/test/fixtures/example10.raml +++ b/test/fixtures/example10.raml @@ -88,6 +88,15 @@ mediaType: application/json foo: default: test +/defaultmediatype: + get: + responses: + 200: + body: + example: + stringProperty: foo + numberProperty: 23 + /mediatypeextension{mediaTypeExtension}: get: responses: diff --git a/test/test10.js b/test/test10.js index e9750d8..20a135c 100644 --- a/test/test10.js +++ b/test/test10.js @@ -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 () { @@ -169,11 +169,11 @@ 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 () { @@ -183,10 +183,10 @@ describe('osprey mock service v1.0', function () { url: '/api/mediatypeextension.xml' } ) - .use(server(http)) - .then(function (res) { - expect(res.body).to.contain('', '', '') - }) + .use(server(http)) + .then(function (res) { + expect(res.body).to.contain('', '', '') + }) }) it('should respect ext', function () { @@ -196,11 +196,11 @@ describe('osprey mock service v1.0', function () { url: '/api/ext.json' } ) - .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}) + }) }) }) })