Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
authorChris Jones <christian.jones@sri.com>
Fri, 28 Dec 2012 17:07:20 -0700
changeset 114 a058e33c1666
parent 113 bee6cc131798
child 115 7c45fc3f524a
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.
stream.go
structs_test.go
xmpp_test.go
--- a/stream.go	Sun Dec 16 23:06:54 2012 -0700
+++ b/stream.go	Fri Dec 28 17:07:20 2012 -0700
@@ -87,9 +87,13 @@
 	}
 	defer close(ch)
 
-	p := xml.NewDecoder(r)
-	p.Context.Map[""] = NsClient
-	p.Context.Map["stream"] = NsStream
+	// This trick loads our namespaces into the parser.
+	nsstr := fmt.Sprintf(`<a xmlns="%s" xmlns:stream="%s">`,
+		NsClient, NsStream)
+	nsrdr := strings.NewReader(nsstr)
+	p := xml.NewDecoder(io.MultiReader(nsrdr, r))
+	p.Token()
+
 Loop:
 	for {
 		// Sniff the next token on the stream.
@@ -206,8 +210,6 @@
 	}(w)
 
 	enc := xml.NewEncoder(w)
-	enc.Context.Map[NsClient] = ""
-	enc.Context.Map[NsStream] = "stream"
 
 	for obj := range ch {
 		if st, ok := obj.(*stream); ok {
--- a/structs_test.go	Sun Dec 16 23:06:54 2012 -0700
+++ b/structs_test.go	Fri Dec 28 17:07:20 2012 -0700
@@ -7,13 +7,20 @@
 import (
 	"bytes"
 	"encoding/xml"
+	"fmt"
+	"os"
+	"runtime"
 	"testing"
 )
 
 func assertEquals(t *testing.T, expected, observed string) {
 	if expected != observed {
-		t.Errorf("Expected:\n%s\nObserved:\n%s\n", expected,
-			observed)
+		file := "unknown"
+		line := 0
+		_, file, line, _ = runtime.Caller(1)
+		fmt.Fprintf(os.Stderr, "%s:%d: Expected:\n%s\nObserved:\n%s\n",
+			file, line, expected, observed)
+		t.Fail()
 	}
 }
 
@@ -45,14 +52,19 @@
 func assertMarshal(t *testing.T, expected string, marshal interface{}) {
 	var buf bytes.Buffer
 	enc := xml.NewEncoder(&buf)
-	enc.Context.Map[NsClient] = ""
-	enc.Context.Map[NsStream] = "stream"
 	err := enc.Encode(marshal)
 	if err != nil {
 		t.Errorf("Marshal error for %s: %s", marshal, err)
 	}
 	observed := buf.String()
-	assertEquals(t, expected, observed)
+	if expected != observed {
+		file := "unknown"
+		line := 0
+		_, file, line, _ = runtime.Caller(1)
+		fmt.Fprintf(os.Stderr, "%s:%d: Expected:\n%s\nObserved:\n%s\n",
+			file, line, expected, observed)
+		t.Fail()
+	}
 }
 
 func TestStreamMarshal(t *testing.T) {
@@ -76,15 +88,15 @@
 func TestStreamErrorMarshal(t *testing.T) {
 	name := xml.Name{Space: NsStreams, Local: "ack"}
 	e := &streamError{Any: Generic{XMLName: name}}
-	exp := `<stream:error><ack xmlns="` + NsStreams +
-		`"></ack></stream:error>`
+	exp := `<error xmlns="` + NsStream + `"><ack xmlns="` + NsStreams +
+		`"></ack></error>`
 	assertMarshal(t, exp, e)
 
 	txt := errText{Lang: "pt", Text: "things happen"}
 	e = &streamError{Any: Generic{XMLName: name}, Text: &txt}
-	exp = `<stream:error><ack xmlns="` + NsStreams +
+	exp = `<error xmlns="` + NsStream + `"><ack xmlns="` + NsStreams +
 		`"></ack><text xmlns="` + NsStreams +
-		`" xml:lang="pt">things happen</text></stream:error>`
+		`" xml:lang="pt">things happen</text></error>`
 	assertMarshal(t, exp, e)
 }
 
--- a/xmpp_test.go	Sun Dec 16 23:06:54 2012 -0700
+++ b/xmpp_test.go	Fri Dec 28 17:07:20 2012 -0700
@@ -81,14 +81,14 @@
 func TestWriteError(t *testing.T) {
 	se := &streamError{Any: Generic{XMLName: xml.Name{Local: "blah"}}}
 	str := testWrite(se)
-	exp := `<stream:error><blah></blah></stream:error>`
+	exp := `<error xmlns="` + NsStream + `"><blah></blah></error>`
 	assertEquals(t, exp, str)
 
 	se = &streamError{Any: Generic{XMLName: xml.Name{Space: NsStreams, Local: "foo"}}, Text: &errText{Lang: "ru", Text: "Пошёл ты"}}
 	str = testWrite(se)
-	exp = `<stream:error><foo xmlns="` + NsStreams +
+	exp = `<error xmlns="` + NsStream + `"><foo xmlns="` + NsStreams +
 		`"></foo><text xmlns="` + NsStreams +
-		`" xml:lang="ru">Пошёл ты</text></stream:error>`
+		`" xml:lang="ru">Пошёл ты</text></error>`
 	assertEquals(t, exp, str)
 }