21 t.Fail() |
21 t.Fail() |
22 } |
22 } |
23 } |
23 } |
24 |
24 |
25 func TestJid(t *testing.T) { |
25 func TestJid(t *testing.T) { |
26 str := "user@domain/res" |
26 jid := JID("user@domain/res") |
27 jid := &JID{} |
27 assertEquals(t, "user", jid.Node()) |
28 if err := jid.Set(str); err != nil { |
28 assertEquals(t, "domain", jid.Domain()) |
29 t.Errorf("Set(%s) failed: %s", str, err) |
29 assertEquals(t, "res", jid.Resource()) |
|
30 |
|
31 jid = "domain.tld" |
|
32 if jid.Node() != "" { |
|
33 t.Errorf("Node: %v\n", jid.Node()) |
30 } |
34 } |
31 assertEquals(t, "user", jid.Node) |
35 assertEquals(t, "domain.tld", jid.Domain()) |
32 assertEquals(t, "domain", jid.Domain) |
36 if jid.Resource() != "" { |
33 assertEquals(t, "res", jid.Resource) |
37 t.Errorf("Resource: %v\n", jid.Resource()) |
34 assertEquals(t, str, jid.String()) |
|
35 |
|
36 str = "domain.tld" |
|
37 if err := jid.Set(str); err != nil { |
|
38 t.Errorf("Set(%s) failed: %s", str, err) |
|
39 } |
38 } |
40 if jid.Node != "" { |
|
41 t.Errorf("Node: %v\n", jid.Node) |
|
42 } |
|
43 assertEquals(t, "domain.tld", jid.Domain) |
|
44 if jid.Resource != "" { |
|
45 t.Errorf("Resource: %v\n", jid.Resource) |
|
46 } |
|
47 assertEquals(t, str, jid.String()) |
|
48 } |
39 } |
49 |
40 |
50 func assertMarshal(t *testing.T, expected string, marshal interface{}) { |
41 func assertMarshal(t *testing.T, expected string, marshal interface{}) { |
51 var buf bytes.Buffer |
42 var buf bytes.Buffer |
52 enc := xml.NewEncoder(&buf) |
43 enc := xml.NewEncoder(&buf) |
106 `"></bind></iq>` |
97 `"></bind></iq>` |
107 assertMarshal(t, exp, iq) |
98 assertMarshal(t, exp, iq) |
108 } |
99 } |
109 |
100 |
110 func TestMarshalEscaping(t *testing.T) { |
101 func TestMarshalEscaping(t *testing.T) { |
111 msg := &Message{Body: &Generic{XMLName: xml.Name{Local: "body"}, |
102 msg := &Message{Body: []Text{Text{XMLName: xml.Name{Local: "body"}, |
112 Chardata: `&<!-- "`}} |
103 Chardata: `&<!-- "`}}} |
113 exp := `<message xmlns="jabber:client"><body>&<!-- "</body></message>` |
104 exp := `<message xmlns="jabber:client"><body>&<!-- "</body></message>` |
114 assertMarshal(t, exp, msg) |
105 assertMarshal(t, exp, msg) |
115 } |
106 } |
116 |
107 |
117 func TestUnmarshalMessage(t *testing.T) { |
108 func TestUnmarshalMessage(t *testing.T) { |
118 str := `<message to="a@b.c"><body>foo!</body></message>` |
109 str := `<message to="a@b.c"><body>foo!</body></message>` |
119 r := strings.NewReader(str) |
110 r := strings.NewReader(str) |
120 ch := make(chan interface{}) |
111 ch := make(chan interface{}) |
121 go recvXml(r, ch, make(map[xml.Name]reflect.Type)) |
112 cl := &Client{} |
|
113 go cl.recvXml(r, ch, make(map[xml.Name]reflect.Type)) |
122 obs := <-ch |
114 obs := <-ch |
123 exp := &Message{XMLName: xml.Name{Local: "message", Space: "jabber:client"}, |
115 exp := &Message{XMLName: xml.Name{Local: "message", Space: "jabber:client"}, |
124 Header: Header{To: "a@b.c", Innerxml: "<body>foo!</body>"}, |
116 Header: Header{To: "a@b.c", Innerxml: "<body>foo!</body>"}, |
125 Body: &Generic{XMLName: xml.Name{Local: "body", Space: "jabber:client"}, |
117 Body: []Text{Text{XMLName: xml.Name{Local: "body", Space: "jabber:client"}, |
126 Chardata: "foo!"}} |
118 Chardata: "foo!"}}} |
127 if !reflect.DeepEqual(obs, exp) { |
119 if !reflect.DeepEqual(obs, exp) { |
128 t.Errorf("read %s\ngot: %#v\nwant: %#v\n", str, obs, exp) |
120 t.Errorf("read %s\ngot: %#v\nwant: %#v\n", str, obs, exp) |
129 } |
121 } |
130 obsMsg, ok := obs.(*Message) |
122 obsMsg, ok := obs.(*Message) |
131 if !ok { |
123 if !ok { |