summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2012-07-02 17:39:56 +0200
committerAzul <azul@leap.se>2012-07-02 17:39:56 +0200
commit337df30b51d2c1bdddcb7fbd05f0ccf46a7a31b3 (patch)
tree706e68051b796b172d7f172dc3da1d13027a2e6e
parent4da016f111510f185b4bf16b4164d43342ef1d66 (diff)
expectRequest and respond{JSON,XML} functions to simplify the tests
-rw-r--r--javascript/spec/login.js27
-rw-r--r--javascript/spec/signup.js40
-rw-r--r--javascript/spec/specHelper.js35
3 files changed, 45 insertions, 57 deletions
diff --git a/javascript/spec/login.js b/javascript/spec/login.js
index 8d25246..55cfa16 100644
--- a/javascript/spec/login.js
+++ b/javascript/spec/login.js
@@ -30,15 +30,12 @@ describe("Login", function() {
it("works with XML responses", function(){
this.srp.identify();
- expect(this.requests.length).toBe(1);
- expect(this.requests[0].url).toBe("handshake/");
- expect(this.requests[0].requestBody).toBe("I=user&A=" + A);
- specHelper.respondXML(this.requests[0], "<r s='"+salt+"' B='"+B+"' />");
- expect(this.requests.length).toBe(2);
- expect(this.requests[1].url).toBe("authenticate/");
- expect(this.requests[1].requestBody).toBe("M=" + M);
- specHelper.respondXML(this.requests[1], "<M>"+M2+"</M>");
-
+
+ this.expectRequest('handshake/', 'I=user&A='+A);
+ this.respondXML("<r s='"+salt+"' B='"+B+"' />");
+ this.expectRequest('authenticate/', 'M='+M);
+ this.respondXML("<M>"+M2+"</M>");
+
expect(this.srp.success).toHaveBeenCalled();
expect(window.location.hash).toBe("#logged_in")
});
@@ -46,14 +43,10 @@ describe("Login", function() {
it("works with JSON responses", function(){
this.srp.identify();
- expect(this.requests.length).toBe(1);
- expect(this.requests[0].url).toBe("handshake/");
- expect(this.requests[0].requestBody).toBe("I=user&A=" + A);
- specHelper.respondJSON(this.requests[0], {s: salt, B: B});
- expect(this.requests.length).toBe(2);
- expect(this.requests[1].url).toBe("authenticate/");
- expect(this.requests[1].requestBody).toBe("M=" + M);
- specHelper.respondJSON(this.requests[1], {M: M2});
+ this.expectRequest('handshake/', 'I=user&A='+A);
+ this.respondJSON({s: salt, B: B});
+ this.expectRequest('authenticate/', 'M='+M);
+ this.respondJSON({M: M2});
expect(this.srp.success).toHaveBeenCalled();
expect(window.location.hash).toBe("#logged_in")
diff --git a/javascript/spec/signup.js b/javascript/spec/signup.js
index 90fe418..b5099b8 100644
--- a/javascript/spec/signup.js
+++ b/javascript/spec/signup.js
@@ -2,11 +2,7 @@ describe("Signup", function() {
beforeEach(function() {
this.srp = new SRP();
- this.xhr = sinon.useFakeXMLHttpRequest();
- var requests = this.requests = [];
- this.xhr.onCreate = function (xhr) {
- requests.push(xhr);
- };
+ specHelper.setupFakeXHR.apply(this);
});
afterEach(function() {
@@ -21,9 +17,8 @@ describe("Signup", function() {
var callback = sinon.spy();
this.srp.register_receive_salt = callback;
this.srp.register();
- expect(this.requests.length).toBe(1);
-
- specHelper.respondXML(this.requests[0], "<salt>5d3055e0acd3ddcfc15</salt>");
+ this.expectRequest('register/salt/', "I=user")
+ this.respondXML("<salt>5d3055e0acd3ddcfc15</salt>");
expect(callback.called).toBeTruthy();
});
@@ -31,9 +26,8 @@ describe("Signup", function() {
var callback = sinon.spy();
this.srp.register_send_verifier = callback;
this.srp.register();
- expect(this.requests.length).toBe(1);
-
- specHelper.respondXML(this.requests[0], "<salt>5d3055e0acd3ddcfc15</salt>");
+ this.expectRequest('register/salt/', "I=user")
+ this.respondXML("<salt>5d3055e0acd3ddcfc15</salt>");
expect(callback).toHaveBeenCalledWith("adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44");
});
@@ -41,14 +35,10 @@ describe("Signup", function() {
var callback = sinon.spy();
this.srp.identify = callback;
this.srp.register();
- expect(this.requests.length).toBe(1);
- expect(this.requests[0].url).toBe("register/salt/");
- expect(this.requests[0].requestBody).toBe("I=user");
- specHelper.respondXML(this.requests[0], "<salt>5d3055e0acd3ddcfc15</salt>");
- expect(this.requests.length).toBe(2);
- expect(this.requests[1].url).toBe("register/user/");
- expect(this.requests[1].requestBody).toBe("v=adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44");
- specHelper.respondXML(this.requests[1], "<ok/>");
+ this.expectRequest('register/salt/', "I=user")
+ this.respondXML("<salt>5d3055e0acd3ddcfc15</salt>");
+ this.expectRequest('register/user/', "v=adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44");
+ this.respondXML("<ok />");
expect(callback).toHaveBeenCalled();
});
@@ -56,14 +46,10 @@ describe("Signup", function() {
var callback = sinon.spy();
this.srp.identify = callback;
this.srp.register();
- expect(this.requests.length).toBe(1);
- expect(this.requests[0].url).toBe("register/salt/");
- expect(this.requests[0].requestBody).toBe("I=user");
- specHelper.respondJSON(this.requests[0], {salt: "5d3055e0acd3ddcfc15"});
- expect(this.requests.length).toBe(2);
- expect(this.requests[1].url).toBe("register/user/");
- expect(this.requests[1].requestBody).toBe("v=adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44");
- specHelper.respondJSON(this.requests[1], {ok: true});
+ this.expectRequest('register/salt/', "I=user")
+ this.respondJSON({salt: "5d3055e0acd3ddcfc15"});
+ this.expectRequest('register/user/', "v=adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44");
+ this.respondJSON({ok: true});
expect(callback).toHaveBeenCalled();
});
diff --git a/javascript/spec/specHelper.js b/javascript/spec/specHelper.js
index 747fa9d..21a0cb7 100644
--- a/javascript/spec/specHelper.js
+++ b/javascript/spec/specHelper.js
@@ -1,31 +1,40 @@
var specHelper = (function() {
// HELPERS
- function respondXML(request, content) {
+ function setupFakeXHR() {
+ this.xhr = sinon.useFakeXMLHttpRequest();
+ var requests = this.requests = [];
+ this.xhr.onCreate = function (xhr) {
+ requests.push(xhr);
+ };
+ this.expectRequest = expectRequest;
+ this.respondJSON = respondJSON;
+ this.respondXML = respondXML;
+ }
+
+ function expectRequest(url, content) {
+ expect(this.requests.length).toBe(1);
+ expect(this.requests[0].url).toBe(url);
+ expect(this.requests[0].requestBody).toBe(content);
+ }
+
+ function respondXML(content) {
+ var request = this.requests.pop();
header = { "Content-Type": "application/xml;charset=utf-8" };
body = '<?xml version="1.0" encoding="UTF-8"?>\n';
body += content;
request.respond(200, header, body);
}
- function respondJSON(request, object) {
+ function respondJSON(object) {
+ var request = this.requests.pop();
header = { "Content-Type": "application/json;charset=utf-8" };
body = JSON.stringify(object);
request.respond(200, header, body);
}
- function setupFakeXHR() {
- this.xhr = sinon.useFakeXMLHttpRequest();
- var requests = this.requests = [];
- this.xhr.onCreate = function (xhr) {
- requests.push(xhr);
- };
- }
-
return {
- respondJSON: respondJSON,
- respondXML: respondXML,
- setupFakeXHR: setupFakeXHR
+ setupFakeXHR: setupFakeXHR,
}
})();