Skip to content

Commit

Permalink
Improve readme and update package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
oziks committed Apr 30, 2015
1 parent dc6bf18 commit 637ddbd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 73 deletions.
103 changes: 31 additions & 72 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,42 @@ First, you have to define the URLs to mock in a configuration file:
// ./superagent-mock-config.js file
module.exports = [
{
// regular expression of URL
/**
* regular expression of URL
*/
pattern: 'https://domain.example/(\\w+)/',

// callback that returns the data
fixtures: function () {
/**
* returns the data
*
* @param match array Result of the resolution of the regular expression
*/
fixtures: function (match) {
/**
* example:
* request.get('https://error.example/404').end(function(err, res){
* console.log(err); // 404
* })
*/
if (match[1] === '404') {
throw new Error(404);
}

/**
* example:
* request.get('https://error.example/200').end(function(err, res){
* console.log(res.body); // "Data fixtures"
* })
*/
return 'Data fixtures';
},

// `match`: result of the resolution of the regular expression
// `data`: data returns by `fixtures` attribute
/**
* returns the result of the request
*
* @param match array Result of the resolution of the regular expression
* @param data mixed Data returns by `fixtures` attribute
*/
callback: function (match, data) {
return {
body: data
Expand All @@ -53,73 +79,6 @@ var config = require('./superagent-mock-config');
require('superagent-mock')(request, config);
```

Also, You can pass matching data of the pattern attributes.

```js
// ./superagent-mock-config.js file
module.exports = [
{
// regular expression of URL
pattern: 'https://domain.example/(\\w+)/',

// passes the matching data of finding 'https://domain.example/' followed by word characters.
fixtures: function (match) {
// example:
// request.get('https://domain.example/foo').end(function(err, res){
// console.log(res.body); // => 'foo'
// })
//
return match[1];
},

// `match`: result of the resolution of the regular expression
// `data`: data returns by `fixtures` attribute
callback: function (match, data) {
return {
body: data
};
}
},
...
];
```

If catches errors in fixtures function, the thrown object assigns first argument of the callback of the request.

```js
// ./superagent-mock-config.js file
module.exports = [
{
// regular expression of URL
pattern: 'https://error.example/(\\w+)/',

// passes the matching data of finding 'https://domain.example/' followed by word characters.
fixtures: function (match) {
// example:
// request.get('https://error.example/404').end(function(err, res){
// // err is newErr;
// })
//

var code = (match || [])[1] || 404;
var newErr = new Error(http.STATUS_CODES[code]);
newErr.response = http.STATUS_CODES[code];
newErr.status = code;
throw newErr;
},

// `match`: result of the resolution of the regular expression
// `data`: data returns by `fixtures` attribute
callback: function (match, data) {
return {
body: data
};
}
},
...
];
```

## Tests

To run units tests: `npm test`.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "superagent-mock",
"description": "superagent plugin allowing to simulate HTTP calls by returning data fixtures based on the requested URL.",
"version": "1.0.0",
"version": "1.1.0",
"keywords": [
"superagent",
"mock",
Expand Down

0 comments on commit 637ddbd

Please sign in to comment.