|
1 // Copyright 2011 The Go Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style |
|
3 // license that can be found in the LICENSE file. |
|
4 |
|
5 package xmpp |
|
6 |
|
7 import ( |
|
8 "bytes" |
|
9 "testing" |
|
10 "xml" |
|
11 ) |
|
12 |
|
13 func TestStreamMarshal(t *testing.T) { |
|
14 s := &Stream{to: "bob"} |
|
15 exp := `<stream:stream to="bob">` |
|
16 assertMarshal(t, exp, s) |
|
17 |
|
18 s = &Stream{to: "bob", from: "alice", id: "#3", version: "5.3"} |
|
19 exp = `<stream:stream to="bob" from="alice" id="#3" version="5.3">` |
|
20 assertMarshal(t, exp, s) |
|
21 |
|
22 s = &Stream{lang: "en_US"} |
|
23 exp = `<stream:stream xml:lang="en_US">` |
|
24 assertMarshal(t, exp, s) |
|
25 } |
|
26 |
|
27 func TestStreamErrorMarshal(t *testing.T) { |
|
28 name := xml.Name{Space: nsStreams, Local: "ack"} |
|
29 e := &StreamError{cond: definedCondition{name}} |
|
30 exp := `<stream:error><ack xmlns="` + nsStreams + |
|
31 `"></ack></stream:error>`; |
|
32 assertMarshal(t, exp, e) |
|
33 |
|
34 txt := errText{Lang: "pt", text: "things happen"} |
|
35 e = &StreamError{cond: definedCondition{name}, text: &txt} |
|
36 exp = `<stream:error><ack xmlns="` + nsStreams + |
|
37 `"></ack><text xmlns="` + nsStreams + |
|
38 `" xml:lang="pt">things happen</text></stream:error>` |
|
39 assertMarshal(t, exp, e) |
|
40 } |
|
41 |
|
42 func assertMarshal(t *testing.T, expected string, marshal interface{}) { |
|
43 buf := bytes.NewBuffer(nil) |
|
44 xml.Marshal(buf, marshal) |
|
45 observed := string(buf.Bytes()) |
|
46 if expected != observed { |
|
47 t.Errorf("Expected:\n%s\nObserved:\n%s\n", expected, |
|
48 observed) |
|
49 } |
|
50 } |