xmpp/structs_test.go
author Chris Jones <christian.jones@sri.com>
Sat, 07 Sep 2013 10:30:22 -0700
changeset 127 a8f9a0c07fc8
parent 126 367e76b3028e
child 128 8342afcffc92
permissions -rw-r--r--
Renamed client.In and client.Out to Recv and Send, respectively.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     1
// Copyright 2011 The Go Authors.  All rights reserved.
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     2
// Use of this source code is governed by a BSD-style
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     3
// license that can be found in the LICENSE file.
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     4
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     5
package xmpp
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     6
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     7
import (
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     8
	"bytes"
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
     9
	"encoding/xml"
114
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    10
	"fmt"
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    11
	"os"
115
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
    12
	"reflect"
114
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    13
	"runtime"
115
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
    14
	"strings"
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    15
	"testing"
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    16
)
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    17
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    18
func assertEquals(t *testing.T, expected, observed string) {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    19
	if expected != observed {
114
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    20
		file := "unknown"
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    21
		line := 0
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    22
		_, file, line, _ = runtime.Caller(1)
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    23
		fmt.Fprintf(os.Stderr, "%s:%d: Expected:\n%s\nObserved:\n%s\n",
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    24
			file, line, expected, observed)
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    25
		t.Fail()
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    26
	}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    27
}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    28
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    29
func TestJid(t *testing.T) {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    30
	str := "user@domain/res"
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    31
	jid := &JID{}
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    32
	if err := jid.Set(str); err != nil {
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    33
		t.Errorf("Set(%s) failed: %s", str, err)
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    34
	}
25
7437d6eed227 Made JID.Node a string rather than *string. This is more appropriate
Chris Jones <chris@cjones.org>
parents: 24
diff changeset
    35
	assertEquals(t, "user", jid.Node)
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    36
	assertEquals(t, "domain", jid.Domain)
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    37
	assertEquals(t, "res", jid.Resource)
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    38
	assertEquals(t, str, jid.String())
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    39
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    40
	str = "domain.tld"
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    41
	if err := jid.Set(str); err != nil {
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    42
		t.Errorf("Set(%s) failed: %s", str, err)
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    43
	}
25
7437d6eed227 Made JID.Node a string rather than *string. This is more appropriate
Chris Jones <chris@cjones.org>
parents: 24
diff changeset
    44
	if jid.Node != "" {
7437d6eed227 Made JID.Node a string rather than *string. This is more appropriate
Chris Jones <chris@cjones.org>
parents: 24
diff changeset
    45
		t.Errorf("Node: %v\n", jid.Node)
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    46
	}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    47
	assertEquals(t, "domain.tld", jid.Domain)
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    48
	if jid.Resource != "" {
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    49
		t.Errorf("Resource: %v\n", jid.Resource)
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    50
	}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    51
	assertEquals(t, str, jid.String())
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    52
}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    53
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    54
func assertMarshal(t *testing.T, expected string, marshal interface{}) {
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    55
	var buf bytes.Buffer
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    56
	enc := xml.NewEncoder(&buf)
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    57
	err := enc.Encode(marshal)
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    58
	if err != nil {
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    59
		t.Errorf("Marshal error for %s: %s", marshal, err)
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    60
	}
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    61
	observed := buf.String()
114
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    62
	if expected != observed {
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    63
		file := "unknown"
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    64
		line := 0
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    65
		_, file, line, _ = runtime.Caller(1)
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    66
		fmt.Fprintf(os.Stderr, "%s:%d: Expected:\n%s\nObserved:\n%s\n",
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    67
			file, line, expected, observed)
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    68
		t.Fail()
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    69
	}
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    70
}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    71
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    72
func TestStreamMarshal(t *testing.T) {
22
d6b7b4cbf50d Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents: 21
diff changeset
    73
	s := &stream{To: "bob"}
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    74
	exp := `<stream:stream xmlns="` + NsClient +
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    75
		`" xmlns:stream="` + NsStream + `" to="bob">`
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    76
	assertEquals(t, exp, s.String())
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    77
22
d6b7b4cbf50d Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents: 21
diff changeset
    78
	s = &stream{To: "bob", From: "alice", Id: "#3", Version: "5.3"}
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    79
	exp = `<stream:stream xmlns="` + NsClient +
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    80
		`" xmlns:stream="` + NsStream + `" to="bob" from="alice"` +
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 3
diff changeset
    81
		` id="#3" version="5.3">`
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    82
	assertEquals(t, exp, s.String())
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    83
22
d6b7b4cbf50d Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents: 21
diff changeset
    84
	s = &stream{Lang: "en_US"}
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    85
	exp = `<stream:stream xmlns="` + NsClient +
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    86
		`" xmlns:stream="` + NsStream + `" xml:lang="en_US">`
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    87
	assertEquals(t, exp, s.String())
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    88
}
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    89
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    90
func TestStreamErrorMarshal(t *testing.T) {
34
7b1f924c75e2 Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents: 31
diff changeset
    91
	name := xml.Name{Space: NsStreams, Local: "ack"}
31
1dc47df5c99f Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
    92
	e := &streamError{Any: Generic{XMLName: name}}
114
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    93
	exp := `<error xmlns="` + NsStream + `"><ack xmlns="` + NsStreams +
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    94
		`"></ack></error>`
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    95
	assertMarshal(t, exp, e)
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    96
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 3
diff changeset
    97
	txt := errText{Lang: "pt", Text: "things happen"}
31
1dc47df5c99f Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
    98
	e = &streamError{Any: Generic{XMLName: name}, Text: &txt}
114
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
    99
	exp = `<error xmlns="` + NsStream + `"><ack xmlns="` + NsStreams +
34
7b1f924c75e2 Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents: 31
diff changeset
   100
		`"></ack><text xmlns="` + NsStreams +
114
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 111
diff changeset
   101
		`" xml:lang="pt">things happen</text></error>`
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   102
	assertMarshal(t, exp, e)
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   103
}
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
   104
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
   105
func TestIqMarshal(t *testing.T) {
111
36287f2cf06e Step 1 of moving to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents: 110
diff changeset
   106
	iq := &Iq{Header: Header{Type: "set", Id: "3",
110
7696e6a01709 Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents: 98
diff changeset
   107
		Nested: []interface{}{Generic{XMLName: xml.Name{Space: NsBind,
7696e6a01709 Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents: 98
diff changeset
   108
			Local: "bind"}}}}}
34
7b1f924c75e2 Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents: 31
diff changeset
   109
	exp := `<iq id="3" type="set"><bind xmlns="` + NsBind +
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
   110
		`"></bind></iq>`
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
   111
	assertMarshal(t, exp, iq)
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
   112
}
26
4d0a369079ce Removed the TextOut channel.
Chris Jones <chris@cjones.org>
parents: 25
diff changeset
   113
71
578c2a83dc18 Added a quick test of XML escaping.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
   114
func TestMarshalEscaping(t *testing.T) {
578c2a83dc18 Added a quick test of XML escaping.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
   115
	msg := &Message{Body: &Generic{XMLName: xml.Name{Local: "body"},
578c2a83dc18 Added a quick test of XML escaping.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
   116
		Chardata: `&<!-- "`}}
115
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   117
	exp := `<message xmlns="jabber:client"><body>&amp;&lt;!-- &#34;</body></message>`
71
578c2a83dc18 Added a quick test of XML escaping.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
   118
	assertMarshal(t, exp, msg)
578c2a83dc18 Added a quick test of XML escaping.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
   119
}
115
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   120
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   121
func TestUnmarshalMessage(t *testing.T) {
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   122
	str := `<message to="a@b.c"><body>foo!</body></message>`
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   123
	r := strings.NewReader(str)
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   124
	ch := make(chan interface{})
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   125
	go readXml(r, ch, make(map[string]func(*xml.Name) interface{}))
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   126
	obs := <-ch
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   127
	exp := &Message{XMLName: xml.Name{Local: "message", Space: "jabber:client"},
116
Chris Jones <christian.jones@sri.com>
parents: 115
diff changeset
   128
		Header: Header{To: "a@b.c", Innerxml: "<body>foo!</body>"},
115
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   129
		Body: &Generic{XMLName: xml.Name{Local: "body", Space: "jabber:client"},
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   130
			Chardata: "foo!"}}
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   131
	if !reflect.DeepEqual(obs, exp) {
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   132
		t.Errorf("read %s\ngot:  %#v\nwant: %#v\n", str, obs, exp)
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   133
	}
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   134
	obsMsg, ok := obs.(*Message)
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   135
	if !ok {
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   136
		t.Fatalf("Not a Message: %T", obs)
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   137
	}
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   138
	obsBody := obsMsg.Body
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   139
	expBody := exp.Body
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   140
	if !reflect.DeepEqual(obsBody, expBody) {
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   141
		t.Errorf("body\ngot:  %#v\nwant: %#v\n", obsBody, expBody)
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   142
	}
7c45fc3f524a Put the sub-elements of Message and Presence into the jabber:client namespace.
Chris Jones <christian.jones@sri.com>
parents: 114
diff changeset
   143
}