5 package xmpp |
5 package xmpp |
6 |
6 |
7 import ( |
7 import ( |
8 "bytes" |
8 "bytes" |
9 "encoding/xml" |
9 "encoding/xml" |
|
10 "fmt" |
|
11 "os" |
|
12 "runtime" |
10 "testing" |
13 "testing" |
11 ) |
14 ) |
12 |
15 |
13 func assertEquals(t *testing.T, expected, observed string) { |
16 func assertEquals(t *testing.T, expected, observed string) { |
14 if expected != observed { |
17 if expected != observed { |
15 t.Errorf("Expected:\n%s\nObserved:\n%s\n", expected, |
18 file := "unknown" |
16 observed) |
19 line := 0 |
|
20 _, file, line, _ = runtime.Caller(1) |
|
21 fmt.Fprintf(os.Stderr, "%s:%d: Expected:\n%s\nObserved:\n%s\n", |
|
22 file, line, expected, observed) |
|
23 t.Fail() |
17 } |
24 } |
18 } |
25 } |
19 |
26 |
20 func TestJid(t *testing.T) { |
27 func TestJid(t *testing.T) { |
21 str := "user@domain/res" |
28 str := "user@domain/res" |
43 } |
50 } |
44 |
51 |
45 func assertMarshal(t *testing.T, expected string, marshal interface{}) { |
52 func assertMarshal(t *testing.T, expected string, marshal interface{}) { |
46 var buf bytes.Buffer |
53 var buf bytes.Buffer |
47 enc := xml.NewEncoder(&buf) |
54 enc := xml.NewEncoder(&buf) |
48 enc.Context.Map[NsClient] = "" |
|
49 enc.Context.Map[NsStream] = "stream" |
|
50 err := enc.Encode(marshal) |
55 err := enc.Encode(marshal) |
51 if err != nil { |
56 if err != nil { |
52 t.Errorf("Marshal error for %s: %s", marshal, err) |
57 t.Errorf("Marshal error for %s: %s", marshal, err) |
53 } |
58 } |
54 observed := buf.String() |
59 observed := buf.String() |
55 assertEquals(t, expected, observed) |
60 if expected != observed { |
|
61 file := "unknown" |
|
62 line := 0 |
|
63 _, file, line, _ = runtime.Caller(1) |
|
64 fmt.Fprintf(os.Stderr, "%s:%d: Expected:\n%s\nObserved:\n%s\n", |
|
65 file, line, expected, observed) |
|
66 t.Fail() |
|
67 } |
56 } |
68 } |
57 |
69 |
58 func TestStreamMarshal(t *testing.T) { |
70 func TestStreamMarshal(t *testing.T) { |
59 s := &stream{To: "bob"} |
71 s := &stream{To: "bob"} |
60 exp := `<stream:stream xmlns="` + NsClient + |
72 exp := `<stream:stream xmlns="` + NsClient + |
74 } |
86 } |
75 |
87 |
76 func TestStreamErrorMarshal(t *testing.T) { |
88 func TestStreamErrorMarshal(t *testing.T) { |
77 name := xml.Name{Space: NsStreams, Local: "ack"} |
89 name := xml.Name{Space: NsStreams, Local: "ack"} |
78 e := &streamError{Any: Generic{XMLName: name}} |
90 e := &streamError{Any: Generic{XMLName: name}} |
79 exp := `<stream:error><ack xmlns="` + NsStreams + |
91 exp := `<error xmlns="` + NsStream + `"><ack xmlns="` + NsStreams + |
80 `"></ack></stream:error>` |
92 `"></ack></error>` |
81 assertMarshal(t, exp, e) |
93 assertMarshal(t, exp, e) |
82 |
94 |
83 txt := errText{Lang: "pt", Text: "things happen"} |
95 txt := errText{Lang: "pt", Text: "things happen"} |
84 e = &streamError{Any: Generic{XMLName: name}, Text: &txt} |
96 e = &streamError{Any: Generic{XMLName: name}, Text: &txt} |
85 exp = `<stream:error><ack xmlns="` + NsStreams + |
97 exp = `<error xmlns="` + NsStream + `"><ack xmlns="` + NsStreams + |
86 `"></ack><text xmlns="` + NsStreams + |
98 `"></ack><text xmlns="` + NsStreams + |
87 `" xml:lang="pt">things happen</text></stream:error>` |
99 `" xml:lang="pt">things happen</text></error>` |
88 assertMarshal(t, exp, e) |
100 assertMarshal(t, exp, e) |
89 } |
101 } |
90 |
102 |
91 func TestIqMarshal(t *testing.T) { |
103 func TestIqMarshal(t *testing.T) { |
92 iq := &Iq{Header: Header{Type: "set", Id: "3", |
104 iq := &Iq{Header: Header{Type: "set", Id: "3", |