Skip to content

Commit

Permalink
feat(karma): relax consumer/provider requirement in MockService
Browse files Browse the repository at this point in the history
- For pact-web use cases, where mock service is started
  independently of the test cases themselves, we allow
  consumer and provider to be specified in underlying
  mock service directly.
- Logs warning to console if details aren't provided to
  assist in debugging issues
- Addresses pact-foundation/karma-pact/pull/4
- Addresses #59
  • Loading branch information
mefellows committed Sep 18, 2017
1 parent 1e8349b commit e9f3a4a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 38 deletions.
5 changes: 5 additions & 0 deletions src/common/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ module.exports = {
if (config.logging) {
console.log(msg)
}
},
warn: (msg) => {
if (config.logging) {
console.warn(msg)
}
}
}
4 changes: 2 additions & 2 deletions src/dsl/mockService.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export type PactfileWriteMode = "overwrite" | "update" | "none";

export class MockService {
constructor(
consumer: string,
provider: string,
consumer?: string,
provider?: string,
port?: number,
host?: string,
ssl?: boolean,
Expand Down
10 changes: 6 additions & 4 deletions src/dsl/mockService.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

const isNil = require('lodash.isnil')
const Request = require('../common/request')
const logger = require('../common/logger')

module.exports = class MockService {

Expand All @@ -22,7 +23,8 @@ module.exports = class MockService {
*/
constructor (consumer, provider, port, host, ssl, pactfileWriteMode) {
if (isNil(consumer) || isNil(provider)) {
throw new Error('Please provide the names of the provider and consumer for this Pact.')
logger.warn('Warning: Consumer\Provider details not provided, ensure ' +
'that the mock service has been started with this information')
}

port = port || 1234
Expand All @@ -33,9 +35,9 @@ module.exports = class MockService {
this._request = new Request()
this._baseURL = `${ssl ? 'https' : 'http'}://${host}:${port}`
this._pactDetails = {
consumer: { name: consumer },
provider: { name: provider },
pactfile_write_mode: pactfileWriteMode
pactfile_write_mode: pactfileWriteMode,
consumer: (consumer) ? { name: consumer } : undefined,
provider: (provider) ? { name: provider } : undefined
}
}

Expand Down
15 changes: 5 additions & 10 deletions src/pact-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,16 @@ var Interaction = require('./dsl/interaction')
module.exports = (opts) => {
var consumer = opts.consumer
var provider = opts.provider

if (isNil(consumer)) {
throw new Error('You must provide a Consumer for this pact.')
}

if (isNil(provider)) {
throw new Error('You must provide a Provider for this pact.')
}

var port = opts.port || 1234
var host = opts.host || '127.0.0.1'
var ssl = opts.ssl || false
var pactfileWriteMode = opts.pactfileWriteMode || 'overwrite'

logger.info(`Setting up Pact with Consumer "${consumer}" and Provider "${provider}" using mock service on Port: "${port}"`)
if (isNil(consumer) || isNil(provider)) {
logger.info(`Setting up Pact using mock service on port: "${port}"`)
} else {
logger.info(`Setting up Pact with Consumer "${consumer}" and Provider "${provider}" using mock service on port: "${port}"`)
}

const mockService = new MockService(consumer, provider, port, host, ssl, pactfileWriteMode)

Expand Down
7 changes: 7 additions & 0 deletions test/common/logger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,35 @@ var proxyquire = require('proxyquire')

describe('Logger#info', () => {
const consoleLogSpy = sinon.spy(console, 'log')
const consoleWarnSpy = sinon.spy(console, 'warn')

context('with logging configuration turned on', () => {
beforeEach(() => {
consoleLogSpy.reset()
consoleWarnSpy.reset()
var logger = proxyquire('../../src/common/logger', { './config': { logging: true } })
logger.info('this will be logged')
logger.warn('this will be logged at warn')
})

it('logs a message', () => {
expect(consoleLogSpy).to.have.been.calledWith('this will be logged')
expect(consoleWarnSpy).to.have.been.calledWith('this will be logged at warn')
})
})

context('with logging configuration turned off', () => {
beforeEach(() => {
consoleLogSpy.reset()
consoleWarnSpy.reset()
var logger = proxyquire('../../src/common/logger', { './config': { logging: false } })
logger.info('this will be ignored')
logger.warn('this will be logged at warn')
})

it('ignores a message to be logged', function () {
expect(consoleLogSpy).to.not.have.been.called
expect(consoleWarnSpy).to.not.have.been.called
})
})
})
64 changes: 42 additions & 22 deletions test/dsl/mockService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ describe('MockService', () => {

it('does not create a MockService when consumer is not informed', () => {
expect(() => { new MockService() })
.to.throw(Error, 'Please provide the names of the provider and consumer for this Pact.')
.not.to.throw(Error)
})

it('does not create a MockService when provider is not informed', () => {
expect(() => { new MockService('consumer') })
.to.throw(Error, 'Please provide the names of the provider and consumer for this Pact.')
.not.to.throw(Error)
})
})

Expand All @@ -48,13 +48,12 @@ describe('MockService', () => {

it('when Interaction added successfully', (done) => {
nock(mock._baseURL).post(/interactions$/).reply(200)
expect(mock.addInteraction(interaction)).to.eventually.notify(done)
expect(mock.addInteraction(interaction)).to.eventually.be.fulfilled.notify(done)
})

it('when Interaction fails to be added', (done) => {
nock(mock._baseURL).post(/interactions$/).reply(500)
expect(mock.addInteraction(interaction)).to.eventually.be.rejected
done()
expect(mock.addInteraction(interaction)).to.eventually.be.rejected.notify(done)
})
})

Expand All @@ -63,13 +62,12 @@ describe('MockService', () => {

it('when interactions are removed successfully', (done) => {
nock(mock._baseURL).delete(/interactions$/).reply(200)
expect(mock.removeInteractions()).to.eventually.notify(done)
expect(mock.removeInteractions()).to.eventually.be.fulfilled.notify(done)
})

it('when interactions fail to be removed', (done) => {
nock(mock._baseURL).delete(/interactions$/).reply(500)
expect(mock.removeInteractions()).to.eventually.be.rejected
done()
expect(mock.removeInteractions()).to.eventually.be.rejected.notify(done)
})
})

Expand All @@ -78,29 +76,51 @@ describe('MockService', () => {

it('when verification is successful', (done) => {
nock(mock._baseURL).get(/interactions\/verification$/).reply(200)
expect(mock.verify()).to.eventually.notify(done)
expect(mock.verify()).to.eventually.be.fulfilled.notify(done)
})

it('when verification fails', (done) => {
nock(mock._baseURL).get(/interactions\/verification$/).reply(500)
expect(mock.verify()).to.eventually.be.rejected
done()
expect(mock.verify()).to.eventually.be.rejected.notify(done)
})
})

describe('#writePact', () => {
const mock = new MockService('consumer', 'provider', 1234)

it('when writing is successful', (done) => {
nock(mock._baseURL).post(/pact$/).reply(200)
expect(mock.writePact()).to.eventually.notify(done)
describe('when consumer and provider details provided', () => {
const mock = new MockService('aconsumer', 'aprovider', 1234)

describe('and writing is successful', () => {
it('should write the consumer and provider details into the pact', (done) => {
nock(mock._baseURL)
.post(/pact$/, {
pactfile_write_mode: 'overwrite',
consumer: { name: 'aconsumer' },
provider: { name: 'aprovider' }
})
.reply(200)
expect(mock.writePact()).to.eventually.be.fulfilled.notify(done)
})
})

describe('and writing fails', () => {
it('should return a rejected promise', (done) => {
nock(mock._baseURL)
.post(/pact$/).reply(500)
expect(mock.writePact()).to.eventually.be.rejected.notify(done)
})
})
})

it('when writing fails', (done) => {
nock(mock._baseURL).post(/pact$/).reply(500)
expect(mock.writePact()).to.eventually.be.rejected
done()
describe('when consumer and provider details are not provided', () => {
const mock = new MockService(null, null, 1234)
it('should not write the consumer and provider details into the pact', (done) => {
nock(mock._baseURL)
.post(/pact$/, {
pactfile_write_mode: 'overwrite',
consumer: undefined,
provider: undefined
}).reply(200)
expect(mock.writePact()).to.eventually.be.fulfilled.notify(done)
})
})
})

})

0 comments on commit e9f3a4a

Please sign in to comment.