From 2ead7961f780b2205897e77785493b8e8b62de03 Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Mon, 22 Aug 2011 12:54:29 +0300
Subject: [PATCH] port unit tests to new wsapi_client abstraction to eliminate
 duplicate code

---
 browserid/tests/lib/wsapi.js | 126 +++--------------------------------
 libs/wsapi_client.js         |   5 ++
 2 files changed, 16 insertions(+), 115 deletions(-)

diff --git a/browserid/tests/lib/wsapi.js b/browserid/tests/lib/wsapi.js
index b9c551f84..b6d8eb1da 100644
--- a/browserid/tests/lib/wsapi.js
+++ b/browserid/tests/lib/wsapi.js
@@ -34,132 +34,28 @@
  * ***** END LICENSE BLOCK ***** */
 
 const
-http = require('http'),
-querystring = require('querystring');
+wcli = require('../../../libs/wsapi_client');
 
-// wsapi abstractions trivial cookie jar
-var cookieJar = {};
+// the client "context"
+var context = {};
 
-// fetch the CSRF token for POSTs
-var g_csrf = undefined;
-
-var fetchCSRF = function(headers, cb) {
-  // used cached csrf if available
-  if (g_csrf) return setTimeout(function() { cb(g_csrf); }, 0);
+// the configuration
+var configuration = {
+  browserid: 'http://127.0.0.1:62700/'
+}
 
-  // otherwise, get a new one
-  return http.get({
-    host: '127.0.0.1',
-    port: '62700',
-    path: '/wsapi/csrf',
-    headers: headers
-  }, function(res) {
-    extractCookies(res);
-    var body = "";
-    res.on('data', function(chunk) { body += chunk; })
-    .on('end', function() {
-      g_csrf = body;
-      cb(body);
-    });
-  }).on('error', function (e) {
-    cb();
-  });
+exports.clearCookies = function() {
+  wcli.clearCookies(context);
 };
 
-exports.clearCookies = function() { cookieJar = {}; g_csrf = undefined; };
-
-function extractCookies(res) {
-  if (res.headers['set-cookie']) {
-    res.headers['set-cookie'].forEach(function(cookie) {
-      var m = /^([^;]+)(?:;.*)$/.exec(cookie);
-      if (m) {
-        var x = m[1].split('=');
-        cookieJar[x[0]] = x[1];
-      }
-    });
-  }
-}
-
-// A macro for wsapi requests
 exports.get = function (path, getArgs) {
   return function () {
-    var cb = this.callback;
-    if (typeof getArgs === 'object')
-      path += "?" + querystring.stringify(getArgs);
-
-    var headers = {};
-    if (Object.keys(cookieJar).length) {
-      headers['Cookie'] = "";
-      for (var k in cookieJar) {
-        headers['Cookie'] += k + "=" + cookieJar[k];
-      }
-    }
-
-    http.get({
-      host: '127.0.0.1',
-      port: '62700',
-      path: path,
-      headers: headers
-    }, function(res) {
-      // see if there are any set-cookies that we should honor
-      extractCookies(res);
-      var body = '';
-      res.on('data', function(chunk) { body += chunk; })
-      .on('end', function() {
-        cb({code: res.statusCode, headers: res.headers, body: body});
-      });
-    }).on('error', function (e) {
-      cb();
-    });
+    wcli.get(configuration, path, context, getArgs, this.callback);
   };
 };
 
-// FIXME: dedup code
-
-// A macro for wsapi requests
-// add CSRF protection to this test
 exports.post = function (path, postArgs) {
   return function () {
-    var cb = this.callback;
-
-    var headers = {
-      'content-type': 'application/x-www-form-urlencoded'
-    };
-
-    fetchCSRF(headers, function() {
-
-      if (Object.keys(cookieJar).length) {
-        headers['Cookie'] = "";
-        for (var k in cookieJar) {
-          headers['Cookie'] += k + "=" + cookieJar[k];
-        }
-      }
-
-      if (typeof postArgs === 'object') {
-        postArgs['csrf'] = g_csrf;
-        body = querystring.stringify(postArgs);
-      }
-
-      var req = http.request({
-        host: '127.0.0.1',
-        port: '62700',
-        path: path,
-        headers: headers,
-        method: "POST"
-      }, function(res) {
-        extractCookies(res);
-        var body = '';
-        res.on('data', function(chunk) { body += chunk; })
-        .on('end', function() {
-          cb({code: res.statusCode, headers: res.headers, body: body});
-        });
-      }).on('error', function (e) {
-        cb();
-      });
-
-      // send the POST
-      req.write(body);
-      req.end();
-    });
+    wcli.post(configuration, path, context, postArgs, this.callback);
   };
 };
diff --git a/libs/wsapi_client.js b/libs/wsapi_client.js
index 8500aa7ba..4c89f98cc 100644
--- a/libs/wsapi_client.js
+++ b/libs/wsapi_client.js
@@ -69,6 +69,11 @@ function extractCookies(ctx, res) {
   }
 }
 
+exports.clearCookies = function(ctx) {
+  if (ctx && ctx.cookieJar) delete ctx.cookieJar;
+  if (ctx && ctx.csrf) delete ctx.csrf;
+};
+
 exports.get = function(cfg, path, context, getArgs, cb) {
   // parse the server URL (cfg.browserid)
   var uObj;
-- 
GitLab