Skip to content

Commit

Permalink
feat: provide parsed file list
Browse files Browse the repository at this point in the history
  • Loading branch information
deniskovalchuk committed Sep 22, 2023
1 parent 3a135c1 commit be8788b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 52 deletions.
12 changes: 9 additions & 3 deletions include/ftp/file_list_reply.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <ftp/replies.hpp>
#include <string>
#include <vector>

namespace ftp
{
Expand All @@ -36,12 +37,17 @@ class file_list_reply : public replies
public:
file_list_reply();

file_list_reply(const replies & replies, const std::string & file_list);
file_list_reply(const replies & replies, const std::string & file_list_str);

[[nodiscard]] const std::string & get_file_list() const;
[[nodiscard]] const std::string & get_file_list_str() const;

[[nodiscard]] const std::vector<std::string> & get_file_list() const;

private:
std::string file_list_;
static std::vector<std::string> parse_file_list(const std::string & file_list_str);

std::string file_list_str_;
std::vector<std::string> file_list_;
};

} // namespace ftp
Expand Down
30 changes: 26 additions & 4 deletions src/file_list_reply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

#include <ftp/file_list_reply.hpp>
#include <sstream>

namespace ftp
{
Expand All @@ -31,14 +32,35 @@ file_list_reply::file_list_reply()
: ftp::replies()
{}

file_list_reply::file_list_reply(const replies & replies, const std::string & file_list)
file_list_reply::file_list_reply(const replies & replies, const std::string & file_list_str)
: ftp::replies(replies),
file_list_(file_list)
{}
file_list_str_(file_list_str)
{
file_list_ = parse_file_list(file_list_str);
}

const std::string & file_list_reply::get_file_list_str() const
{
return file_list_str_;
}

const std::string & file_list_reply::get_file_list() const
const std::vector<std::string> & file_list_reply::get_file_list() const
{
return file_list_;
}

std::vector<std::string> file_list_reply::parse_file_list(const std::string & file_list_str)
{
std::vector<std::string> file_list;

std::istringstream iss(file_list_str);
std::string line;
while (std::getline(iss, line))
{
file_list.push_back(line);
}

return file_list;
}

} // namespace ftp
61 changes: 16 additions & 45 deletions test/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <boost/process.hpp>
#include <filesystem>
#include <memory>
Expand All @@ -37,6 +38,7 @@ namespace
{

using namespace ftp::test;
using testing::ElementsAre;

class test_observer : public ftp::observer
{
Expand Down Expand Up @@ -68,37 +70,6 @@ class test_observer : public ftp::observer
std::string status_strings_;
};

class test_file_list_observer : public ftp::observer
{
public:
[[nodiscard]] const std::string & get_file_list()
{
return file_list_;
}

[[nodiscard]] std::size_t get_file_count()
{
return file_count_;
}

private:
void on_file_list(std::string_view file_list) override
{
file_list_ = file_list;
file_count_ = 0;

std::istringstream iss(file_list_);
std::string file;
while (std::getline(iss, file))
{
file_count_++;
}
}

std::string file_list_;
std::size_t file_count_;
};

class test_cancel_callback : public ftp::transfer_callback
{
public:
Expand Down Expand Up @@ -795,36 +766,34 @@ TEST_F(client, append_file)

TEST_F(client, get_file_list_only_names)
{
std::shared_ptr<test_file_list_observer> observer = std::make_shared<test_file_list_observer>();
ftp::client client;

client.add_observer(observer);

check_reply(client.connect("localhost", 2121), "220 FTP server is ready.");

check_reply(client.login("user", "password"), CRLF("331 Username ok, send password.",
"230 Login successful.",
"200 Type set to: Binary."));

check_last_reply(client.get_file_list(".", true), "226 Transfer complete.");
ASSERT_TRUE(observer->get_file_list().empty());
client.set_transfer_type(ftp::transfer_type::ascii);

ftp::file_list_reply reply = client.get_file_list(".", true);
check_last_reply(reply, "226 Transfer complete.");
ASSERT_THAT(reply.get_file_list(), ElementsAre());

check_reply(client.create_directory("dir1"), R"(257 "/dir1" directory created.)");
check_reply(client.create_directory("dir2"), R"(257 "/dir2" directory created.)");

check_last_reply(client.get_file_list(".", true), "226 Transfer complete.");
ASSERT_EQ(CRLF("dir1", "dir2", ""), observer->get_file_list());
reply = client.get_file_list(".", true);
check_last_reply(reply, "226 Transfer complete.");
ASSERT_THAT(reply.get_file_list(), ElementsAre("dir1", "dir2"));

check_reply(client.disconnect(), "221 Goodbye.");
}

TEST_F(client, upload_unique_file)
{
std::shared_ptr<test_file_list_observer> observer = std::make_shared<test_file_list_observer>();
ftp::client client;

client.add_observer(observer);

check_reply(client.connect("localhost", 2121), "220 FTP server is ready.");

check_reply(client.login("user", "password"), CRLF("331 Username ok, send password.",
Expand All @@ -836,16 +805,18 @@ TEST_F(client, upload_unique_file)
check_last_reply(client.upload_file(ftp::istream_adapter(iss), "file", true), "226 Transfer complete.");
}

check_last_reply(client.get_file_list(".", true), "226 Transfer complete.");
ASSERT_EQ(1, observer->get_file_count());
ftp::file_list_reply reply = client.get_file_list(".", true);
check_last_reply(reply, "226 Transfer complete.");
ASSERT_EQ(1, reply.get_file_list().size());

{
std::istringstream iss("content");
check_last_reply(client.upload_file(ftp::istream_adapter(iss), "file", true), "226 Transfer complete.");
}

check_last_reply(client.get_file_list(".", true), "226 Transfer complete.");
ASSERT_EQ(2, observer->get_file_count());
reply = client.get_file_list(".", true);
check_last_reply(reply, "226 Transfer complete.");
ASSERT_EQ(2, reply.get_file_list().size());

check_reply(client.disconnect(), "221 Goodbye.");
}
Expand Down

0 comments on commit be8788b

Please sign in to comment.