xmpp_test.go
author Chris Jones <christian.jones@sri.com>
Fri, 28 Dec 2012 17:07:20 -0700
changeset 114 a058e33c1666
parent 98 c9cc4eda6dce
permissions -rw-r--r--
Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible. Also improved the output from the two assert functions to show the info of the caller rather than the assert function itself.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     1
// Copyright 2011 The Go Authors.  All rights reserved.
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     2
// Use of this source code is governed by a BSD-style
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     3
// license that can be found in the LICENSE file.
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     4
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     5
package xmpp
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     6
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     7
import (
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
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"
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    10
	"reflect"
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    11
	"strings"
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    12
	"sync"
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    13
	"testing"
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    14
)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    15
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    16
func TestReadError(t *testing.T) {
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    17
	r := strings.NewReader(`<stream:error><bad-foo xmlns="blah"/>` +
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    18
		`</stream:error>`)
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    19
	ch := make(chan interface{})
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    20
	go readXml(r, ch, make(map[string]func(*xml.Name) interface{}))
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    21
	x := <-ch
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    22
	se, ok := x.(*streamError)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    23
	if !ok {
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    24
		t.Fatalf("not StreamError: %T", x)
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    25
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    26
	assertEquals(t, "bad-foo", se.Any.XMLName.Local)
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    27
	assertEquals(t, "blah", se.Any.XMLName.Space)
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    28
	if se.Text != nil {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    29
		t.Errorf("text not nil: %v", se.Text)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    30
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    31
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    32
	r = strings.NewReader(`<stream:error><bad-foo xmlns="blah"/>` +
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    33
		`<text xml:lang="en" xmlns="` + NsStreams +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    34
		`">Error text</text></stream:error>`)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    35
	ch = make(chan interface{})
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    36
	go readXml(r, ch, make(map[string]func(*xml.Name) interface{}))
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    37
	x = <-ch
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    38
	se, ok = x.(*streamError)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    39
	if !ok {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    40
		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    41
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    42
	assertEquals(t, "bad-foo", se.Any.XMLName.Local)
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    43
	assertEquals(t, "blah", se.Any.XMLName.Space)
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    44
	assertEquals(t, "Error text", se.Text.Text)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    45
	assertEquals(t, "en", se.Text.Lang)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    46
}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    47
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    48
func TestReadStream(t *testing.T) {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    49
	r := strings.NewReader(`<stream:stream to="foo.com" ` +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    50
		`from="bar.org" id="42"` +
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    51
		`xmlns="` + NsClient + `" xmlns:stream="` + NsStream +
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    52
		`" version="1.0">`)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    53
	ch := make(chan interface{})
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    54
	go readXml(r, ch, make(map[string]func(*xml.Name) interface{}))
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    55
	x := <-ch
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    56
	ss, ok := x.(*stream)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    57
	if !ok {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    58
		t.Fatalf("not stream: %v", reflect.TypeOf(x))
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    59
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    60
	assertEquals(t, "foo.com", ss.To)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    61
	assertEquals(t, "bar.org", ss.From)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    62
	assertEquals(t, "42", ss.Id)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    63
	assertEquals(t, "1.0", ss.Version)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    64
}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    65
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    66
func testWrite(obj interface{}) string {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    67
	w := bytes.NewBuffer(nil)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    68
	ch := make(chan interface{})
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    69
	var wg sync.WaitGroup
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    70
	wg.Add(1)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    71
	go func() {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    72
		defer wg.Done()
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    73
		writeXml(w, ch)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    74
	}()
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    75
	ch <- obj
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    76
	close(ch)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    77
	wg.Wait()
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    78
	return w.String()
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    79
}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    80
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    81
func TestWriteError(t *testing.T) {
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    82
	se := &streamError{Any: Generic{XMLName: xml.Name{Local: "blah"}}}
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    83
	str := testWrite(se)
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: 98
diff changeset
    84
	exp := `<error xmlns="` + NsStream + `"><blah></blah></error>`
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    85
	assertEquals(t, exp, str)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    86
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    87
	se = &streamError{Any: Generic{XMLName: xml.Name{Space: NsStreams, Local: "foo"}}, Text: &errText{Lang: "ru", Text: "Пошёл ты"}}
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    88
	str = testWrite(se)
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: 98
diff changeset
    89
	exp = `<error xmlns="` + NsStream + `"><foo xmlns="` + NsStreams +
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    90
		`"></foo><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: 98
diff changeset
    91
		`" xml:lang="ru">Пошёл ты</text></error>`
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    92
	assertEquals(t, exp, str)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    93
}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    94
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    95
func TestWriteStream(t *testing.T) {
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    96
	ss := &stream{To: "foo.org", From: "bar.com", Id: "42", Lang: "en", Version: "1.0"}
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    97
	str := testWrite(ss)
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    98
	exp := `<stream:stream xmlns="` + NsClient +
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    99
		`" xmlns:stream="` + NsStream + `" to="foo.org"` +
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
   100
		` from="bar.com" id="42" xml:lang="en" version="1.0">`
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
   101
	assertEquals(t, exp, str)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
   102
}