Merge server

This commit is contained in:
Jake 2015-02-11 20:30:31 -06:00
commit ed99d918c1
4 changed files with 161 additions and 18 deletions

View File

@ -12,3 +12,6 @@ notifications:
skip_join: true skip_join: true
services: services:
- redis-server - redis-server
cache:
directories:
- node_modules

View File

@ -45,6 +45,7 @@ module.exports = function(req, res) {
userId = userId.replace(/-/g, ""); userId = userId.replace(/-/g, "");
logging.log(rid + "userid: " + userId); logging.log(rid + "userid: " + userId);
try { try {
helpers.get_avatar(rid, userId, helm, size, function(err, status, image, hash) { helpers.get_avatar(rid, userId, helm, size, function(err, status, image, hash) {
logging.log(rid + "storage type: " + human_status[status]); logging.log(rid + "storage type: " + human_status[status]);

View File

@ -7,6 +7,7 @@ var http = require("http");
var mime = require("mime"); var mime = require("mime");
var url = require("url"); var url = require("url");
var fs = require("fs"); var fs = require("fs");
var server = null;
var routes = { var routes = {
index: require("./routes/index"), index: require("./routes/index"),
@ -86,15 +87,27 @@ function requestHandler(req, res) {
} }
} }
var boot = module.exports = function () { var exp = {};
exp.boot = function(callback) {
var port = process.env.PORT || 3000; var port = process.env.PORT || 3000;
var bind_ip = process.env.BIND || "127.0.0.1"; var bind_ip = process.env.BIND || "127.0.0.1";
logging.log("Server running on http://" + bind_ip + ":" + port + "/"); logging.log("Server running on http://" + bind_ip + ":" + port + "/");
http.createServer(requestHandler).listen(port, bind_ip); server = http.createServer(requestHandler).listen(port, bind_ip, function() {
callback();
});
}; };
exp.close = function(callback) {
server.close(function() {
callback();
});
}
module.exports = exp;
if (require.main === module) { if (require.main === module) {
boot(); exp.boot(function(){});
// cleaning worker // cleaning worker
setInterval(clean.run, config.cleaning_interval * 1000); setInterval(clean.run, config.cleaning_interval * 1000);

View File

@ -1,6 +1,5 @@
var assert = require("assert"); var assert = require("assert");
var fs = require("fs"); var fs = require("fs");
var networking = require("../modules/networking"); var networking = require("../modules/networking");
var helpers = require("../modules/helpers"); var helpers = require("../modules/helpers");
var logging = require("../modules/logging"); var logging = require("../modules/logging");
@ -8,7 +7,9 @@ var config = require("../modules/config");
var skins = require("../modules/skins"); var skins = require("../modules/skins");
var cache = require("../modules/cache"); var cache = require("../modules/cache");
var renders = require("../modules/renders"); var renders = require("../modules/renders");
var server = require("../server");
var cleaner = require("../modules/cleaner"); var cleaner = require("../modules/cleaner");
var request = require("request");
// we don't want tests to fail because of slow internet // we don't want tests to fail because of slow internet
config.http_timeout *= 3; config.http_timeout *= 3;
@ -32,8 +33,8 @@ function getRandomInt(min, max) {
var ids = [ var ids = [
uuid.toLowerCase(), uuid.toLowerCase(),
name.toLowerCase(), name.toLowerCase(),
name.toUpperCase(),
uuid.toUpperCase(), uuid.toUpperCase(),
name.toUpperCase()
]; ];
describe("Crafatar", function() { describe("Crafatar", function() {
@ -42,6 +43,10 @@ describe("Crafatar", function() {
before(function() { before(function() {
cache.get_redis().flushall(); cache.get_redis().flushall();
// largest possible integers, cause I don't know
// how big hard drives are these days
config.cleaning_disk_limit = Math.pow(2, 32) - 1;;
config.cleaning_redis_limit = Math.pow(2, 32) - 1;;
cleaner.run(); cleaner.run();
}); });
@ -100,7 +105,6 @@ describe("Crafatar", function() {
}); });
}); });
}); });
describe("Avatar", function() { describe("Avatar", function() {
// profile "Alex" - hoping it'll never have a skin // profile "Alex" - hoping it'll never have a skin
var alex_uuid = "ec561538f3fd461daff5086b22154bce"; var alex_uuid = "ec561538f3fd461daff5086b22154bce";
@ -169,13 +173,135 @@ describe("Crafatar", function() {
}); });
}); });
it("should not find the file", function(done) { it("should not find the file", function(done) {
skins.open_skin(rid, 'non/existant/path', function(err, img) { skins.open_skin(rid, "non/existant/path", function(err, img) {
assert.notStrictEqual(err, null); assert.notStrictEqual(err, null);
done(); done();
}); });
}); });
}); });
describe("Server", function() {
before(function(done) {
server.boot(function() {
done();
});
});
// Test the home page
it("should return a 200 (home page)", function(done) {
request.get("http://localhost:3000", function(error, res, body) {
assert.equal(200, res.statusCode);
done();
});
});
it("should return a 200 (asset request)", function(done) {
request.get("http://localhost:3000/stylesheets/style.css", function(error, res, body) {
assert.equal(200, res.statusCode);
done();
});
});
// invalid method, we only allow GET and HEAD requests
it("should return a 405 (invalid method)", function(done) {
request.post("http://localhost:3000", function(error, res, body) {
assert.equal(405, res.statusCode);
done();
});
});
it("should return a 422 (invalid size)", function(done) {
var size = config.max_size + 1;
request.get("http://localhost:3000/avatars/Jake0oo0?size=" + size, function(error, res, body) {
assert.equal(422, res.statusCode);
done();
});
});
it("should return a 422 (invalid scale)", function(done) {
var scale = config.max_scale + 1;
request.get("http://localhost:3000/renders/head/Jake0oo0?scale=" + scale, function(error, res, body) {
assert.equal(422, res.statusCode);
done();
});
});
// no default images for capes, should 404
it("should return a 404 (no cape)", function(done) {
request.get("http://localhost:3000/capes/Jake0oo0", function(error, res, body) {
assert.equal(404, res.statusCode);
done();
});
});
it("should return a 422 (invalid render type)", function(done) {
request.get("http://localhost:3000/renders/side/Jake0oo0", function(error, res, body) {
assert.equal(422, res.statusCode);
done();
});
});
// testing all paths for valid inputs
var locations = ["avatars", "skins", "renders/head"]
for (var l in locations) {
var location = locations[l];
(function(location) {
it("should return a 200 (valid input " + location + ")", function(done) {
request.get("http://localhost:3000/" + location + "/Jake0oo0", function(error, res, body) {
assert.equal(200, res.statusCode);
done();
});
})
it("should return a 422 (invalid id " + location + ")", function(done) {
request.get("http://localhost:3000/" + location + "/thisisaninvaliduuid", function(error, res, body) {
assert.equal(422, res.statusCode);
done();
});
});
})(location);
}
// testing all paths for invalid id formats
var locations = ["avatars", "capes", "skins", "renders/head"]
for (var l in locations) {
var location = locations[l];
(function(location) {
it("should return a 422 (invalid id " + location + ")", function(done) {
request.get("http://localhost:3000/" + location + "/thisisaninvaliduuid", function(error, res, body) {
assert.equal(422, res.statusCode);
done();
});
});
})(location);
}
//testing all paths for default images
locations = ["avatars", "skins", "renders/head"]
for (var l in locations) {
var location = locations[l];
(function(location) {
it("should return a 404 (default steve image " + location + ")", function(done) {
request.get("http://localhost:3000/" + location + "/invalidjsvns?default=steve", function(error, res, body) {
assert.equal(404, res.statusCode);
done();
});
});
it("should return a 200 (default external image " + location + ")", function(done) {
request.get("http://localhost:3000/" + location + "/invalidjsvns?default=https%3A%2F%2Fi.imgur.com%2FocJVWAc.png", function(error, res, body) {
assert.equal(200, res.statusCode);
done();
});
});
})(location);
}
after(function(done) {
server.close(function() {
done();
})
});
});
// we have to make sure that we test both a 32x64 and 64x64 skin // we have to make sure that we test both a 32x64 and 64x64 skin
describe("Networking: Render", function() { describe("Networking: Render", function() {
it("should not fail (username, 32x64 skin)", function(done) { it("should not fail (username, 32x64 skin)", function(done) {