This commit is contained in:
parent
b3a4757b2f
commit
fe5b652240
3 changed files with 30 additions and 9 deletions
|
@ -157,7 +157,11 @@ struct Error {
|
||||||
if(!attr || attr->get_value() != "error") {
|
if(!attr || attr->get_value() != "error") {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
return serialization::TryParse<Error>(element);
|
try {
|
||||||
|
return serialization::Parse<Error>(element);
|
||||||
|
} catch(...) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
friend auto operator<<(xmlpp::Element* element, const Error& message) -> void {
|
friend auto operator<<(xmlpp::Element* element, const Error& message) -> void {
|
||||||
element->set_attribute("type", "error");
|
element->set_attribute("type", "error");
|
||||||
|
|
|
@ -37,10 +37,7 @@ constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T
|
||||||
template <typename T>
|
template <typename T>
|
||||||
constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T;
|
constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T;
|
||||||
|
|
||||||
template <typename T>
|
// TODO(sha512sum): Add TryParse
|
||||||
constexpr auto TryParse(xmlpp::Element* element, Tag<T> = {}) -> std::optional<T> {
|
|
||||||
return Serialization<std::optional<T>>::Parse(element);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
constexpr auto Serialize(xmlpp::Element* node, const T& element) -> void
|
constexpr auto Serialize(xmlpp::Element* node, const T& element) -> void
|
||||||
|
|
|
@ -21,6 +21,19 @@ auto CreateTestData() {
|
||||||
return doc;
|
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{
|
const message::Message<BareJid, BareJid> kMessage{
|
||||||
.from = {.username = "user1", .server = "server.i2p"},
|
.from = {.username = "user1", .server = "server.i2p"},
|
||||||
.to = {.username = "user2", .server = "server.i2p"},
|
.to = {.username = "user2", .server = "server.i2p"},
|
||||||
|
@ -37,11 +50,18 @@ const message::Error<BareJid, BareJid> kError{.from = {.username = "user1", .ser
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
TEST(Parse, Message) {
|
TEST(Parse, Message) {
|
||||||
auto doc = CreateTestData();
|
using T = Message<BareJid, BareJid>;
|
||||||
auto node = doc->get_root_node();
|
{
|
||||||
auto message = Serialization<message::Message<BareJid, BareJid>>::Parse(node);
|
auto doc = CreateTestData();
|
||||||
|
auto node = doc->get_root_node();
|
||||||
|
auto message = Serialization<T>::Parse(node);
|
||||||
|
EXPECT_EQ(message, static_cast<T>(kMessage));
|
||||||
|
}
|
||||||
|
|
||||||
EXPECT_EQ(message, 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) {
|
TEST(Serialize, Message) {
|
||||||
|
|
Loading…
Reference in a new issue