Bug 760909 - Add onRequest and mock response handling to AITC server; r=gps
authorNick Desaulniers <ndesaulniers@mozilla.com>
Thu, 12 Jul 2012 16:52:58 -0700
changeset 99298 3eddb4087f85950c969eb9b567bc0f8777333f01
parent 99297 a2fe3cbf454a32033fbe41c40c80dd95dbe3d384
child 99299 c2d5b44491943e59b7403cb16b6bb5bcffc7ef1e
push id23117
push usergszorc@mozilla.com
push dateSat, 14 Jul 2012 20:35:19 +0000
treeherdermozilla-central@8e2f9cc15bd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgps
bugs760909
milestone16.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 760909 - Add onRequest and mock response handling to AITC server; r=gps
services/common/aitcserver.js
services/common/tests/unit/test_aitc_server.js
--- a/services/common/aitcserver.js
+++ b/services/common/aitcserver.js
@@ -129,16 +129,21 @@ AITCServer10User.prototype = {
  */
 function AITCServer10Server() {
   this._log = Log4Moz.repository.getLogger("Services.Common.AITCServer");
 
   this.server = new HttpServer();
   this.port = null;
   this.users = {};
   this.autoCreateUsers = false;
+  this.mockStatus = {
+    code: null,
+    method: null
+  };
+  this.onRequest = null;
 
   this._appsAppHandlers = {
     GET:    this._appsAppGetHandler,
     PUT:    this._appsAppPutHandler,
     DELETE: this._appsAppDeleteHandler,
   };
 }
 AITCServer10Server.prototype = {
@@ -201,22 +206,42 @@ AITCServer10Server.prototype = {
    */
   getUser: function getUser(username) {
     if (!(username in this.users)) {
       throw new Error("user is not present in server: " + username);
     }
 
     return this.users[username];
   },
+  
+  /**
+   * Returns a specific status code for testing.
+   */
+  _respondWithMockStatus: function _respondWithMockStatus(request, response) {
+    response.setStatusLine(request.httpVersion, this.mockStatus.code,
+      this.mockStatus.method);
+    this._onRequest();
+  },
+
+  _onRequest: function _onRequest() {
+    if (typeof this.onRequest === 'function') {
+      this.onRequest();
+    }
+  },
 
   /**
    * HTTP handler for requests to /1.0/ which don't have a specific user
    * registered.
    */
   _generalHandler: function _generalHandler(request, response) {
+    if (this.mockStatus.code && this.mockStatus.method) {
+      this._respondWithMockStatus(request, response);
+      return;
+    }
+    this._onRequest();
     let path = request.path;
     this._log.info("Request: " + request.method + " " + path);
 
     if (path.indexOf(this.VERSION_PATH) != 0) {
       throw new Error("generalHandler invoked improperly.");
     }
 
     let rest = request.path.substr(this.VERSION_PATH.length);
@@ -241,16 +266,21 @@ AITCServer10Server.prototype = {
   },
 
   /**
    * HTTP handler for requests for a specific user.
    *
    * This handles request routing to the appropriate handler.
    */
   _userHandler: function _userHandler(username, request, response) {
+    if (this.mockStatus.code && this.mockStatus.method) {
+      this._respondWithMockStatus(request, response);
+      return;
+    }
+    this._onRequest();
     this._log.info("Request: " + request.method + " " + request.path);
     let path = request.path;
     let prefix = this.VERSION_PATH + username + "/";
 
     if (path.indexOf(prefix) != 0) {
       throw new Error("userHandler invoked improperly.");
     }
 
--- a/services/common/tests/unit/test_aitc_server.js
+++ b/services/common/tests/unit/test_aitc_server.js
@@ -158,11 +158,34 @@ add_test(function test_invalid_request_m
       allowed.add(method);
     }
 
     do_check_eq(allowed.size(), 3);
     for (let method of ["GET", "PUT", "DELETE"]) {
       do_check_true(allowed.has(method));
     }
 
-    run_next_test();
+    server.stop(run_next_test);
   });
 });
+
+add_test(function test_respond_with_mock_status() {
+  let username = '123'
+  let server = get_server_with_user(username);
+  server.mockStatus = {
+    code: 405,
+    method: "Method Not Allowed"
+  };
+  let request = new RESTRequest(server.url);
+  request.dispatch("GET", null, function onComplete(error){
+    do_check_eq(this.response.status, 405);
+    
+    server.mockStatus = {
+      code: 399,
+      method: "Self Destruct"
+    };
+    let request2 = new RESTRequest(server.url);
+    request2.dispatch("GET", null, function onComplete(error){
+      do_check_eq(this.response.status, 399);
+      server.stop(run_next_test);
+    });
+  });
+});
\ No newline at end of file