浏览代码

avoid reserved property names (+ test), fixes #145

jomo 9 年之前
父节点
当前提交
c8d74d47be
共有 2 个文件被更改,包括 39 次插入29 次删除
  1. 9 6
      lib/helpers.js
  2. 30 23
      test/test.js

+ 9 - 6
lib/helpers.js

@@ -119,15 +119,18 @@ var requests = {
 };
 
 function push_request(userId, type, fun) {
-  if (!requests[type][userId]) {
-    requests[type][userId] = [];
+  // avoid special properties (e.g. 'constructor')
+  var userId_safe = "!" + userId;
+  if (!requests[type][userId_safe]) {
+    requests[type][userId_safe] = [];
   }
-  requests[type][userId].push(fun);
+  requests[type][userId_safe].push(fun);
 }
 
 // calls back all queued requests that match userId and type
 function resume(userId, type, err, hash) {
-  var callbacks = requests[type][userId];
+  var userId_safe = "!" + userId;
+  var callbacks = requests[type][userId_safe];
   if (callbacks) {
     if (callbacks.length > 1) {
       logging.debug(callbacks.length, "simultaneous requests for", userId);
@@ -142,7 +145,7 @@ function resume(userId, type, err, hash) {
     }
 
     // it's still an empty array
-    delete requests[type][userId];
+    delete requests[type][userId_safe];
   }
 }
 
@@ -152,7 +155,7 @@ function resume(userId, type, err, hash) {
 // callback: error, image hash
 function store_images(rid, userId, cache_details, type, callback) {
   var is_uuid = userId.length > 16;
-  if (requests[type][userId]) {
+  if (requests[type]["!" + userId]) {
     logging.debug(rid, "adding to request queue");
     push_request(userId, type, callback);
   } else {

+ 30 - 23
test/test.js

@@ -301,30 +301,37 @@ describe("Crafatar", function() {
     });
 
     it("should not fail on simultaneous requests", function(done) {
-      var url = "http://localhost:3000/avatars/696a82ce41f44b51aa31b8709b8686f0";
-      // 10 requests at once
-      var requests = 10;
-      var finished = 0;
-      function partDone() {
-        finished++;
-        if (requests === finished) {
-          done();
+      // do not change "constructor" !
+      // it's a reserved property name, we're testing for that
+      var sids = ["696a82ce41f44b51aa31b8709b8686f0", "constructor"];
+
+      for (var j in sids) {
+        var id = sids[j];
+        var url = "http://localhost:3000/avatars/" + id;
+        // 10 requests at once
+        var requests = 10;
+        var finished = 0;
+        function partDone() {
+          finished++;
+          if (requests === finished) {
+            done();
+          }
+        }
+        function req() {
+          request.get(url, function(error, res, body) {
+            assert.ifError(error);
+            assert.strictEqual(res.statusCode, 200);
+            assert_headers(res);
+            assert(res.headers.etag);
+            assert.strictEqual(res.headers["content-type"], "image/png");
+            assert(body);
+            partDone();
+          });
+        }
+        // make simultanous requests
+        for (var k = 0; k < requests; k++) {
+          req(k);
         }
-      }
-      function req() {
-        request.get(url, function(error, res, body) {
-          assert.ifError(error);
-          assert.strictEqual(res.statusCode, 200);
-          assert_headers(res);
-          assert(res.headers.etag);
-          assert.strictEqual(res.headers["content-type"], "image/png");
-          assert(body);
-          partDone();
-        });
-      }
-      // make simultanous requests
-      for (var j = 0; j < requests; j++) {
-        req(j);
       }
     });