Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Before hook tests passing
Browse files Browse the repository at this point in the history
Removed unnecessary guards, added in other necessary guards
Updated test provider
  • Loading branch information
Donald Robertson committed May 1, 2017
1 parent 4c4037a commit bf533bb
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 195 deletions.
10 changes: 5 additions & 5 deletions lib/contract/hook/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ module.exports = (variables, hook, done) => {
request(_request, (err, res, body) => {
if (err) { return done(err) }

const responseBody = parseResponseBody(res, body)
if (typeof responseBody === 'object' && hook.variables) {
const result = _.mapValues(hook.variables, extractValue(responseBody))
variables = _.merge(variables, result)
}
const responseBody = body ? parseResponseBody(res, body) : body
res.body = responseBody

const result = _.mapValues(hook.variables, extractValue(res))
variables = _.merge(variables, result)

return done(null, variables)
})
Expand Down
16 changes: 9 additions & 7 deletions lib/request/compile-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ const _ = require('lodash')
const hyperid = require('hyperid')
const instance = hyperid()

module.exports = (body) => {
if (body.includes('<%=') && body.includes('%>')) {
const compileBody = _.template(body)
return compileBody({ hyperid: instance() })
}
// module.exports = (body) => {
// if (body.includes('<%=') && body.includes('%>')) {
// const compileBody = _.template(body)
// return compileBody({ hyperid: instance() })
// }
//
// return body
// }

return body
}
module.exports = (body) => _.template(body)({ hyperid: instance() })
6 changes: 6 additions & 0 deletions lib/request/compile-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ module.exports = (headers, variables) => {
return _.mapValues(headers, compileHeader(variables))
}

// const compileHeader = (variables) => (value) => {
// return value.includes('<%=') && value.includes('%>')
// ? _.template(value)(variables)
// : value
// }

const compileHeader = (variables) => (value) => _.template(value)(variables)
12 changes: 7 additions & 5 deletions lib/request/compile-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

const _ = require('lodash')

module.exports = (url, variables) => {
return url.includes('<%=') && url.includes('%>')
? _.template(url)(variables)
: url
}
// module.exports = (url, variables) => {
// return url.includes('<%=') && url.includes('%>')
// ? _.template(url)(variables)
// : url
// }

module.exports = (url, variables) => _.template(url)(variables)
2 changes: 1 addition & 1 deletion test/contracts/consumer-valid-passing-before-hook.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"before": [{
"request": {
"baseUrl": "http://localhost:8379",
"path": "/api/user/<%= id %>",
"path": "/api/user",
"method": "POST",
"headers": {
"Content-Type": "application/json"
Expand Down
42 changes: 42 additions & 0 deletions test/contracts/consumer-valid-passing-before-hooks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"consumer": {
"provider_one": {
"user_api": {
"OK": {
"before": [{
"request": {
"baseUrl": "http://localhost:8379",
"path": "/api/user",
"method": "POST",
"headers": {
"Content-Type": "application/json"
},
"body": {
"email": "<%= hyperid %>@test.com"
}
},
"variables": {
"id": "$.body.id"
}
}, {
"request": {
"baseUrl": "http://localhost:8379",
"path": "/api/user/<%= id %>/delete"
}
}],
"request": {
"baseUrl": "http://localhost:8379",
"path": "/api/user/<%= id %>/restore"
},
"response": {
"statusCode": 200,
"body": {
"id": "Joi.number().integer()",
"name": "Joi.string()"
}
}
}
}
}
}
}
20 changes: 18 additions & 2 deletions test/helpers/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,26 @@ Provider.prototype.start = function (options, done) {
})

app.get('/api/user/:id', (req, res) => {
if (req.params.id !== 1) {
if (req.params.id === '1') {
res.status(200).send({ id: 1, name: 'John Doe' })
} else {
res.status(404).send('Not Found')
}
})

app.get('/api/user/:id/delete', (req, res) => {
if (req.params.id === '1') {
res.status(200).end()
} else {
res.status(404).send('Not Found')
}
})

app.get('/api/user/:id/restore', (req, res) => {
if (req.params.id === '1') {
res.status(200).send({ id: 1, name: 'John Doe' })
} else {
res.status(201).send({ id: 1, name: 'John Doe' })
res.status(404).send('Not Found')
}
})

Expand Down
56 changes: 0 additions & 56 deletions test/integration/api/consumer-endpoint-after-hook.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,60 +76,4 @@ describe.skip('Consumer Endpoint (POST /api/contracts) Integration Test', functi

after(jackal.stop)
})

context('with valid, failing contracts', function () {
let port, dbPath, options

before(function (done) {
port = 8378
dbPath = 'test/integration/api/consumer.json'
options = {
port: port,
quiet: true,
db: { path: dbPath }
}

jackal.start(options, done)
})

it('should return a list of contract results for the consumer suite', function (done) {
const buf = fs.readFileSync('test/contracts/consumer-valid-failing.json')

const req = {
url: `http://localhost:${port}/api/contracts`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: buf
}

const expected = {
message: 'Failures Exist',
status: 'FAILED',
results: [
{ name: 'provider_one/user_api/OK', consumer: 'consumer', status: 'Pass', error: null },
{ name: 'provider_one/receipt_api/OK', consumer: 'consumer', status: 'Pass', error: null },
{ name: 'provider_two/product_api/OK', consumer: 'consumer', status: 'Fail', error: 'Contract failed: "description" must be a number\nresponse.statusCode: 200\nresponse.body: [{"id":1,"name":"Crutch","description":"Walking Aid"},{"id":2,"name":"Jackal","description":"Wild Animal"}]' }
]
}

request(req, (err, res, body) => {
expect(err).to.not.exist
expect(res.statusCode).to.equal(200)
expect(JSON.parse(body)).to.eql(expected)
done()
})
})

after(function (done) {
fs.stat(dbPath, (err, stats) => {
if (stats) { fs.unlink(dbPath, done) }
else { done() }
})
})

after(jackal.stop)
})
})
56 changes: 0 additions & 56 deletions test/integration/api/consumer-endpoint-after-hooks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,60 +76,4 @@ describe.skip('Consumer Endpoint (POST /api/contracts) Integration Test', functi

after(jackal.stop)
})

context('with valid, failing contracts', function () {
let port, dbPath, options

before(function (done) {
port = 8378
dbPath = 'test/integration/api/consumer.json'
options = {
port: port,
quiet: true,
db: { path: dbPath }
}

jackal.start(options, done)
})

it('should return a list of contract results for the consumer suite', function (done) {
const buf = fs.readFileSync('test/contracts/consumer-valid-failing.json')

const req = {
url: `http://localhost:${port}/api/contracts`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: buf
}

const expected = {
message: 'Failures Exist',
status: 'FAILED',
results: [
{ name: 'provider_one/user_api/OK', consumer: 'consumer', status: 'Pass', error: null },
{ name: 'provider_one/receipt_api/OK', consumer: 'consumer', status: 'Pass', error: null },
{ name: 'provider_two/product_api/OK', consumer: 'consumer', status: 'Fail', error: 'Contract failed: "description" must be a number\nresponse.statusCode: 200\nresponse.body: [{"id":1,"name":"Crutch","description":"Walking Aid"},{"id":2,"name":"Jackal","description":"Wild Animal"}]' }
]
}

request(req, (err, res, body) => {
expect(err).to.not.exist
expect(res.statusCode).to.equal(200)
expect(JSON.parse(body)).to.eql(expected)
done()
})
})

after(function (done) {
fs.stat(dbPath, (err, stats) => {
if (stats) { fs.unlink(dbPath, done) }
else { done() }
})
})

after(jackal.stop)
})
})
4 changes: 2 additions & 2 deletions test/integration/api/consumer-endpoint-before-hook.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const request = require('request')
const jackal = require('../../helpers/jackal')
const Provider = require('../../helpers/provider')

describe.only('Consumer Endpoint (POST /api/contracts) Integration Test', function () {
describe('Consumer Endpoint (POST /api/contracts) Integration Test', function () {
let providerOne

before(function (done) {
Expand Down Expand Up @@ -53,7 +53,7 @@ describe.only('Consumer Endpoint (POST /api/contracts) Integration Test', functi

request(req, (err, res, body) => {
expect(err).to.not.exist
expect(res.statusCode).to.equal(200)
expect(res.statusCode).to.equal(201)
expect(JSON.parse(body)).to.eql(expected)
done()
})
Expand Down
64 changes: 3 additions & 61 deletions test/integration/api/consumer-endpoint-before-hooks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const request = require('request')
const jackal = require('../../helpers/jackal')
const Provider = require('../../helpers/provider')

describe.skip('Consumer Endpoint (POST /api/contracts) Integration Test', function () {
describe('Consumer Endpoint (POST /api/contracts) Integration Test', function () {
let providerOne, providerTwo

before(function (done) {
Expand Down Expand Up @@ -37,7 +37,7 @@ describe.skip('Consumer Endpoint (POST /api/contracts) Integration Test', functi
})

it('should return a list of contract results for the consumer suite', function (done) {
const buf = fs.readFileSync('test/contracts/consumer-valid-passing.json')
const buf = fs.readFileSync('test/contracts/consumer-valid-passing-before-hooks.json')

const req = {
url: `http://localhost:${port}/api/contracts`,
Expand All @@ -53,9 +53,7 @@ describe.skip('Consumer Endpoint (POST /api/contracts) Integration Test', functi
message: 'All Passed',
status: 'PASSED',
results: [
{ name: 'provider_one/user_api/OK', consumer: 'consumer', status: 'Pass', error: null },
{ name: 'provider_one/receipt_api/OK', consumer: 'consumer', status: 'Pass', error: null },
{ name: 'provider_two/product_api/OK', consumer: 'consumer', status: 'Pass', error: null }
{ name: 'provider_one/user_api/OK', consumer: 'consumer', status: 'Pass', error: null }
]
}

Expand All @@ -76,60 +74,4 @@ describe.skip('Consumer Endpoint (POST /api/contracts) Integration Test', functi

after(jackal.stop)
})

context('with valid, failing contracts', function () {
let port, dbPath, options

before(function (done) {
port = 8378
dbPath = 'test/integration/api/consumer.json'
options = {
port: port,
quiet: true,
db: { path: dbPath }
}

jackal.start(options, done)
})

it('should return a list of contract results for the consumer suite', function (done) {
const buf = fs.readFileSync('test/contracts/consumer-valid-failing.json')

const req = {
url: `http://localhost:${port}/api/contracts`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: buf
}

const expected = {
message: 'Failures Exist',
status: 'FAILED',
results: [
{ name: 'provider_one/user_api/OK', consumer: 'consumer', status: 'Pass', error: null },
{ name: 'provider_one/receipt_api/OK', consumer: 'consumer', status: 'Pass', error: null },
{ name: 'provider_two/product_api/OK', consumer: 'consumer', status: 'Fail', error: 'Contract failed: "description" must be a number\nresponse.statusCode: 200\nresponse.body: [{"id":1,"name":"Crutch","description":"Walking Aid"},{"id":2,"name":"Jackal","description":"Wild Animal"}]' }
]
}

request(req, (err, res, body) => {
expect(err).to.not.exist
expect(res.statusCode).to.equal(200)
expect(JSON.parse(body)).to.eql(expected)
done()
})
})

after(function (done) {
fs.stat(dbPath, (err, stats) => {
if (stats) { fs.unlink(dbPath, done) }
else { done() }
})
})

after(jackal.stop)
})
})

0 comments on commit bf533bb

Please sign in to comment.