larra/tests/stream.cpp

53 lines
1.9 KiB
C++
Raw Permalink Normal View History

2024-08-30 13:01:35 +00:00
#include <gtest/gtest.h>
#include <larra/stream.hpp>
namespace larra::xmpp {
constexpr std::string_view kSerializedData =
R"(<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' xml:lang='en' version='1.0' id='abc' from='user@example.com' />
)";
constexpr std::string_view kCheckSerializeData =
2024-09-28 19:15:31 +00:00
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<stream:stream xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" "
"from=\"user@example.com\" to=\"example.com\" id=\"abc\" version=\"1.0\" xml:lang=\"en\"/>\n";
2024-08-30 13:01:35 +00:00
TEST(Stream, Serialize) {
2024-09-03 15:36:08 +00:00
UserStream originalStream;
2024-09-28 19:15:31 +00:00
originalStream.from = BareJid{.username = "user", .server = "example.com"};
2024-09-03 15:36:08 +00:00
originalStream.to = "example.com";
2024-08-30 13:01:35 +00:00
originalStream.id = "abc";
originalStream.version = "1.0";
originalStream.xmlLang = "en";
2024-09-28 19:15:31 +00:00
xmlpp::Document doc;
auto streamNode = doc.create_root_node(UserStream::kDefaultName, UserStream::kDefaultNamespace, UserStream::kDefaultPrefix);
2024-08-30 13:01:35 +00:00
streamNode << originalStream;
2024-09-28 19:15:31 +00:00
const std::string serializedData = doc.write_to_string();
2024-08-30 13:01:35 +00:00
ASSERT_EQ(serializedData, kCheckSerializeData);
}
TEST(Stream, Deserialize) {
2024-09-28 19:15:31 +00:00
xmlpp::DomParser parser;
parser.parse_memory(static_cast<std::string>(kSerializedData));
auto parsedDoc = parser.get_document();
2024-08-30 13:01:35 +00:00
2024-09-28 19:15:31 +00:00
const UserStream deserializedStream = UserStream::Parse(parsedDoc->get_root_node());
2024-08-30 13:01:35 +00:00
ASSERT_TRUE(deserializedStream.from.has_value());
ASSERT_EQ(ToString(*deserializedStream.from), "user@example.com");
ASSERT_FALSE(deserializedStream.to.has_value());
ASSERT_TRUE(deserializedStream.id.has_value());
ASSERT_EQ(deserializedStream.id.value(), "abc");
ASSERT_TRUE(deserializedStream.version.has_value());
ASSERT_EQ(deserializedStream.version.value(), "1.0");
ASSERT_TRUE(deserializedStream.xmlLang.has_value());
ASSERT_EQ(deserializedStream.xmlLang.value(), "en");
}
} // namespace larra::xmpp