1
I need to develop automated integration tests in an application whose features have external dependencies like FTP. I wonder if it is possible to simulate an FTP as well as to simulate a database with Moq, for example.
So far, in my searches I only found frameworks and Nuget packages that create FTP servers, but it is not my goal at the moment. I wanted to resort to this only if it is not possible to do a simulation for the tests.
For now, I have created a class that communicates with an existing FTP server.
public class FTPComunicacao {
private void DownloadArquivo(string ftpDiretorioPath, string username, string senha, string destinoLocalArquivoPath) { int bytesRead = 0; byte[] buffer = new byte[2048];
FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(new Uri(ftpDiretorioPath));
ftp.Method = WebRequestMethods.Ftp.DownloadFile;
Stream reader = ftp.GetResponse().GetResponseStream();
FileStream fileStream = new FileStream(destinoLocalArquivoPath, FileMode.Create);
while (true)
{
bytesRead = reader.Read(buffer, 0, buffer.Length);
if(bytesRead == 0)
{
break;
}
fileStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();
}
private FtpWebRequest CriaRequisicaoFtp(string ftpDiretorioPath, string username, string senha, bool keepAlive = false)
{
FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(new Uri(ftpDiretorioPath));
//Setar o proxy como nulo porque se não, o proxy usado vai receber uma resposta html do monitoramento firewall
ftp.Proxy = null;
ftp.UsePassive = true;
ftp.UseBinary = true;
ftp.KeepAlive = keepAlive;
ftp.Credentials = new NetworkCredential(username, senha);
return ftp;
}
}