|
@@ -2,6 +2,7 @@
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
#include "../include/JsonCommander.h"
|
|
|
+#include "../include/base64.h"
|
|
|
#include "FileManagerMock.h"
|
|
|
|
|
|
namespace {
|
|
@@ -871,4 +872,102 @@ TEST(Listdata, InvalidRequest) {
|
|
|
EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
|
|
|
}
|
|
|
|
|
|
+TEST(Head, Positive) {
|
|
|
+ FileManagerMock fileManager;
|
|
|
+
|
|
|
+ JsonCommander jsonCommander(fileManager);
|
|
|
+
|
|
|
+ const std::string command = "head";
|
|
|
+ const std::string file = "asdf.txt";
|
|
|
+
|
|
|
+ Json::Value message;
|
|
|
+ message["command"] = command;
|
|
|
+ message["file"] = file;
|
|
|
+
|
|
|
+ std::vector<char> bytes = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
|
|
|
+ const std::string bytesAsString = base64::encodeVector(bytes);
|
|
|
+ EXPECT_CALL(fileManager, getBytesFromFile(testing::_, testing::_)).WillOnce(testing::Return(std::make_pair(bytes, FileManager::headError::no_error)));
|
|
|
+
|
|
|
+ JsonCommander::Response response = jsonCommander.execute(message);
|
|
|
+
|
|
|
+ EXPECT_TRUE(response.action == JsonCommander::Action::send);
|
|
|
+ EXPECT_EQ(response.json["command"].asString(), command);
|
|
|
+ EXPECT_TRUE(response.json["accept"].asBool());
|
|
|
+ EXPECT_EQ(response.json["file"].asString(), file);
|
|
|
+ EXPECT_EQ(response.json["data"].asString(), bytesAsString);
|
|
|
+ EXPECT_EQ(response.json["error"].asString(), "");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(Head, InvalidRequest) {
|
|
|
+ FileManagerMock fileManager;
|
|
|
+
|
|
|
+ JsonCommander jsonCommander(fileManager);
|
|
|
+
|
|
|
+ const std::string command = "head";
|
|
|
+ const int file = 3641;
|
|
|
+
|
|
|
+ Json::Value message;
|
|
|
+ message["command"] = command;
|
|
|
+ message["file"] = file;
|
|
|
+
|
|
|
+ JsonCommander::Response response = jsonCommander.execute(message);
|
|
|
+
|
|
|
+ EXPECT_TRUE(response.action == JsonCommander::Action::send);
|
|
|
+ EXPECT_EQ(response.json["command"].asString(), command);
|
|
|
+ EXPECT_FALSE(response.json["accept"].asBool());
|
|
|
+ EXPECT_EQ(response.json["file"].asString(), "");
|
|
|
+ EXPECT_EQ(response.json["data"].asString(), "");
|
|
|
+ EXPECT_NE(response.json["error"].asString(), "");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(Head, NoSuchFile) {
|
|
|
+ FileManagerMock fileManager;
|
|
|
+
|
|
|
+ JsonCommander jsonCommander(fileManager);
|
|
|
+
|
|
|
+ const std::string command = "head";
|
|
|
+ const std::string file = "asdf.txt";
|
|
|
+
|
|
|
+ Json::Value message;
|
|
|
+ message["command"] = command;
|
|
|
+ message["file"] = file;
|
|
|
+
|
|
|
+ std::vector<char> bytes;
|
|
|
+ EXPECT_CALL(fileManager, getBytesFromFile(testing::_, testing::_)).WillOnce(testing::Return(std::make_pair(bytes, FileManager::headError::no_such_file)));
|
|
|
+
|
|
|
+ JsonCommander::Response response = jsonCommander.execute(message);
|
|
|
+
|
|
|
+ EXPECT_TRUE(response.action == JsonCommander::Action::send);
|
|
|
+ EXPECT_EQ(response.json["command"].asString(), command);
|
|
|
+ EXPECT_FALSE(response.json["accept"].asBool());
|
|
|
+ EXPECT_EQ(response.json["file"].asString(), file);
|
|
|
+ EXPECT_EQ(response.json["data"].asString(), "");
|
|
|
+ EXPECT_NE(response.json["error"].asString(), "");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(Head, FileTooSmall) {
|
|
|
+ FileManagerMock fileManager;
|
|
|
+
|
|
|
+ JsonCommander jsonCommander(fileManager);
|
|
|
+
|
|
|
+ const std::string command = "head";
|
|
|
+ const std::string file = "asdf.txt";
|
|
|
+
|
|
|
+ Json::Value message;
|
|
|
+ message["command"] = command;
|
|
|
+ message["file"] = file;
|
|
|
+
|
|
|
+ std::vector<char> bytes;
|
|
|
+ EXPECT_CALL(fileManager, getBytesFromFile(testing::_, testing::_)).WillOnce(testing::Return(std::make_pair(bytes, FileManager::headError::file_too_small)));
|
|
|
+
|
|
|
+ JsonCommander::Response response = jsonCommander.execute(message);
|
|
|
+
|
|
|
+ EXPECT_TRUE(response.action == JsonCommander::Action::send);
|
|
|
+ EXPECT_EQ(response.json["command"].asString(), command);
|
|
|
+ EXPECT_FALSE(response.json["accept"].asBool());
|
|
|
+ EXPECT_EQ(response.json["file"].asString(), file);
|
|
|
+ EXPECT_EQ(response.json["data"].asString(), "");
|
|
|
+ EXPECT_NE(response.json["error"].asString(), "");
|
|
|
+}
|
|
|
+
|
|
|
} // namespace
|