Skip to content
Browse files

v1.0.0

  • Loading branch information...
1 parent 9eec647 commit 1806efaad85fffb80251416c8d21c1b69e1e886f @dncrews dncrews committed Oct 22, 2013
Showing with 43 additions and 97 deletions.
  1. +6 −3 bin/tests
  2. +21 −23 lib/fs-superagent.js
  3. +0 −69 lib/node/index.js
  4. +4 −2 package.json
  5. +12 −0 test/server/fs-superagent.js
View
9 bin/tests
@@ -1,4 +1,7 @@
-#/usr/bin/env bash
+#/usr/bin/env sh
+
+# set env to test. Run mocha tests at test/server. This file will be invoked by npm test locally.
+NODE_ENV=test `pwd`/node_modules/.bin/mocha --reporter spec --globals page,assets,showingChat test/server/
+# NODE_ENV=test `pwd`/node_modules/.bin/mocha --reporter spec --globals page,assets,showingChat test/server/fs-superagent
+
-# NODE_ENV=test `pwd`/node_modules/.bin/mocha --require blanket --reporter json-cov test/server/
-NODE_ENV=test `pwd`/node_modules/.bin/mocha --reporter spec test/server/
View
44 lib/fs-superagent.js
@@ -28,18 +28,19 @@
'use strict';
var superagent = require('superagent-defaults')
- , debug = require('debug')('fsSuperagent')
+ , debug = require('debug')('fs-superagent:core')
, url = require('url');
module.exports = fsSuperagent;
-function fsSuperagent(config) {
- var context = superagent()
+function fsSuperagent(config, addlRequestHandler) {
+ var user = config.user || {}
, cookies = config.cookies || {}
, headers = config.headers || {};
- return context.on('request', function(request) {
+
+ return superagent().on('request', function(request) {
var reqId = headers['x-request-id']
- , sessId = cookies.fssessionid
+ , sessId = user.sessionId || cookies.fssessionid
, info = url.parse(request.url)
, addToLogs = {}
, endHandler;
@@ -49,7 +50,7 @@ function fsSuperagent(config) {
request.set('accept', 'application/json');
// Do we want a default timeout on the client?
- request.timeout(20000);
+ // request.timeout(20000);
if (reqId) request.set('x-request-id', reqId);
if (sessId) request.set('authorization', 'Bearer ' + sessId);
@@ -61,18 +62,15 @@ function fsSuperagent(config) {
};
request.setSessionId = function(sessionName) {
- request.query(sessionName, sessId);
+ var query = {};
+
+ query[sessionName] = sessId;
+ request.query(query);
return request;
};
- request.handleErrors = function() {
- request.on('error', function errorHandler(err) {
- // TODO: Add Metric logging (FAILURE)
- if (err.timeout) {
- // TODO: Do something to register a timeout to the server
- }
- // TODO: Do something to register an error with the server
- });
+ request.setErrorHandler = function(handler) {
+ request.on('error', handler);
return request;
};
@@ -96,6 +94,10 @@ function fsSuperagent(config) {
}
};
+ if (addlRequestHandler) {
+ addlRequestHandler(request);
+ }
+
// We have to overwrite the request.end to fix the error handling problems
request._end = request.end;
@@ -118,18 +120,14 @@ function fsSuperagent(config) {
doCb(null, response);
};
// Bind the error callback for errors
- request
- .on('error', function(err) {
- // handle errors too?
- doCb(err);
- });
+ request.setErrorHandler(doCb);
}
request._end(function(response) {
// TODO: Add metric logging (FINISHED)
- console.log('addToLogs', addToLogs);
- console.log('contentLength', response.headers['content-length']);
- console.log('status' , response.status);
+ // console.log('addToLogs', addToLogs);
+ // console.log('contentLength', response.headers['content-length']);
+ // console.log('status' , response.status);
okCB(response);
});
};
View
69 lib/node/index.js
@@ -1,69 +0,0 @@
-/**
- * Description:
- * Creates req.superagent from fs-superagent.
- * Adds some additional defaults:
- *
- * - Metric logging
- * - Request closing on client close
- * Creates a req.superagent() function which add some nice defaults and log metrics to a superagent call.
- *
- * Example
- *
- * app.get('/', function(req, res) {
- * req.superagent
- * .get('http://example.com')
- * .end(function(err, res) {
- * // The request has the default headers applied
- * // do app work here
- * })
- * });
- */
-
-/*global Buffer:false,clearInterval:false,clearTimeout:false,console:false,exports:false,global:false,module:false,process:false,querystring:false,require:false,setInterval:false,setTimeout:false,__filename:false,__dirname:false */
-'use strict';
-
-/**
- * Module deps
- */
-var url = require('url')
- , fsSuperagent = require('../fs-superagent');
-
-module.exports = function() {
- return function superagent(req, res, next) {
- var context = fsSuperagent({
- 'cookies' : req.cookies,
- 'headers' : req.headers
- });
- context.on('request', function(request) {
- var info = url.parse(request.url)
- , addToLogs
- , done = req.metric.profile('response_time', {
- lib: 'fs-superagent',
- protocol: (info.protocol || '').replace(':', ''),
- hostname: info.hostname,
- port: info.port,
- path: info.path
- });
-
- function closeRequest() {
- request.abort();
- }
-
- req.on('close', closeRequest);
-
- request.on('response', function(response) {
- var metricData = {
- code: response.status,
- bytes: response.headers['content-length']
- };
- for (var key in addToLogs) {
- if (addToLogs.hasOwnProperty(key)) {
- metricData[key] = addToLogs[key];
- }
- }
- req.removeListener('close', closeRequest);
- done(metricData);
- });
- });
- };
-};
View
6 package.json
@@ -1,6 +1,6 @@
{
"name": "fs-superagent",
- "version": "0.0.0",
+ "version": "1.0.0",
"description": "FamilySearch-specific Superagent Defaults",
"main": "index.js",
"scripts": {
@@ -17,7 +17,9 @@
},
"dependencies": {
"express": "~2.5.11",
- "debug": "~0.7.2"
+ "debug": "~0.7.2",
+ "superagent": "~0.15.7",
+ "superagent-defaults": "~0.1.4"
},
"devDependencies": {
"mocha": "~1.13.0",
View
12 test/server/fs-superagent.js
@@ -0,0 +1,12 @@
+/*global Buffer:false,clearInterval:false,clearTimeout:false,console:false,exports:false,global:false,module:false,process:false,querystring:false,require:false,setInterval:false,setTimeout:false,__filename:false,__dirname:false,describe:false,it:false,before:false,after:false */
+'use strict';
+
+var expect = require('expect.js');
+
+describe("FS Superagent", function() {
+ describe("TODO: some addl unit testing outside of woodruff :)", function() {
+ it ('should fail', function(done) {
+ expect(true).to.be(false);
+ });
+ });
+});

0 comments on commit 1806efa

Please sign in to comment.
Something went wrong with that request. Please try again.