larra/tests/message.cpp

88 lines
2.9 KiB
C++
Raw Permalink Normal View History

#include <gtest/gtest.h>
#include <larra/jid.hpp>
#include <larra/message.hpp>
namespace larra::xmpp {
namespace {
auto CreateTestData() {
auto doc = std::make_unique<xmlpp::Document>();
auto node = doc->create_root_node("message");
node->set_attribute("from", "user1@server.i2p");
node->set_attribute("to", "user2@server.i2p");
node->set_attribute("type", "chat");
node->set_attribute("id", "1");
node->set_attribute("lang", "en", "xml");
auto bodyNode = node->add_child_element("body");
bodyNode->add_child_text("hello");
return doc;
}
auto CreateErrorTestData() {
auto doc = std::make_unique<xmlpp::Document>();
auto node = doc->create_root_node("message");
node->set_attribute("from", "user1@server.i2p");
node->set_attribute("to", "user2@server.i2p");
node->set_attribute("type", "error");
auto error = node->add_child_element("error");
error->set_attribute("type", "auth");
auto notAuthorized = error->add_child_element("not-authorized");
notAuthorized->set_namespace_declaration("urn:ietf:params:xml:ns:xmpp-stanzas");
return doc;
}
const message::Message<BareJid, BareJid> kMessage{
.from = {.username = "user1", .server = "server.i2p"},
.to = {.username = "user2", .server = "server.i2p"},
.type = message::type::kChat,
.id = "1",
.language = "en",
.body = {{.content = "hello"}} //
};
const message::Error<BareJid, BareJid> kError{.from = {.username = "user1", .server = "server.i2p"}, //
.to = {.username = "user2", .server = "server.i2p"},
.error = stanza::error::NotAuthorized{}};
} // namespace
TEST(Parse, Message) {
using T = Message<BareJid, BareJid>;
{
auto doc = CreateTestData();
auto node = doc->get_root_node();
auto message = Serialization<T>::Parse(node);
EXPECT_EQ(message, static_cast<T>(kMessage));
}
auto doc = CreateErrorTestData();
auto node = doc->get_root_node();
auto message = Serialization<T>::Parse(node);
EXPECT_EQ(message, static_cast<T>(kError));
}
TEST(Serialize, Message) {
{
auto expected =
R"(<?xml version="1.0" encoding="UTF-8"?>
<message from="user1@server.i2p" to="user2@server.i2p" type="chat" id="1" xml:lang="en"><body>hello</body></message>
)";
xmlpp::Document doc;
auto node = doc.create_root_node("message");
Serialization<message::Message<BareJid, BareJid>>::Serialize(node, kMessage);
EXPECT_EQ(doc.write_to_string(), expected);
}
auto expected = R"(<?xml version="1.0" encoding="UTF-8"?>
<message type="error" from="user1@server.i2p" to="user2@server.i2p"><error type="auth"><not-authorized xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></message>
)";
xmlpp::Document doc;
auto node = doc.create_root_node("message");
Serialization<message::Error<BareJid, BareJid>>::Serialize(node, kError);
EXPECT_EQ(doc.write_to_string(), expected);
}
} // namespace larra::xmpp