Skip to content

Commit

Permalink
Merge pull request #143 from yahoo/testing
Browse files Browse the repository at this point in the history
Refactor Fetchr Tests
  • Loading branch information
Vijar committed May 5, 2016
2 parents 45036dd + 6664cad commit d972790
Show file tree
Hide file tree
Showing 15 changed files with 586 additions and 680 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ module.exports = {
};
```

And in your service call:

```js
fetcher
.read('someData')
.params({id: ###})
.end(function (err, data, meta) {
if (err) {
// err.output will be { message: "Not found", more: "meta data" }
}
});
```

## XHR Timeouts

`xhrTimeout` is an optional config property that allows you to set timeout (in ms) for all clientside requests, defaults to `3000`.
Expand Down
5 changes: 5 additions & 0 deletions libs/fetcher.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ Request.prototype.end = function (callback) {
self.options._serviceMeta.push(result.meta);
}
return result;
}, function(errData) {
if (errData.meta) {
self.options._serviceMeta.push(errData.meta);
}
throw errData;
});

if (callback) {
Expand Down
21 changes: 14 additions & 7 deletions libs/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function executeRequest (request, resolve, reject) {
function Fetcher (options) {
this.options = options || {};
this.req = this.options.req || {};
this.serviceMeta = [];
this._serviceMeta = [];

}

Expand Down Expand Up @@ -335,7 +335,14 @@ Fetcher.middleware = function (options) {
}
if (err) {
var errResponse = getErrorResponse(err);
res.status(errResponse.statusCode).json(responseFormatter(req, res, errResponse.output));
if (req.query && req.query.returnMeta) {
res.status(errResponse.statusCode).json(responseFormatter(req, res, {
output: errResponse.output,
meta: meta
}));
} else {
res.status(errResponse.statusCode).json(responseFormatter(req, res, errResponse.output));
}
return;
}
if (req.query.returnMeta) {
Expand Down Expand Up @@ -418,7 +425,7 @@ Fetcher.middleware = function (options) {
Fetcher.prototype.read = function (resource, params, config, callback) {
var request = new Request('read', resource, {
req: this.req,
serviceMeta: this.serviceMeta
serviceMeta: this._serviceMeta
});
if (1 === arguments.length) {
return request;
Expand Down Expand Up @@ -453,7 +460,7 @@ Fetcher.prototype.read = function (resource, params, config, callback) {
Fetcher.prototype.create = function (resource, params, body, config, callback) {
var request = new Request('create', resource, {
req: this.req,
serviceMeta: this.serviceMeta
serviceMeta: this._serviceMeta
});
if (1 === arguments.length) {
return request;
Expand Down Expand Up @@ -489,7 +496,7 @@ Fetcher.prototype.create = function (resource, params, body, config, callback) {
Fetcher.prototype.update = function (resource, params, body, config, callback) {
var request = new Request('update', resource, {
req: this.req,
serviceMeta: this.serviceMeta
serviceMeta: this._serviceMeta
});
if (1 === arguments.length) {
return request;
Expand Down Expand Up @@ -524,7 +531,7 @@ Fetcher.prototype.update = function (resource, params, body, config, callback) {
Fetcher.prototype['delete'] = function (resource, params, config, callback) {
var request = new Request('delete', resource, {
req: this.req,
serviceMeta: this.serviceMeta
serviceMeta: this._serviceMeta
});
if (1 === arguments.length) {
return request;
Expand Down Expand Up @@ -563,7 +570,7 @@ Fetcher.prototype.updateOptions = function (options) {
* Get all the aggregated metadata sent data services in this request
*/
Fetcher.prototype.getServiceMeta = function () {
return this.serviceMeta;
return this._serviceMeta;
};

module.exports = Fetcher;
Expand Down
4 changes: 4 additions & 0 deletions libs/util/http.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ function io(url, options) {
err = new Error(errMessage);
err.statusCode = status;
err.body = errBody || body;
if (err.body) {
err.output = err.body.output;
err.meta = err.body.meta;
}
if (408 === status || 0 === status) {
err.timeout = options.timeout;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"pre-commit": "^1.0.0",
"qs": "^4.0.0",
"request": "^2.61.0",
"sinon": "^1.17.3",
"supertest": "^1.0.1"
},
"jshintConfig": {
Expand Down
6 changes: 6 additions & 0 deletions tests/mock/MockErrorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright 2014, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
var lodash = require('lodash');
var MockErrorService = {
name: 'mock_error_service',

Expand All @@ -21,6 +22,11 @@ var MockErrorService = {
* @static
*/
read: function (req, resource, params, config, callback) {
if (req.query && req.query.cors && params && Object.keys(params).length === 0) {
// in our CORS test, we use regular query params instead of matrix params for the params object will be empty
// create params from req.query but omit the context values(i.e. cors & returnMeta)
params = lodash.omitBy(req.query, function (v, k) { return k === 'cors' || k === 'returnMeta' || k === '_csrf' });
}
callback({
statusCode: parseInt(params.statusCode),
output: params.output,
Expand Down
6 changes: 6 additions & 0 deletions tests/mock/MockService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
var debug = require('debug')('mservice');
var lodash = require('lodash');
var MockService = {
name: 'mock_service',

Expand All @@ -22,6 +23,11 @@ var MockService = {
* @static
*/
read: function (req, resource, params, config, callback) {
if (req.query && req.query.cors && params && Object.keys(params).length === 0) {
// in our CORS test, we use regular query params instead of matrix params for the params object will be empty
// create params from req.query but omit the context values(i.e. cors & returnMeta)
params = lodash.omitBy(req.query, function (v, k) { return k === 'cors' || k === 'returnMeta' || k === '_csrf' });
}
debug([].splice.call(arguments, 1));
callback(null, {
operation: {
Expand Down
25 changes: 0 additions & 25 deletions tests/mock/app.js

This file was deleted.

26 changes: 0 additions & 26 deletions tests/mock/corsApp.js

This file was deleted.

21 changes: 21 additions & 0 deletions tests/mock/mockApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright 2016, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
var DEFAULT_XHR_PATH = '/api';

var express = require('express');
var bodyParser = require('body-parser');
var app = express();

var FetcherServer = require('../../libs/fetcher');
var mockService = require('./MockService');
var mockErrorService = require('./MockErrorService');
FetcherServer.registerService(mockService);
FetcherServer.registerService(mockErrorService);

app.use(bodyParser.json());
app.use(DEFAULT_XHR_PATH, FetcherServer.middleware());

module.exports = app;
module.exports.DEFAULT_XHR_PATH = DEFAULT_XHR_PATH;
28 changes: 28 additions & 0 deletions tests/mock/mockCorsApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright 2016, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
var express = require('express');
var bodyParser = require('body-parser');
var app = express();

var FetcherServer = require('../../libs/fetcher');
var mockService = require('./MockService');
var mockErrorService = require('./MockErrorService');
FetcherServer.registerService(mockService);
FetcherServer.registerService(mockErrorService);

app.use(bodyParser.json());
app.use(function cors (req, res, next) {
if (req.query.cors) {
res.set('Access-Control-Allow-Origin', '*');
next();
} else {
res.sendStatus(403);
}
});
app.use(FetcherServer.middleware());

var CORS_PORT = 3001;
module.exports = app.listen(CORS_PORT);
module.exports.corsPath = 'http://localhost:' + CORS_PORT;
Loading

0 comments on commit d972790

Please sign in to comment.