stubborn-ws
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"parserOptions": { | ||
"ecmaVersion": 6, | ||
"ecmaVersion": 2017, | ||
"ecmaFeatures": { | ||
@@ -11,4 +11,3 @@ "arrowFunctions": true, | ||
"templateStrings": true, | ||
"forOf": true, | ||
"experimentalObjectRestSpread": true | ||
"forOf": true | ||
} | ||
@@ -15,0 +14,0 @@ }, |
{ | ||
"name": "stubborn-ws", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "", | ||
"homepage": "https://github.com/ybonnefond/stubborn#stubborn", | ||
"main": "index.js", | ||
@@ -9,4 +10,6 @@ "keywords": "stubborn, test, test api, testing, testing web server, nock, dyson, jest, jasmine, mocha", | ||
"test": "jest", | ||
"test-ci": "yarn lint && jest --coverage --coverageReporters=text-lcov | coveralls", | ||
"lint": "eslint src" | ||
"test-ci": "mkdir -p coverage && yarn lint && jest --coverage --coverageReporters=lcov && cat ./coverage/lcov.info | coveralls", | ||
"lint": "eslint src", | ||
"doc": "jsdoc2md -f src/*.js > doc/API.md", | ||
"release": "yarn doc && yarn publish" | ||
}, | ||
@@ -20,3 +23,4 @@ "author": "", | ||
"jest": "^23.3.0", | ||
"jest-diff": "^23.2.0" | ||
"jest-diff": "^23.2.0", | ||
"jsdoc-to-markdown": "^4.0.1" | ||
}, | ||
@@ -23,0 +27,0 @@ "jest": { |
@@ -9,5 +9,6 @@ # Stubborn | ||
- [Installation](#installation) | ||
* [Node version support](#node-version-support) | ||
- [Usage](#usage) | ||
* [Basic Usage](#basic-usage) | ||
- [API](#api) | ||
- [Contributing](#contributing) | ||
* [Release](#release) | ||
@@ -40,4 +41,3 @@ <hr /> | ||
### Basic Usage | ||
```Javascript | ||
```javascript | ||
describe('Test', () => { | ||
@@ -125,1 +125,20 @@ const got = require('got'); | ||
## API | ||
See the [API documentation](doc/API.md) | ||
## Contributing | ||
### Release | ||
```bash | ||
git checkout master | ||
git pull --rebase | ||
yarn doc | ||
git add . | ||
git commit -m 'doc(): Update documentation' | ||
yarn publish --<major|minor|patch> | ||
git push --follow-tags | ||
``` | ||
Then go to github to [draft a new release](https://github.com/ybonnefond/stubborn/releases/new) |
'use strict'; | ||
module.exports = { | ||
Stubborn: require('./HttpServer') | ||
Stubborn: require('./Stubborn') | ||
}; |
162
src/Route.js
@@ -6,2 +6,5 @@ 'use strict'; | ||
class Route { | ||
/** | ||
* @class Route | ||
*/ | ||
constructor(method, path) { | ||
@@ -34,2 +37,7 @@ this.method = method; | ||
/** | ||
* Return the route definition, helpful to find why the route didn't match the request | ||
* | ||
* @returns {RouteDefinition} | ||
*/ | ||
getDefinition() { | ||
@@ -45,2 +53,26 @@ return { | ||
/** | ||
* Set the headers definition. | ||
* | ||
* @example | ||
* ```js | ||
* // Bypass all headers | ||
* stubborn | ||
* .get('/bypass') | ||
* .setHeaders(null); | ||
* | ||
* stubborn | ||
* .get('/match') | ||
* .setHeaders({ | ||
* // Match using `===` | ||
* exact: 'match', | ||
* // Match using `/^match/.test(value)` | ||
* regexp: /^match/, | ||
* // Match using `func(value);` | ||
* func: (value) => 'match' === value | ||
* }); | ||
* ``` | ||
* @param {HeadersDefinition|null} headers Headers definition | ||
* @returns {this} | ||
*/ | ||
setHeaders(headers) { | ||
@@ -52,2 +84,26 @@ this.headers = null === headers ? headers : Object.assign({}, this.headers, headers); | ||
/** | ||
* Set the headers definition. | ||
* | ||
* @example | ||
* ```js | ||
* // Bypass all query parameters | ||
* stubborn | ||
* .get('/bypass') | ||
* .setQueryParameters(null); | ||
* | ||
* stubborn | ||
* .get('/match') | ||
* .setQueryParameters({ | ||
* // Match using `===` | ||
* exact: 'match', | ||
* // Match using `/^match/.test(value)` | ||
* regexp: /^match/, | ||
* // Match using `func(value);` | ||
* func: (value) => 'match' === value | ||
* }); | ||
* ``` | ||
* @param {QueryParametersDefinition|null} query Query parameters definition | ||
* @returns {this} | ||
*/ | ||
setQueryParameters(query) { | ||
@@ -59,2 +115,32 @@ this.query = null === query ? query : Object.assign({}, this.query, query); | ||
/** | ||
* Set the body definition. Body can be a string or a complex object mixing | ||
* | ||
* @example | ||
* ```js | ||
* // Bypass all query parameters | ||
* stubborn | ||
* .get('/bypass') | ||
* .setBody(null); | ||
* | ||
* stubborn | ||
* .get('/simple') | ||
* .setBody('text body'); | ||
* | ||
* stubborn | ||
* .get('/object') | ||
* .setBody({ | ||
* // Match using `===` | ||
* exact: 'match', | ||
* // Match using `/^match/.test(value)` | ||
* regexp: /^match/, | ||
* // Match using `func(value);` | ||
* func: (value) => 'match' === value, | ||
* object: { reg: /\d+/, bypass: null }, | ||
* arr: [{ item: '1' }, { item: /2/ }] | ||
* }); | ||
* ``` | ||
* @param {BodyDefinition} body Body definition | ||
* @returns {this} | ||
*/ | ||
setBody(body) { | ||
@@ -66,2 +152,7 @@ this.body = body; | ||
/** | ||
* Set the response status code | ||
* @param {Number} statusCode HTTP status code | ||
* @returns {this} | ||
*/ | ||
setResponseStatusCode(statusCode) { | ||
@@ -73,2 +164,18 @@ this.response.statusCode = statusCode; | ||
/** | ||
* Set the response body | ||
* | ||
* If response is an Object, values can be a function which will receive the request as parameter | ||
* | ||
* @example | ||
* ```js | ||
* ws.get('/resource') | ||
* .setResponseBody({ | ||
* key: 'value', | ||
* funKey: (req) => `${req.query.param}-param` | ||
* }); | ||
* ``` | ||
* @param {string|Object.<string, mixed>} body | ||
* @returns {this} | ||
*/ | ||
setResponseBody(body) { | ||
@@ -80,2 +187,15 @@ this.response.body = body; | ||
/** | ||
* Set the response header | ||
* key/value object. If value is a function it will receive the request as parameter | ||
* ```js | ||
* ws.get('/resource') | ||
* .setResponseHeaders({ | ||
* 'Content-type': 'application/json', | ||
* 'Custom-Header': (req) => `${req.header.custom}-response` | ||
* }); | ||
* ``` | ||
* @param {Object.<mixed>} body | ||
* @returns {this} | ||
*/ | ||
setResponseHeaders(headers) { | ||
@@ -89,1 +209,43 @@ this.response.headers = headers; | ||
module.exports = Route; | ||
/** | ||
* @typedef {Object} RouteDefinition | ||
* @property {string} method The route method, one of [GET, POST, PATCH, PUT, DELETE] | ||
* @property {string} path The stubbed resource path (ex: /users) | ||
* @property {HeadersDefinition} headers | ||
* @property {QueryParametersDefinition} query | ||
* @property {BodyDefinition} body | ||
*/ | ||
/** | ||
* key/value object. Key is the header name, value is the header definition | ||
* The header definition can be a `string`, a `Regexp`, a `function` or `null` | ||
* - `string`: Match using string equality against the request header value | ||
* - `Regexp`: Match using Regexp.test against the request header value | ||
* - `function`: Request header value will be passed to the function. The function Should return a boolean | ||
* - `null`: act as a wildcard, header will not be processed | ||
* @typedef {object.<string, string|Regexp|function|null>} HeadersDefinition | ||
*/ | ||
/** | ||
* key/value object. Key is the query parameter name, value is the parameter definition | ||
* The parameter definition can be a `string`, a `Regexp`, a `function` or `null` | ||
* - `string`: Match using string equality against the request parameter value | ||
* - `Regexp`: Match using Regexp.test against the request parameter value | ||
* - `function`: Request parameter value will be passed to the function. The function Should return a boolean | ||
* - `null`: act as a wildcard, parameter will not be processed | ||
* | ||
* @typedef {Object.<string, string|Regexp|function|null>} QueryParametersDefinition | ||
*/ | ||
/** | ||
* Body can be a string, an array or a key/mixed object where values can be a `string`, a `Regexp`, a `function`, object, Array or `null` | ||
* - `string`: Match using string equality against the request parameter value | ||
* - `Regexp`: Match using Regexp.test against the request parameter value | ||
* - `function`: Request parameter value will be passed to the function. The function Should return a boolean | ||
* - `Object`: Object will be recursively processed. | ||
* - `Array`: Each array items will be recursively processed | ||
* - `null`: act as a wildcard, parameter will not be processed | ||
* | ||
* @typedef {string|Array|Object.<string, string|Regexp|Body|function|null>} BodyDefinition | ||
*/ |
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
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
71699
30
1495
1
142
6