~samwhited/xmpp

a71a8800319db52bbb73e449841510958ea46334 — Sam Whited 5 years ago 8748f2b
Fix NoVersion stream option

Add more stream tests
2 files changed, 65 insertions(+), 26 deletions(-)

M stream/stream.go
M stream/stream_test.go
M stream/stream.go => stream/stream.go +29 -26
@@ 102,33 102,36 @@ func (s Stream) StartElement() xml.StartElement {
	} else {
		xmlns = "jabber:client"
	}
	attrs := []xml.Attr{
		xml.Attr{
			xml.Name{"", "to"},
			s.to.String(),
		},
		xml.Attr{
			xml.Name{"", "from"},
			s.from.String(),
		},
		xml.Attr{
			xml.Name{"xml", "lang"},
			s.options.lang.String(),
		},
		xml.Attr{
			xml.Name{"", "id"},
			s.id,
		},
		xml.Attr{
			xml.Name{"", "xmlns"},
			xmlns,
		},
	}
	if !s.options.noVersionAttr {
		attrs = append(attrs, xml.Attr{
			xml.Name{"", "version"},
			s.version.String(),
		})
	}
	return xml.StartElement{
		Name: xml.Name{"stream", "stream"},
		Attr: []xml.Attr{
			xml.Attr{
				xml.Name{"", "to"},
				s.to.String(),
			},
			xml.Attr{
				xml.Name{"", "from"},
				s.from.String(),
			},
			xml.Attr{
				xml.Name{"", "version"},
				s.version.String(),
			},
			xml.Attr{
				xml.Name{"xml", "lang"},
				s.options.lang.String(),
			},
			xml.Attr{
				xml.Name{"", "id"},
				s.id,
			},
			xml.Attr{
				xml.Name{"", "xmlns"},
				xmlns,
			},
		},
		Attr: attrs,
	}
}

M stream/stream_test.go => stream/stream_test.go +36 -0
@@ 7,6 7,8 @@ package stream
import (
	"encoding/xml"
	"testing"

	"bitbucket.org/mellium/xmpp/jid"
)

var (


@@ 58,3 60,37 @@ func TestStreamFromStartElement(t *testing.T) {
		}
	}
}

func TestStreamStartElement(t *testing.T) {
	to, _ := jid.ParseString("shakespeare.lit")
	from, _ := jid.ParseString("example.net")

	// The default stream namespace should be jabber:client.
	s := New(to, from)
	se := s.StartElement()
	xmlnsname := xml.Name{"", "xmlns"}
	for _, attr := range se.Attr {
		if attr.Name == xmlnsname && attr.Value != "jabber:client" {
			t.Log("Default stream xmlns should be jabber:client.")
			t.Fail()
		}
	}

	// Server to Server streams should have jabber:server namespace.
	se = New(to, from, ServerToServer).StartElement()
	for _, attr := range se.Attr {
		if attr.Name == xmlnsname && attr.Value != "jabber:server" {
			t.Log("ServerToServer option should set namespace to jabber:server.")
			t.Fail()
		}
	}

	// The NoVersion option should result in no stream version attribute.
	se = New(to, from, NoVersion).StartElement()
	for _, attr := range se.Attr {
		if attr.Name.Local == "version" {
			t.Log("NoVersion option should result in no version attribute.")
			t.Fail()
		}
	}
}