From 8027b00664e864d4720567fd85c77193dbf83fc6 Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Fri, 19 Aug 2011 13:19:24 +0300
Subject: [PATCH] fixes to wsapi_client, and a partially implemented ad-hoc
 test script using it for account creation

---
 performance/lib/test.js         | 19 +++++++++++++++++++
 performance/lib/wsapi_client.js | 11 ++++++-----
 2 files changed, 25 insertions(+), 5 deletions(-)
 create mode 100644 performance/lib/test.js

diff --git a/performance/lib/test.js b/performance/lib/test.js
new file mode 100644
index 000000000..e88531b11
--- /dev/null
+++ b/performance/lib/test.js
@@ -0,0 +1,19 @@
+const wcli = require("./wsapi_client.js");
+
+// lets create a user!
+
+// we'll need a cookie jar (client context)
+var ctx = {};
+
+// and a configuration (what server we're talking to)
+var cfg = { browserid: 'http://127.0.0.1:10002' }
+
+// now start the dance with a call to stage_user
+wcli.post(cfg, '/wsapi/stage_user', ctx, {
+  email: 'first@fakeemail.com',
+  pass: 'firstfakepass',
+  pubkey: 'fakepubkey',
+  site:'fakesite.com'
+}, function (r) {
+  console.log(r.body);
+});
diff --git a/performance/lib/wsapi_client.js b/performance/lib/wsapi_client.js
index 3b8d3b73a..4772632c2 100644
--- a/performance/lib/wsapi_client.js
+++ b/performance/lib/wsapi_client.js
@@ -44,7 +44,8 @@
 const
 http = require('http'),
 https = require('https'),
-url = require('url');
+url = require('url'),
+querystring = require('querystring');
 
 function injectCookies(ctx, headers) {
   if (ctx.cookieJar && Object.keys(ctx.cookieJar).length) {
@@ -101,17 +102,17 @@ exports.get = function(cfg, path, context, cb) {
 };
 
 function withCSRF(cfg, context, cb) {
-  if (context.csrf) cb();
+  if (context.csrf) cb(context.csrf);
   else {
     exports.get(cfg, '/wsapi/csrf', context, function(r) {
       context.csrf = r.body;
-      cb();
+      cb(context.csrf);
     });
   }
 }
 
 exports.post = function(cfg, path, context, postArgs, cb) {
-  withCSRF(cfg, context, function() {
+  withCSRF(cfg, context, function(csrf) {
     // parse the server URL (cfg.browserid)
     var uObj;
     var meth;
@@ -128,7 +129,7 @@ exports.post = function(cfg, path, context, postArgs, cb) {
     injectCookies(context, headers);
 
     if (typeof postArgs === 'object') {
-      postArgs['csrf'] = g_csrf;
+      postArgs['csrf'] = csrf;
       body = querystring.stringify(postArgs);
     }
 
-- 
GitLab