Explorar o código

fix #22, caused by @e6481e3

jomo %!s(int64=10) %!d(string=hai) anos
pai
achega
3d708d2342
Modificáronse 2 ficheiros con 16 adicións e 15 borrados
  1. 7 12
      modules/networking.js
  2. 9 3
      test/test.js

+ 7 - 12
modules/networking.js

@@ -38,20 +38,18 @@ var get_username_url = function(name, callback) {
     } else if (error) {
       callback(error, null);
     } else if (response.statusCode == 404) {
-      // skin doesn't exist
+      // skin (or user) doesn't exist
       logging.log(name + " has no skin");
-      callback(0, null);
+      callback(null, null);
     } else if (response.statusCode == 429) {
       // Too Many Requests
       // Never got this, seems like skins aren't limited
-      logging.warn(name + " Too many requests");
-      logging.warn(body);
+      logging.warn(body || "Too many requests");
       callback(null, null);
     } else {
       logging.error(name + " Unknown error:");
       logging.error(response);
-      logging.error(body);
-      callback(null, null);
+      callback(body || "Unknown error", null);
     }
   });
 };
@@ -72,17 +70,14 @@ var get_uuid_url = function(uuid, callback) {
     } else if (response.statusCode == 204 || response.statusCode == 404) {
       // we get 204 No Content when UUID doesn't exist (including 404 in case they change that)
       logging.log(uuid + " uuid does not exist");
-      callback(0, null);
+      callback(null, null);
     } else if (response.statusCode == 429) {
       // Too Many Requests
-      logging.warn(uuid + " Too many requests");
-      logging.warn(body);
-      callback(null, null);
+      callback(body || "Too many requests", null);
     } else {
       logging.error(uuid + " Unknown error:");
       logging.error(response);
-      logging.error(body);
-      callback(null, null);
+      callback(body || "Unknown error", null);
     }
   });
 };

+ 9 - 3
test/test.js

@@ -64,13 +64,13 @@ describe('Crafatar', function() {
     });
     it("should not exist (uuid)", function(done) {
       networking.get_skin_url("00000000000000000000000000000000", function(err, profile) {
-        assert.strictEqual(err, 0);
+        assert.strictEqual(err, null);
         done();
       });
     });
     it("should not exist (username)", function(done) {
       networking.get_skin_url("Steve", function(err, profile) {
-        assert.strictEqual(err, 0);
+        assert.strictEqual(err, null);
         done();
       });
     });
@@ -154,8 +154,14 @@ describe('Crafatar', function() {
     before(function() {
       cache.get_redis().flushall();
     });
-    it("should be rate limited", function(done) {
+    it("should be rate limited (uuid)", function(done) {
       helpers.get_avatar(uuid, false, 160, function(err, status, image) {
+        assert.strictEqual(JSON.parse(err).error, "TooManyRequestsException");
+        done();
+      });
+    });
+    it("should NOT be rate limited (username)", function(done) {
+      helpers.get_avatar(username, false, 160, function(err, status, image) {
         assert.strictEqual(err, null);
         done();
       });