From 6985a6a243acbcf619ecb064873bd11806816c04 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 10 Mar 2022 12:50:09 +0100 Subject: [PATCH] Externalize handling of command serialization --- src/client.rs | 12 ++++----- src/contact/check.rs | 9 ++----- src/contact/create.rs | 10 ++------ src/contact/delete.rs | 11 ++------ src/contact/info.rs | 11 ++------ src/contact/update.rs | 10 ++------ src/domain/check.rs | 11 ++------ src/domain/create.rs | 26 +++---------------- src/domain/delete.rs | 11 ++------ src/domain/info.rs | 11 ++------ src/domain/renew.rs | 11 ++------ src/domain/transfer.rs | 47 +++++------------------------------ src/domain/update.rs | 11 ++------ src/extensions/consolidate.rs | 31 +++++++---------------- src/extensions/namestore.rs | 16 ++++-------- src/extensions/rgp/report.rs | 24 ++++++------------ src/extensions/rgp/request.rs | 17 ++++--------- src/host/check.rs | 11 ++------ src/host/create.rs | 11 ++------ src/host/delete.rs | 11 ++------ src/host/info.rs | 11 ++------ src/host/update.rs | 10 ++------ src/login.rs | 8 ++---- src/logout.rs | 7 ++---- src/message/ack.rs | 9 ++----- src/message/poll.rs | 9 ++----- src/request.rs | 14 +++++------ src/tests/mod.rs | 23 ++++++++++++++++- 28 files changed, 110 insertions(+), 293 deletions(-) diff --git a/src/client.rs b/src/client.rs index fadf637..401b475 100644 --- a/src/client.rs +++ b/src/client.rs @@ -126,11 +126,11 @@ impl EppClient { Ext: Extension + 'e, { let data = data.into(); - let epp_xml = - >::serialize_request(data.command, data.extension, id)?; + let document = >::command(data.command, data.extension, id); + let xml = document.serialize()?; - debug!("{}: request: {}", self.connection.registry, &epp_xml); - let response = self.connection.transact(&epp_xml)?.await?; + debug!("{}: request: {}", self.connection.registry, &xml); + let response = self.connection.transact(&xml)?.await?; debug!("{}: response: {}", self.connection.registry, &response); match Cmd::deserialize_response(&response) { @@ -169,8 +169,8 @@ impl EppClient { #[derive(Debug)] pub struct RequestData<'c, 'e, C, E> { - command: &'c C, - extension: Option<&'e E>, + pub(crate) command: &'c C, + pub(crate) extension: Option<&'e E>, } impl<'c, C: Command> From<&'c C> for RequestData<'c, 'static, C, NoExtension> { diff --git a/src/contact/check.rs b/src/contact/check.rs index c1289d0..234b2c0 100644 --- a/src/contact/check.rs +++ b/src/contact/check.rs @@ -58,19 +58,14 @@ mod tests { use crate::common::NoExtension; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/contact/check.xml").unwrap(); let object = ContactCheck { contact_ids: &["eppdev-contact-1", "eppdev-contact-2"], }; - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/contact/check.xml", &object); } #[test] diff --git a/src/contact/create.rs b/src/contact/create.rs index 9722b9b..275f932 100644 --- a/src/contact/create.rs +++ b/src/contact/create.rs @@ -106,12 +106,10 @@ mod tests { use crate::contact::Address; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/contact/create.xml").unwrap(); - let street = &["58", "Orchid Road"]; let address = Address::new(street, "Paris", "Paris", "392374", "FR".parse().unwrap()); let postal_info = PostalInfo::new("int", "John Doe", "Acme Widgets", address); @@ -129,11 +127,7 @@ mod tests { ); object.set_fax(fax); - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/contact/create.xml", &object); } #[test] diff --git a/src/contact/delete.rs b/src/contact/delete.rs index 1f01e21..fa308a7 100644 --- a/src/contact/delete.rs +++ b/src/contact/delete.rs @@ -48,19 +48,12 @@ mod tests { use crate::common::NoExtension; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/contact/delete.xml").unwrap(); - let object = ContactDelete::new("eppdev-contact-3"); - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/contact/delete.xml", &object); } #[test] diff --git a/src/contact/info.rs b/src/contact/info.rs index c63e503..7fe5662 100644 --- a/src/contact/info.rs +++ b/src/contact/info.rs @@ -110,19 +110,12 @@ mod tests { use crate::common::NoExtension; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/contact/info.xml").unwrap(); - let object = ContactInfo::new("eppdev-contact-3", "eppdev-387323"); - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/contact/info.xml", &object); } #[test] diff --git a/src/contact/update.rs b/src/contact/update.rs index 4d860e6..aab3c87 100644 --- a/src/contact/update.rs +++ b/src/contact/update.rs @@ -112,12 +112,10 @@ mod tests { use crate::contact::Address; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/contact/update.xml").unwrap(); - let mut object = ContactUpdate::new("eppdev-contact-3"); let street = &["58", "Orchid Road"]; @@ -135,11 +133,7 @@ mod tests { }]; object.remove(remove_statuses); - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/contact/update.xml", &object); } #[test] diff --git a/src/domain/check.rs b/src/domain/check.rs index 2bc9430..93952a6 100644 --- a/src/domain/check.rs +++ b/src/domain/check.rs @@ -56,21 +56,14 @@ mod tests { use crate::common::NoExtension; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/domain/check.xml").unwrap(); - let object = DomainCheck { domains: &["eppdev.com", "eppdev.net"], }; - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/domain/check.xml", &object); } #[test] diff --git a/src/domain/create.rs b/src/domain/create.rs index 0ad07e3..f0a1d27 100644 --- a/src/domain/create.rs +++ b/src/domain/create.rs @@ -113,12 +113,10 @@ mod tests { use crate::domain::{HostAttr, HostAttrList, HostObjList}; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/domain/create.xml").unwrap(); - let contacts = &[ DomainContact { contact_type: "admin".into(), @@ -143,17 +141,11 @@ mod tests { Some(contacts), ); - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/domain/create.xml", &object); } #[test] fn command_with_host_obj() { - let xml = get_xml("request/domain/create_with_host_obj.xml").unwrap(); - let contacts = &[ DomainContact { contact_type: "admin".into(), @@ -179,17 +171,11 @@ mod tests { Some(contacts), ); - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/domain/create_with_host_obj.xml", &object); } #[test] fn command_with_host_attr() { - let xml = get_xml("request/domain/create_with_host_attr.xml").unwrap(); - let contacts = &[ DomainContact { contact_type: "admin".into(), @@ -228,11 +214,7 @@ mod tests { Some(contacts), ); - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/domain/create_with_host_attr.xml", &object); } #[test] diff --git a/src/domain/delete.rs b/src/domain/delete.rs index 4c5a31a..dde56d9 100644 --- a/src/domain/delete.rs +++ b/src/domain/delete.rs @@ -48,19 +48,12 @@ mod tests { use crate::common::NoExtension; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/domain/delete.xml").unwrap(); - let object = DomainDelete::new("eppdev.com"); - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/domain/delete.xml", &object); } #[test] diff --git a/src/domain/info.rs b/src/domain/info.rs index 25b92f8..f1961b3 100644 --- a/src/domain/info.rs +++ b/src/domain/info.rs @@ -136,20 +136,13 @@ mod tests { use crate::common::NoExtension; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; use chrono::{TimeZone, Utc}; #[test] fn command() { - let xml = get_xml("request/domain/info.xml").unwrap(); - let object = DomainInfo::new("eppdev.com", Some("2fooBAR")); - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/domain/info.xml", &object); } #[test] diff --git a/src/domain/renew.rs b/src/domain/renew.rs index 107ec63..f8457ec 100644 --- a/src/domain/renew.rs +++ b/src/domain/renew.rs @@ -81,22 +81,15 @@ mod tests { use crate::common::NoExtension; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; use chrono::{NaiveDate, TimeZone, Utc}; #[test] fn command() { - let xml = get_xml("request/domain/renew.xml").unwrap(); - let exp_date = NaiveDate::from_ymd(2022, 7, 23); let object = DomainRenew::new("eppdev.com", exp_date, Period::years(1).unwrap()); - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/domain/renew.xml", &object); } #[test] diff --git a/src/domain/transfer.rs b/src/domain/transfer.rs index a6c28eb..0afc210 100644 --- a/src/domain/transfer.rs +++ b/src/domain/transfer.rs @@ -139,72 +139,37 @@ mod tests { use crate::common::NoExtension; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn request_command() { - let xml = get_xml("request/domain/transfer_request.xml").unwrap(); - let object = DomainTransfer::new("testing.com", Some(Period::years(1).unwrap()), "epP4uthd#v"); - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/domain/transfer_request.xml", &object); } #[test] fn approve_command() { - let xml = get_xml("request/domain/transfer_approve.xml").unwrap(); - let object = DomainTransfer::approve("testing.com"); - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/domain/transfer_approve.xml", &object); } #[test] fn reject_command() { - let xml = get_xml("request/domain/transfer_reject.xml").unwrap(); - let object = DomainTransfer::reject("testing.com"); - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/domain/transfer_reject.xml", &object); } #[test] fn cancel_command() { - let xml = get_xml("request/domain/transfer_cancel.xml").unwrap(); - let object = DomainTransfer::cancel("testing.com"); - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/domain/transfer_cancel.xml", &object); } #[test] fn query_command() { - let xml = get_xml("request/domain/transfer_query.xml").unwrap(); - let object = DomainTransfer::query("testing.com", "epP4uthd#v"); - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/domain/transfer_query.xml", &object); } #[test] diff --git a/src/domain/update.rs b/src/domain/update.rs index 54f8371..a349adb 100644 --- a/src/domain/update.rs +++ b/src/domain/update.rs @@ -105,12 +105,10 @@ mod tests { use crate::common::{NoExtension, ObjectStatus}; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/domain/update.xml").unwrap(); - let mut object = DomainUpdate::new("eppdev.com"); let statuses = &[ObjectStatus { @@ -142,12 +140,7 @@ mod tests { object.add(add); object.remove(remove); object.info(change_info); - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/domain/update.xml", &object); } #[test] diff --git a/src/extensions/consolidate.rs b/src/extensions/consolidate.rs index 14b89f8..1ef54bc 100644 --- a/src/extensions/consolidate.rs +++ b/src/extensions/consolidate.rs @@ -118,13 +118,10 @@ mod tests { use super::{GMonthDay, Update}; use crate::domain::update::{DomainChangeInfo, DomainUpdate}; use crate::extensions::consolidate::UpdateWithNameStore; - use crate::request::Transaction; - use crate::tests::{get_xml, CLTRID}; + use crate::tests::assert_serialized; #[test] fn command() { - let xml = get_xml("request/extensions/consolidate.xml").unwrap(); - let exp = GMonthDay::new(5, 31, None).unwrap(); let consolidate_ext = Update::new(exp); @@ -136,20 +133,14 @@ mod tests { auth_info: None, }); - let serialized = >::serialize_request( - &object, - Some(&consolidate_ext), - CLTRID, - ) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized( + "request/extensions/consolidate.xml", + (&object, &consolidate_ext), + ); } #[test] fn command_with_namestore() { - let xml = get_xml("request/extensions/consolidate_namestore.xml").unwrap(); - let exp = GMonthDay::new(5, 31, None).unwrap(); let consolidate_ext = UpdateWithNameStore::new(exp, "com"); @@ -161,13 +152,9 @@ mod tests { auth_info: None, }); - let serialized = >::serialize_request( - &object, - Some(&consolidate_ext), - CLTRID, - ) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized( + "request/extensions/consolidate_namestore.xml", + (&object, &consolidate_ext), + ); } } diff --git a/src/extensions/namestore.rs b/src/extensions/namestore.rs index 30d500b..5aa0181 100644 --- a/src/extensions/namestore.rs +++ b/src/extensions/namestore.rs @@ -98,26 +98,20 @@ mod tests { use super::NameStore; use crate::domain::check::DomainCheck; use crate::request::Transaction; - use crate::tests::{get_xml, CLTRID}; + use crate::tests::{assert_serialized, get_xml}; #[test] fn command() { - let xml = get_xml("request/extensions/namestore.xml").unwrap(); - let namestore_ext = NameStore::new("com"); let object = DomainCheck { domains: &["example1.com", "example2.com", "example3.com"], }; - let serialized = >::serialize_request( - &object, - Some(&namestore_ext), - CLTRID, - ) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized( + "request/extensions/namestore.xml", + (&object, &namestore_ext), + ); } #[test] diff --git a/src/extensions/rgp/report.rs b/src/extensions/rgp/report.rs index 6b19a0a..0146533 100644 --- a/src/extensions/rgp/report.rs +++ b/src/extensions/rgp/report.rs @@ -104,13 +104,10 @@ mod tests { use super::{RgpRestoreReport, Update}; use crate::domain::update::{DomainChangeInfo, DomainUpdate}; - use crate::request::Transaction; - use crate::tests::{get_xml, CLTRID}; + use crate::tests::assert_serialized; #[test] fn command() { - let xml = get_xml("request/extensions/rgp_restore_report.xml").unwrap(); - let pre_data = "Pre-delete registration data goes here. Both XML and free text are allowed."; let post_data = @@ -119,9 +116,9 @@ mod tests { let restored_at = DateTime::from_str("2021-07-20T22:00:00.0Z").unwrap(); let restore_reason = "Registrant error."; let statements = &[ - "This registrar has not restored the Registered Name in order to assume the rights to use or sell the Registered Name for itself or for any third party.", - "The information in this report is true to best of this registrar's knowledge, and this registrar acknowledges that intentionally supplying false information in this report shall constitute an incurable material breach of the Registry-Registrar Agreement.", - ]; + "This registrar has not restored the Registered Name in order to assume the rights to use or sell the Registered Name for itself or for any third party.", + "The information in this report is true to best of this registrar's knowledge, and this registrar acknowledges that intentionally supplying false information in this report shall constitute an incurable material breach of the Registry-Registrar Agreement.", + ]; let other = "Supporting information goes here."; let domain_restore_report = Update { @@ -142,14 +139,9 @@ mod tests { auth_info: None, }); - let serialized = - >>::serialize_request( - &object, - Some(&domain_restore_report), - CLTRID, - ) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized( + "request/extensions/rgp_restore_report.xml", + (&object, &domain_restore_report), + ); } } diff --git a/src/extensions/rgp/request.rs b/src/extensions/rgp/request.rs index cc3ba78..fd11390 100644 --- a/src/extensions/rgp/request.rs +++ b/src/extensions/rgp/request.rs @@ -72,12 +72,10 @@ mod tests { use crate::domain::update::{DomainChangeInfo, DomainUpdate}; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, SUCCESS_MSG, SVTRID}; #[test] fn request_command() { - let xml = get_xml("request/extensions/rgp_restore_request.xml").unwrap(); - let domain_restore_request = Update { data: RgpRestoreRequest::default(), }; @@ -91,15 +89,10 @@ mod tests { object.info(change_info); - let serialized = - >>::serialize_request( - &object, - Some(&domain_restore_request), - CLTRID, - ) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized( + "request/extensions/rgp_restore_request.xml", + (&object, &domain_restore_request), + ); } #[test] diff --git a/src/host/check.rs b/src/host/check.rs index ab24cbd..bb0cd97 100644 --- a/src/host/check.rs +++ b/src/host/check.rs @@ -60,21 +60,14 @@ mod tests { use crate::common::NoExtension; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/host/check.xml").unwrap(); - let object = HostCheck { hosts: &["ns1.eppdev-1.com", "host1.eppdev-1.com"], }; - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/host/check.xml", &object); } #[test] diff --git a/src/host/create.rs b/src/host/create.rs index 7018d8a..400be0b 100644 --- a/src/host/create.rs +++ b/src/host/create.rs @@ -80,24 +80,17 @@ mod tests { use crate::common::NoExtension; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/host/create.xml").unwrap(); - let addresses = &[ IpAddr::from([29, 245, 122, 14]), IpAddr::from([0x2404, 0x6800, 0x4001, 0x801, 0, 0, 0, 0x200e]), ]; let object = HostCreate::new("host1.eppdev-1.com", Some(addresses)); - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/host/create.xml", &object); } #[test] diff --git a/src/host/delete.rs b/src/host/delete.rs index f4d006c..95d828a 100644 --- a/src/host/delete.rs +++ b/src/host/delete.rs @@ -48,19 +48,12 @@ mod tests { use crate::common::NoExtension; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/host/delete.xml").unwrap(); - let object = HostDelete::new("ns1.eppdev-1.com"); - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/host/delete.xml", &object); } #[test] diff --git a/src/host/info.rs b/src/host/info.rs index ba5bdc2..f76f18c 100644 --- a/src/host/info.rs +++ b/src/host/info.rs @@ -112,19 +112,12 @@ mod tests { use crate::common::NoExtension; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/host/info.xml").unwrap(); - let object = HostInfo::new("ns1.eppdev-1.com"); - - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/host/info.xml", &object); } #[test] diff --git a/src/host/update.rs b/src/host/update.rs index a1e0f5f..bec23e8 100644 --- a/src/host/update.rs +++ b/src/host/update.rs @@ -97,12 +97,10 @@ mod tests { use crate::common::{NoExtension, ObjectStatus}; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/host/update.xml").unwrap(); - let addr = &[IpAddr::from([ 0x2404, 0x6800, 0x4001, 0x801, 0, 0, 0, 0x200e, ])]; @@ -129,11 +127,7 @@ mod tests { name: "host2.eppdev-1.com".into(), }); - let serialized = - >::serialize_request(&object, None, CLTRID) - .unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/host/update.xml", &object); } #[test] diff --git a/src/login.rs b/src/login.rs index fd755f5..96d6713 100644 --- a/src/login.rs +++ b/src/login.rs @@ -69,17 +69,13 @@ mod tests { use super::Login; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SUCCESS_MSG, SVTRID}; #[test] fn command() { let ext_uris = Some(&["http://schema.ispapi.net/epp/xml/keyvalue-1.0"][..]); - - let xml = get_xml("request/login.xml").unwrap(); let object = Login::new("username", "password", ext_uris); - let serialized = object.serialize_request(None, CLTRID).unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/login.xml", &object); } #[test] diff --git a/src/logout.rs b/src/logout.rs index a621e21..30f1571 100644 --- a/src/logout.rs +++ b/src/logout.rs @@ -23,15 +23,12 @@ mod tests { use super::Logout; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SVTRID}; #[test] fn command() { - let xml = get_xml("request/logout.xml").unwrap(); let object = Logout; - let serialized = object.serialize_request(None, CLTRID).unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/logout.xml", &object); } #[test] diff --git a/src/message/ack.rs b/src/message/ack.rs index 465bceb..5ee6a3f 100644 --- a/src/message/ack.rs +++ b/src/message/ack.rs @@ -36,17 +36,12 @@ mod tests { use super::MessageAck; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID}; + use crate::tests::{assert_serialized, get_xml, SUCCESS_MSG, SVTRID}; #[test] fn command() { - let xml = get_xml("request/message/ack.xml").unwrap(); - let object = MessageAck::new(12345); - - let serialized = object.serialize_request(None, CLTRID).unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/message/ack.xml", &object); } #[test] diff --git a/src/message/poll.rs b/src/message/poll.rs index 327ee12..b7a2d6b 100644 --- a/src/message/poll.rs +++ b/src/message/poll.rs @@ -54,20 +54,15 @@ mod tests { use crate::message::poll::MessageData; use crate::request::Transaction; use crate::response::ResultCode; - use crate::tests::{get_xml, CLTRID, SVTRID}; + use crate::tests::{assert_serialized, get_xml, CLTRID, SVTRID}; use chrono::{TimeZone, Utc}; use std::net::IpAddr; #[test] fn command() { - let xml = get_xml("request/message/poll.xml").unwrap(); - let object = MessagePoll::default(); - - let serialized = object.serialize_request(None, CLTRID).unwrap(); - - assert_eq!(xml, serialized); + assert_serialized("request/message/poll.xml", &object); } #[test] diff --git a/src/request.rs b/src/request.rs index 413903f..88cf340 100644 --- a/src/request.rs +++ b/src/request.rs @@ -15,17 +15,17 @@ pub const EPP_LANG: &str = "en"; /// Trait to set correct value for xml tags when tags are being generated from generic types pub trait Transaction: Command + Sized { - fn serialize_request( - &self, - extension: Option<&Ext>, - client_tr_id: &str, - ) -> Result { - as EppXml>::serialize(&CommandDocument::new(CommandWrapper { + fn command<'c, 'e: 'c, 'i: 'c>( + &'c self, + extension: Option<&'e Ext>, + client_tr_id: &'i str, + ) -> CommandDocument<'c, Self, Ext> { + CommandDocument::new(CommandWrapper { command: Self::COMMAND, data: self, extension, client_tr_id: client_tr_id.into(), - })) + }) } fn deserialize_response( diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 6adcde4..203bd45 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,8 +1,15 @@ //! Module for automated tests -use regex::Regex; use std::{error::Error, fs::File, io::Read}; +use regex::Regex; + +use crate::{ + client::RequestData, + request::{Command, Extension, Transaction}, + xml::EppXml, +}; + pub(crate) const RESOURCES_DIR: &str = "./tests/resources"; pub(crate) const CLTRID: &str = "cltrid:1626454866"; pub(crate) const SVTRID: &str = "RO-6879-1627224678242975"; @@ -27,3 +34,17 @@ pub(crate) fn get_xml(path: &str) -> Result> { } Ok(buf) } + +pub(crate) fn assert_serialized<'c, 'e, Cmd, Ext>( + path: &str, + req: impl Into>, +) where + Cmd: Transaction + Command + 'c, + Ext: Extension + 'e, +{ + let expected = get_xml(path).unwrap(); + let req = req.into(); + let document = >::command(req.command, req.extension, CLTRID); + let actual = EppXml::serialize(&document).unwrap(); + assert_eq!(expected, actual); +}