Skip to main content

epp_client/host/
check.rs

1//! Types for EPP host check request
2
3use std::fmt::Debug;
4
5use super::XMLNS;
6use crate::common::{CheckResponse, NoExtension, StringValue};
7use crate::request::{Command, Transaction};
8use serde::Serialize;
9
10impl<'a> Transaction<NoExtension> for HostCheck<'a> {}
11
12impl<'a> Command for HostCheck<'a> {
13    type Response = CheckResponse;
14    const COMMAND: &'static str = "check";
15}
16
17// Request
18
19/// Type for data under the host &lt;check&gt; tag
20#[derive(Serialize, Debug)]
21struct HostList<'a> {
22    /// XML namespace for host commands
23    #[serde(rename = "xmlns:host")]
24    xmlns: &'a str,
25    /// List of hosts to be checked for availability
26    #[serde(rename = "host:name")]
27    hosts: Vec<StringValue<'a>>,
28}
29
30#[derive(Serialize, Debug)]
31/// Type for EPP XML &lt;check&gt; command for hosts
32struct SerializeHostCheck<'a> {
33    /// The instance holding the list of hosts to be checked
34    #[serde(rename = "host:check")]
35    list: HostList<'a>,
36}
37
38impl<'a> From<HostCheck<'a>> for SerializeHostCheck<'a> {
39    fn from(check: HostCheck<'a>) -> Self {
40        Self {
41            list: HostList {
42                xmlns: XMLNS,
43                hosts: check.hosts.iter().map(|&id| id.into()).collect(),
44            },
45        }
46    }
47}
48
49/// The EPP `check` command for hosts
50#[derive(Clone, Debug, Serialize)]
51#[serde(into = "SerializeHostCheck")]
52pub struct HostCheck<'a> {
53    /// The list of hosts to be checked
54    pub hosts: &'a [&'a str],
55}
56
57#[cfg(test)]
58mod tests {
59    use super::HostCheck;
60    use crate::response::ResultCode;
61    use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};
62
63    #[test]
64    fn command() {
65        let object = HostCheck {
66            hosts: &["ns1.eppdev-1.com", "host1.eppdev-1.com"],
67        };
68        assert_serialized("request/host/check.xml", &object);
69    }
70
71    #[test]
72    fn response() {
73        let object = response_from_file::<HostCheck>("response/host/check.xml");
74        let result = object.res_data().unwrap();
75
76        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
77        assert_eq!(object.result.message, SUCCESS_MSG.into());
78        assert_eq!(result.list[0].id, "host1.eppdev-1.com");
79        assert!(result.list[0].available);
80        assert_eq!(result.list[1].id, "ns1.testing.com");
81        assert!(!result.list[1].available);
82        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into());
83        assert_eq!(object.tr_ids.server_tr_id, SVTRID.into());
84    }
85}