better error reporting. add test

This commit is contained in:
Andrew Kelley 2013-01-26 22:46:47 -05:00
parent 2434b49528
commit 035f69919d
2 changed files with 46 additions and 6 deletions

View file

@ -143,14 +143,23 @@ function createServer(options) {
user: client.username,
serverId: digest
});
request.end(function (err,resp){
request.end(function(err, resp) {
var myErr;
if (err) {
client.end('Error :', err);
return;
}
if (resp.text !== "YES") {
server.emit('error', err);
client.end('InternalError');
} else if (resp.serverError) {
myErr = new Error("session.minecraft.net is broken: " + resp.status);
myErr.code = 'EMCSESSION500';
server.emit('error', myErr);
client.end('McSessionDown');
} else if (resp.serverError) {
myErr = new Error("session.minecraft.net rejected request: " + resp.status);
myErr.code = 'EMCSESSION400';
server.emit('error', myErr);
client.end('McSessionRejectedAuthRequest');
} else if (resp.text !== "YES") {
client.end('FailedToVerifyUsername');
return;
}
});
}

View file

@ -525,6 +525,37 @@ describe("mc-server", function() {
}
}
});
it("kicks clients when invalid credentials", function(done) {
var server = mc.createServer();
var count = 5;
server.on('connection', function(client) {
client.on('end', function(reason) {
resolve();
server.close();
});
});
server.on('close', function() {
resolve();
});
server.on('listening', function() {
resolve();
var client = mc.createClient({
username: 'lalalal',
password: "this is wrong",
});
client.on('error', function(err) {
assert.strictEqual(err.code, 'ELOGIN400');
resolve();
});
client.on('end', function() {
resolve();
});
});
function resolve() {
count -= 1;
if (count <= 0) done();
}
});
it("gives correct reason for kicking clients when shutting down", function(done) {
var server = mc.createServer({ 'online-mode': false, });
var count = 2;