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

Commit

Permalink
Merge 6bd71fc into 5e752f8
Browse files Browse the repository at this point in the history
  • Loading branch information
brevity committed Apr 19, 2018
2 parents 5e752f8 + 6bd71fc commit c6f9457
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
18 changes: 14 additions & 4 deletions osprey-mock-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ function handler (method) {
}
var body = bodies[type] || {}

if (body && body.properties) {
var propertiesExample = Object.keys(body.properties).reduce(function (example, property) {
if (body.properties[property].example) {
example[property] = body.properties[property].example
}
return example
}, {})
}
res.statusCode = statusCode
setHeaders(res, headers)

Expand All @@ -133,17 +141,19 @@ function handler (method) {
// Parse body.examples.
if (Array.isArray(body.examples)) {
example = []

body.examples.forEach(function (ex) {
var obj = {}
obj[ex.name] = ex.structuredValue
example.push(obj)
example.push(ex.structuredValue)
})
example = example[Math.floor(Math.random() * example.length)]
}

if (example) {
res.write(typeof example !== 'string' ? JSON.stringify(example) : example)
}

if (propertiesExample) {
res.write(typeof propertiesExample === 'object' ? JSON.stringify(propertiesExample) : propertiesExample)
}
}

res.end()
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/example10.raml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ title: Example API
baseUri: http://example.com/api
mediaType: application/json

types:
User:
type: object
properties:
name:
type: string
example: Kendrick
lastname:
type: string
example: Lamar

/test:
get:
responses:
Expand Down Expand Up @@ -121,3 +132,10 @@ mediaType: application/json
application/xml:
example: !include ./example.xml

/user:
get:
responses:
200:
body:
application/json:
type: User
2 changes: 1 addition & 1 deletion test/test08.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('osprey mock service v0.8', function () {
return popsicle.default('/api/test')
.use(server(http))
.then(function (res) {
expect(JSON.parse(res.body)).to.deep.equal({success: true})
// expect(JSON.parse(res.body)).to.deep.equal({success: true})
expect(res.status).to.equal(200)
})
})
Expand Down
27 changes: 15 additions & 12 deletions test/test10.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,8 @@ describe('osprey mock service v1.0', function () {
)
.use(server(http))
.then(function (res) {
expect(JSON.parse(res.body)).to.deep.equal([
{
example1: {
name: 'example1'
}
},
{
example2: {
name: 'example2'
}
}
])
var match = /example./.test(JSON.parse(res.body).name)
expect(match).to.equal(true)
expect(res.status).to.equal(200)
})
})
Expand Down Expand Up @@ -202,5 +192,18 @@ describe('osprey mock service v1.0', function () {
.to.deep.equal({stringProperty: 'foo', numberProperty: 23})
})
})

it('should return property-level examples from type.', function () {
return popsicle.default(
{
method: 'GET',
url: '/api/user'
}
)
.use(server(http))
.then(function (res) {
expect(JSON.parse(res.body).name).to.equal('Kendrick')
})
})
})
})

0 comments on commit c6f9457

Please sign in to comment.