Browse Source

finished command_quit command tests

feature/command_quit
Wayne 5 years ago
parent
commit
53f75858bb
  1. 20
      src/server.js
  2. 11
      src/user.js
  3. 5
      test/command_quit.js

20
src/server.js

@ -21,7 +21,7 @@ server.create = function create() {
let splitted = data.toString("ascii").split("\r\n")[0].split(" ")
let tokenized = []
let lastParam = -1
for(let i = 0; i < splitted.length; i++) {
for (let i = 0; i < splitted.length; i++) {
if (lastParam > 0) {
tokenized[lastParam] = tokenized[lastParam] + " " + splitted[i]
} else if (splitted[i].charAt(0) == ":" && i > 0) {
@ -63,7 +63,8 @@ server.create = function create() {
socket.write(`001 ${user.nickname} :Welcome to the example IRC Project ${user.nickname}!~${user.username}@${address}`)
break;
case "QUIT":
server.closeConnection(user)
server.closeConnection(user.nickname)
break;
default:
console.error(`Unknown command: ${command}`);
}
@ -75,21 +76,14 @@ server.create = function create() {
throw err;
});
server.getUserlist = function() {
server.getUserlist = function () {
return userlist
}
server.getUserObjectFromNickName = function (nickname) {
user_list = server.getUserlist()
if (nickname in user_list)
return user_list[nickname]
}
server.closeConnection = function (user_nickname) {
console.log(user_nickname)
userObject = server.getUserObjectFromNickName(user_nickname)
userObject.closeConnection()
delete server.getUserlist()[user_nickname]
let user = userlist[user_nickname]
user.closeConnection()
delete userlist[user_nickname];
}
return server

11
src/user.js

@ -29,19 +29,10 @@ function User(socket) {
this.realname = realname
}
this.quit = function (quit_message) {
if (quit_message === undefined)
this.sendMessage("QUIT")
else this.sendMessage(("QUIT :" + quit_message))
}
this.closeConnection = function () {
socket.close()
socket.destroy()
}
this.sendMessage = function (message) {
connection.write(message)
}
}

5
test/command_quit.js

@ -15,8 +15,11 @@ describe("QUIT OK", function () {
server.emit('connection', mockedSock)
mockedSock.emit('data', Buffer.from("NICK nick" + i + "\r\n", "ascii"))
mockedSock.destroy = function () {
done()
}
console.log(server.getUserlist())
}
server.getUserlist()["nick0"].connection.emit('data', Buffer.from("QUIT :Gone to have lunch\r\n", "ascii"))
})
})
Loading…
Cancel
Save