consumer-contracts
Advanced tools
Comparing version 1.0.0 to 1.1.0
module.exports.Contract = require('./lib/contract'); | ||
module.exports.Joi = require('joi'); | ||
module.exports.Joi = require('joi'); |
@@ -6,8 +6,2 @@ var Joi = require('joi'); | ||
var package = require('../package'); | ||
var request = require('request').defaults({ | ||
json: true, | ||
headers: { | ||
'user-agent': 'consumer-contracts/' + package.version | ||
} | ||
}); | ||
@@ -41,2 +35,8 @@ var requiredOptions = [ | ||
this.response = options.response; | ||
this.client = (options.client || require('request')).defaults({ | ||
json: true, | ||
headers: { | ||
'user-agent': 'consumer-contracts/' + package.version | ||
} | ||
}); | ||
} | ||
@@ -47,3 +47,3 @@ | ||
request(this.request, function (err, res) { | ||
this.client(this.request, function (err, res) { | ||
if (err) return cb(new Error(util.format('Request failed: %s', err.message))); | ||
@@ -68,2 +68,2 @@ | ||
module.exports = Contract; | ||
module.exports = Contract; |
{ | ||
"name": "consumer-contracts", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Consumer driven contracts for Node.js", | ||
@@ -36,3 +36,4 @@ "main": "index.js", | ||
"recursive-readdir": "^1.2.1", | ||
"request": "^2.55.0" | ||
"request": "^2.55.0", | ||
"sinon": "^1.14.1" | ||
}, | ||
@@ -58,2 +59,2 @@ "devDependencies": { | ||
} | ||
} | ||
} |
# consumer-contracts | ||
[![Build Status](https://travis-ci.org/robinjmurphy/consumer-contracts.svg)](https://travis-ci.org/robinjmurphy/consumer-contracts) [![Code Climate](https://codeclimate.com/github/robinjmurphy/consumer-contracts/badges/gpa.svg)](https://codeclimate.com/github/robinjmurphy/consumer-contracts) | ||
> Consumer-driven contracts in JavaScript | ||
@@ -75,3 +77,3 @@ | ||
``` | ||
@@ -101,3 +103,3 @@ | ||
The `consumer` property appears in the output of the `consumer-contracts` tool and should be the name of the consuming service that the contract applies to. | ||
The `consumer` property appears in the output of the `consumer-contracts` tool and should be the name of the consuming service that the contract applies to. | ||
@@ -137,3 +139,3 @@ ### `name` | ||
### Validating the response code | ||
#### Validating the response code | ||
@@ -156,3 +158,3 @@ To require a specific HTTP status code, set the `statusCode` property to that value: | ||
### Validating the response headers | ||
#### Validating the response headers | ||
@@ -170,3 +172,3 @@ The response headers can be validated using a Joi schema: | ||
### Validating the response body | ||
#### Validating the response body | ||
@@ -186,2 +188,28 @@ The response body can be validated using a Joi schema: | ||
### `client` _optional_ | ||
You can use a pre-configured [request](https://github.com/request/request) client for your contracts using the `client` property. This can be useful when you have a set of common request options across contracts. | ||
```js | ||
var Contract = require('consumer-contracts').Contract; | ||
var Joi = require('consumer-contracts').Joi; | ||
var client = require('request').defaults({ | ||
headers: { | ||
authorization: 'Bearer xxx' | ||
} | ||
}); | ||
module.exports = new Contract({ | ||
name: 'Contract name', | ||
consumer: 'Consumer name', | ||
request: { | ||
// ... | ||
}, | ||
response: { | ||
// ... | ||
}, | ||
client: client | ||
}); | ||
``` | ||
## CLI | ||
@@ -188,0 +216,0 @@ |
@@ -5,2 +5,3 @@ var Contract = require('../lib/contract'); | ||
var nock = require('nock'); | ||
var sinon = require('sinon'); | ||
@@ -131,3 +132,59 @@ describe('Contract', function () { | ||
}); | ||
it('sets a user-agent header', function (done) { | ||
nock('http://api.example.com', { | ||
reqheaders: { | ||
'user-agent': 'consumer-contracts/' + require('../package').version | ||
} | ||
}).get('/').reply(200); | ||
var contract = new Contract({ | ||
name: 'Name', | ||
consumer: 'Consumer', | ||
request: { | ||
url: 'http://api.example.com/' | ||
}, | ||
response: { | ||
statusCode: 200 | ||
} | ||
}); | ||
contract.validate(function (err) { | ||
assert.ifError(err); | ||
done(); | ||
}); | ||
}); | ||
it('supports passing a custom request client', function (done) { | ||
nock('http://api.example.com', { | ||
reqheaders: { | ||
authorization: 'Bearer xxx', | ||
'user-agent': 'consumer-contracts/' + require('../package').version | ||
} | ||
}).get('/').reply(200); | ||
var client = require('request').defaults({ | ||
headers: { | ||
authorization: 'Bearer xxx' | ||
} | ||
}); | ||
var contract = new Contract({ | ||
name: 'Name', | ||
consumer: 'Consumer', | ||
request: { | ||
url: 'http://api.example.com/' | ||
}, | ||
response: { | ||
statusCode: 200 | ||
}, | ||
client: client | ||
}); | ||
contract.validate(function (err) { | ||
assert.ifError(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17336
11
318
226
9
+ Addedsinon@^1.14.1
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedcall-bind@1.0.8(transitive)
+ Addedcall-bind-apply-helpers@1.0.1(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.0.0(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedformatio@1.1.1(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.6(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-arguments@1.2.0(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-generator-function@1.0.10(transitive)
+ Addedis-typed-array@1.1.15(transitive)
+ Addedlolex@1.3.2(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedsamsam@1.1.2(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedsinon@1.17.7(transitive)
+ Addedutil@0.12.5(transitive)
+ Addedwhich-typed-array@1.1.18(transitive)