Bug 990834 (part 1) - minor refactor of hawk tests. r=ckarlof, a=sylvestre
authorMark Hammond <mhammond@skippinet.com.au>
Mon, 14 Apr 2014 11:53:29 +1000
changeset 183732 deb83f2f75faf0dc402e934cb5f52d3539e87e08
parent 183731 915700dc51980eaef319a2193b519e4f593f39e2
child 183733 329a2a180a8b7d627dd2d42cd45b500aa707b8e1
push id3463
push usermhammond@skippinet.com.au
push dateMon, 14 Apr 2014 03:15:06 +0000
treeherdermozilla-beta@b074e386a410 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersckarlof, sylvestre
bugs990834
milestone29.0
Bug 990834 (part 1) - minor refactor of hawk tests. r=ckarlof, a=sylvestre
services/sync/tests/unit/test_browserid_identity.js
--- a/services/sync/tests/unit/test_browserid_identity.js
+++ b/services/sync/tests/unit/test_browserid_identity.js
@@ -407,66 +407,85 @@ add_task(function test_getTokenErrorWith
   // The observer should have fired - check it got the value in the response.
   Assert.ok(Status.backoffInterval >= 200000);
 });
 
 add_task(function test_getHAWKErrors() {
   _("BrowserIDManager correctly handles various HAWK failures.");
 
   _("Arrange for a 401 - Sync should reflect an auth error.");
-  yield initializeIdentityWithHAWKFailure({
-    status: 401,
-    headers: {"content-type": "application/json"},
-    body: JSON.stringify({}),
+  let config = makeIdentityConfig();
+  yield initializeIdentityWithHAWKResponseFactory(config, function(method, data, uri) {
+    Assert.equal(method, "post");
+    Assert.equal(uri, "http://mockedserver:9999/certificate/sign")
+    return {
+      status: 401,
+      headers: {"content-type": "application/json"},
+      body: JSON.stringify({}),
+    }
   });
   Assert.equal(Status.login, LOGIN_FAILED_LOGIN_REJECTED, "login was rejected");
 
   // XXX - other interesting responses to return?
 
   // And for good measure, some totally "unexpected" errors - we generally
   // assume these problems are going to magically go away at some point.
   _("Arrange for an empty body with a 200 response - should reflect a network error.");
-  yield initializeIdentityWithHAWKFailure({
-    status: 200,
-    headers: [],
-    body: "",
+  yield initializeIdentityWithHAWKResponseFactory(config, function(method, data, uri) {
+    Assert.equal(method, "post");
+    Assert.equal(uri, "http://mockedserver:9999/certificate/sign")
+    return {
+      status: 200,
+      headers: [],
+      body: "",
+    }
   });
   Assert.equal(Status.login, LOGIN_FAILED_NETWORK_ERROR, "login state is LOGIN_FAILED_NETWORK_ERROR");
 });
 
 // End of tests
 // Utility functions follow
 
 // Create a new browserid_identity object and initialize it with a
-// hawk mock that simulates a failure.
+// hawk mock that simulates HTTP responses.
+// The callback function will be called each time the mocked hawk server wants
+// to make a request.  The result of the callback should be the mock response
+// object that will be returned to hawk.
 // A token server mock will be used that doesn't hit a server, so we move
 // directly to a hawk request.
-function* initializeIdentityWithHAWKFailure(response) {
+function* initializeIdentityWithHAWKResponseFactory(config, cbGetResponse) {
   // A mock request object.
-  function MockRESTRequest() {};
+  function MockRESTRequest(uri, credentials, extra) {
+    this._uri = uri;
+    this._credentials = credentials;
+    this._extra = extra;
+  };
   MockRESTRequest.prototype = {
     setHeader: function() {},
     post: function(data, callback) {
-      this.response = response;
+      this.response = cbGetResponse("post", data, this._uri, this._credentials, this._extra);
+      callback.call(this);
+    },
+    get: function(callback) {
+      this.response = cbGetResponse("get", null, this._uri, this._credentials, this._extra);
       callback.call(this);
     }
   }
 
   // The hawk client.
   function MockedHawkClient() {}
-  MockedHawkClient.prototype = new HawkClient();
+  MockedHawkClient.prototype = new HawkClient("http://mockedserver:9999");
   MockedHawkClient.prototype.constructor = MockedHawkClient;
   MockedHawkClient.prototype.newHAWKAuthenticatedRESTRequest = function(uri, credentials, extra) {
-    return new MockRESTRequest();
+    return new MockRESTRequest(uri, credentials, extra);
   }
 
   // tie it all together - configureFxAccountIdentity isn't useful here :(
   let fxaClient = new MockFxAccountsClient();
   fxaClient.hawk = new MockedHawkClient();
-  let config = makeIdentityConfig();
   let internal = {
     fxAccountsClient: fxaClient,
   }
   let fxa = new FxAccounts(internal);
   fxa.internal.currentAccountState.signedInUser = {
       accountData: config.fxaccount.user,
   };