summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/xmpp/test/test_xmppXml.js
blob: 1b6ea9a175b226624c6c26dd4ee9d90ec00534f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

var { Stanza } = ChromeUtils.importESModule(
  "resource:///modules/xmpp-xml.sys.mjs"
);

var TEST_DATA = [
  {
    input: {
      name: "message",
      namespace: Stanza.NS.client,
      attributes: {
        jid: "user@domain",
        type: null,
      },
      data: [],
    },
    XmlOutput: '<message xmlns="jabber:client" jid="user@domain"/>',
    stringOutput: '<message xmlns="jabber:client" jid="user@domain"/>\n',
    isError: false,
    description: "Ignore attribute with null value",
  },
  {
    input: {
      name: "message",
      namespace: Stanza.NS.client,
      attributes: {
        jid: "user@domain",
        type: undefined,
      },
      data: [],
    },
    XmlOutput: '<message xmlns="jabber:client" jid="user@domain"/>',
    stringOutput: '<message xmlns="jabber:client" jid="user@domain"/>\n',
    isError: false,
    description: "Ignore attribute with undefined value",
  },
  {
    input: {
      name: "message",
      namespace: undefined,
      attributes: {},
      data: [],
    },
    XmlOutput: "<message/>",
    stringOutput: "<message/>\n",
    isError: false,
    description: "Ignore namespace with undefined value",
  },
  {
    input: {
      name: undefined,
      attributes: {},
      data: [],
    },
    XmlOutput: "",
    stringOutput: "",
    isError: true,
    description: "Node must have a name",
  },
  {
    input: {
      name: "message",
      attributes: {},
      data: "test message",
    },
    XmlOutput: "<message>test message</message>",
    stringOutput: "<message>\n test message\n</message>\n",
    isError: false,
    description: "Node with text content",
  },
];

function testXMLNode() {
  for (let current of TEST_DATA) {
    try {
      let result = Stanza.node(
        current.input.name,
        current.input.namespace,
        current.input.attributes,
        current.input.data
      );
      equal(result.getXML(), current.XmlOutput, current.description);
      equal(
        result.convertToString(),
        current.stringOutput,
        current.description
      );
      equal(current.isError, false);
    } catch (e) {
      equal(current.isError, true, current.description);
    }
  }

  run_next_test();
}

function run_test() {
  add_test(testXMLNode);

  run_next_test();
}