Skip to content

Commit

Permalink
Adjust unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickjuchli committed Oct 27, 2019
1 parent 9cb635d commit 175fe0d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
7 changes: 4 additions & 3 deletions test/SocketMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module.exports = class SocketMock extends EventEmitter {
connect() {
this.remoteAddress = "somewhere"
}
setEncoding() {
setEncoding(encoding) {
return this
}
removeAllListeners() {
return this
Expand All @@ -32,10 +33,10 @@ module.exports = class SocketMock extends EventEmitter {
this.emit("didSend", buf);
}
end() {
this.emit("end");
this.emit("data", null);
this.destroyed = true;
}
pipe(target) {
this.on("data", chunk => target.write(chunk));
this.on("data", chunk => chunk ? target.write(chunk) : target.end());
}
};
33 changes: 17 additions & 16 deletions test/downloadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ describe("Download directory listing", function() {
client.prepareTransfer = ftp => {
//@ts-ignore that SocketMock can't be assigned to client.ftp
ftp.dataSocket = new SocketMock();
//@ts-ignore
ftp.dataSocket.connect()
return Promise.resolve({code: 200, message: "OK"});
};
//@ts-ignore
Expand All @@ -38,75 +40,74 @@ describe("Download directory listing", function() {
function sendCompleteList() {
client.ftp.socket.emit("data", "125 Sending");
client.ftp.dataSocket.emit("data", bufList);
client.ftp.dataSocket.end();
client.ftp.dataSocket.end()
client.ftp.socket.emit("data", "250 Done");
}

function requestListAndVerify(done) {
client.list().then(result => {
function requestListAndVerify() {
return client.list().then(result => {
assert.deepEqual(result, expList);
done();
});
}

it("handles data socket ending before control confirms", function(done) {
requestListAndVerify(done);
it("handles data socket ending before control confirms", function() {
setTimeout(() => {
client.ftp.socket.emit("data", "125 Sending");
client.ftp.dataSocket.emit("data", bufList);
client.ftp.dataSocket.end();
client.ftp.socket.emit("data", "250 Done");
});
return requestListAndVerify();
});

it("handles control confirming before data socket ends", function(done) {
requestListAndVerify(done);
it("handles control confirming before data socket ends", function() {
setTimeout(() => {
client.ftp.socket.emit("data", "125 Sending");
client.ftp.socket.emit("data", "250 Done");
client.ftp.dataSocket.emit("data", bufList);
client.ftp.dataSocket.end();
});
return requestListAndVerify();
});

it("handles data coming in before control announces beginning", function(done) {
requestListAndVerify(done);
it("handles data coming in before control announces beginning", function() {
setTimeout(() => {
client.ftp.dataSocket.emit("data", bufList);
client.ftp.socket.emit("data", "125 Sending");
client.ftp.dataSocket.end();
client.ftp.socket.emit("data", "250 Done");
});
return requestListAndVerify();
});

it("handles data transmission being complete before control announces beginning", function(done) {
requestListAndVerify(done);
it("handles data transmission being complete before control announces beginning", function() {
setTimeout(() => {
client.ftp.dataSocket.emit("data", bufList);
client.ftp.dataSocket.end();
client.ftp.socket.emit("data", "125 Sending");
client.ftp.socket.emit("data", "250 Done");
});
return requestListAndVerify();
});

it("handles control announcing with 150 instead of 125", function(done) {
requestListAndVerify(done);
it("handles control announcing with 150 instead of 125", function() {
setTimeout(() => {
client.ftp.socket.emit("data", "150 Sending");
client.ftp.dataSocket.emit("data", bufList);
client.ftp.dataSocket.end();
client.ftp.socket.emit("data", "250 Done");
});
return requestListAndVerify();
});

it("handles control confirming end with 200 instead of 250", function(done) {
requestListAndVerify(done);
it("handles control confirming end with 200 instead of 250", function() {
setTimeout(() => {
client.ftp.socket.emit("data", "125 Sending");
client.ftp.dataSocket.emit("data", bufList);
client.ftp.dataSocket.end();
client.ftp.socket.emit("data", "200 Done");
});
return requestListAndVerify();
});

it("relays FTP error response even if data transmitted completely", function() {
Expand Down
2 changes: 2 additions & 0 deletions test/fileTransferSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ describe("Download to file", function() {
client.prepareTransfer = ftp => {
//@ts-ignore that SocketMock can't be assigned to client.ftp
ftp.dataSocket = new SocketMock();
//@ts-ignore
ftp.dataSocket.connect()
return Promise.resolve({code: 200, message: "OK"});
};
//@ts-ignore
Expand Down
2 changes: 2 additions & 0 deletions test/uploadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe("Upload", function() {
client.prepareTransfer = () => Promise.resolve({code: 200, message: "ok"}); // Don't change
client.ftp.socket = new SocketMock();
client.ftp.dataSocket = new SocketMock();
//@ts-ignore
client.ftp.dataSocket.connect()
});

afterEach(function() {
Expand Down

0 comments on commit 175fe0d

Please sign in to comment.