~samwhited/xmpp

1b1201603a37f2962fce53d3c8739af11c0a6509 — Sam Whited 1 year, 7 months ago f049ab8
all: remove unnecessary pointer in WrapIQ
M bind.go => bind.go +2 -2
@@ 52,12 52,12 @@ type bindIQ struct {

func (biq *bindIQ) TokenReader() xml.TokenReader {
	if biq.Err != nil {
		return stanza.WrapIQ(&biq.IQ, xmlstream.Wrap(biq.Err.TokenReader(),
		return stanza.WrapIQ(biq.IQ, xmlstream.Wrap(biq.Err.TokenReader(),
			xml.StartElement{Name: xml.Name{Local: "bind", Space: ns.Bind}},
		))
	}

	return stanza.WrapIQ(&biq.IQ, xmlstream.Wrap(biq.Bind.TokenReader(),
	return stanza.WrapIQ(biq.IQ, xmlstream.Wrap(biq.Bind.TokenReader(),
		xml.StartElement{Name: xml.Name{Local: "bind", Space: ns.Bind}},
	))
}

M ping/ping.go => ping/ping.go +1 -1
@@ 20,7 20,7 @@ const NS = `urn:xmpp:ping`
// payload.
func IQ(to jid.JID) xml.TokenReader {
	start := xml.StartElement{Name: xml.Name{Local: "ping", Space: NS}}
	return stanza.WrapIQ(&stanza.IQ{
	return stanza.WrapIQ(stanza.IQ{
		To:   to,
		Type: stanza.GetIQ,
	}, xmlstream.Wrap(nil, start))

M roster/roster.go => roster/roster.go +1 -1
@@ 187,7 187,7 @@ func (iq IQ) TokenReader() xml.TokenReader {
		iq.IQ.Type = stanza.GetIQ
	}

	return stanza.WrapIQ(&iq.IQ, xmlstream.Wrap(
	return stanza.WrapIQ(iq.IQ, xmlstream.Wrap(
		itemMarshaler{items: iq.Query.Item},
		xml.StartElement{Name: xml.Name{Local: "query", Space: NS}, Attr: attrs},
	))

M send_test.go => send_test.go +6 -6
@@ 73,22 73,22 @@ var sendTests = [...]struct {
		writesBody: true,
	},
	4: {
		r:          stanza.WrapIQ(&stanza.IQ{Type: stanza.ResultIQ}, nil),
		r:          stanza.WrapIQ(stanza.IQ{Type: stanza.ResultIQ}, nil),
		writesBody: true,
	},
	5: {
		r:          stanza.WrapIQ(&stanza.IQ{Type: stanza.ErrorIQ}, nil),
		r:          stanza.WrapIQ(stanza.IQ{Type: stanza.ErrorIQ}, nil),
		writesBody: true,
	},
	6: {
		r:          stanza.WrapIQ(&stanza.IQ{ID: testIQID, Type: stanza.GetIQ}, nil),
		r:          stanza.WrapIQ(stanza.IQ{ID: testIQID, Type: stanza.GetIQ}, nil),
		writesBody: true,
		resp:       stanza.WrapIQ(&stanza.IQ{ID: testIQID, Type: stanza.ResultIQ}, nil),
		resp:       stanza.WrapIQ(stanza.IQ{ID: testIQID, Type: stanza.ResultIQ}, nil),
	},
	7: {
		r:          stanza.WrapIQ(&stanza.IQ{ID: testIQID, Type: stanza.SetIQ}, nil),
		r:          stanza.WrapIQ(stanza.IQ{ID: testIQID, Type: stanza.SetIQ}, nil),
		writesBody: true,
		resp:       stanza.WrapIQ(&stanza.IQ{ID: testIQID, Type: stanza.ErrorIQ}, nil),
		resp:       stanza.WrapIQ(stanza.IQ{ID: testIQID, Type: stanza.ErrorIQ}, nil),
	},
}


M session.go => session.go +1 -1
@@ 402,7 402,7 @@ func (s *Session) handleInputStream(handler Handler) (err error) {

		// If the user did not write a response to an IQ, send a default one.
		if needsResp && !rw.wroteResp {
			_, err := xmlstream.Copy(s, stanza.WrapIQ(&stanza.IQ{
			_, err := xmlstream.Copy(s, stanza.WrapIQ(stanza.IQ{
				ID:   id,
				Type: stanza.ErrorIQ,
			}, stanza.Error{

M session_test.go => session_test.go +4 -4
@@ 155,7 155,7 @@ var serveTests = [...]struct {
	},
	3: {
		handler: xmpp.HandlerFunc(func(rw xmlstream.TokenReadWriter, start *xml.StartElement) error {
			_, err := xmlstream.Copy(rw, stanza.WrapIQ(&stanza.IQ{
			_, err := xmlstream.Copy(rw, stanza.WrapIQ(stanza.IQ{
				ID:   "1234",
				Type: stanza.ResultIQ,
			}, nil))


@@ 167,7 167,7 @@ var serveTests = [...]struct {
	},
	4: {
		handler: xmpp.HandlerFunc(func(rw xmlstream.TokenReadWriter, start *xml.StartElement) error {
			_, err := xmlstream.Copy(rw, stanza.WrapIQ(&stanza.IQ{
			_, err := xmlstream.Copy(rw, stanza.WrapIQ(stanza.IQ{
				ID:   "wrongid",
				Type: stanza.ResultIQ,
			}, nil))


@@ 179,7 179,7 @@ var serveTests = [...]struct {
	},
	5: {
		handler: xmpp.HandlerFunc(func(rw xmlstream.TokenReadWriter, start *xml.StartElement) error {
			_, err := xmlstream.Copy(rw, stanza.WrapIQ(&stanza.IQ{
			_, err := xmlstream.Copy(rw, stanza.WrapIQ(stanza.IQ{
				ID:   "1234",
				Type: stanza.ErrorIQ,
			}, nil))


@@ 191,7 191,7 @@ var serveTests = [...]struct {
	},
	6: {
		handler: xmpp.HandlerFunc(func(rw xmlstream.TokenReadWriter, start *xml.StartElement) error {
			_, err := xmlstream.Copy(rw, stanza.WrapIQ(&stanza.IQ{
			_, err := xmlstream.Copy(rw, stanza.WrapIQ(stanza.IQ{
				ID:   "1234",
				Type: stanza.GetIQ,
			}, nil))

M stanza/doc.go => stanza/doc.go +1 -1
@@ 55,7 55,7 @@
//    func PingIQ(to jid.JID) xml.TokenReader {
//        start := xml.StartElement{Name: xml.Name{Space: "urn:xmpp:ping", Local: "ping"}}
//        return stanza.WrapIQ(
//            &stanza.IQ{To: to, Type: stanza.GetIQ},
//            stanza.IQ{To: to, Type: stanza.GetIQ},
//            xmlstream.Wrap(nil, start)
//        )
//    }

M stanza/example_pingstream_test.go => stanza/example_pingstream_test.go +1 -1
@@ 18,7 18,7 @@ import (
// a ping payload.
func WrapPingIQ(to jid.JID) xml.TokenReader {
	start := xml.StartElement{Name: xml.Name{Local: "ping", Space: "urn:xmpp:ping"}}
	return stanza.WrapIQ(&stanza.IQ{To: to, Type: stanza.GetIQ}, xmlstream.Wrap(nil, start))
	return stanza.WrapIQ(stanza.IQ{To: to, Type: stanza.GetIQ}, xmlstream.Wrap(nil, start))
}

func Example_stream() {

M stanza/iq.go => stanza/iq.go +11 -7
@@ 15,18 15,22 @@ import (
// WrapIQ wraps a payload in an IQ stanza.
// The resulting IQ may not contain an id or from attribute and thus may not be
// valid without further processing.
func WrapIQ(iq *IQ, payload xml.TokenReader) xml.TokenReader {
func WrapIQ(iq IQ, payload xml.TokenReader) xml.TokenReader {
	attr := []xml.Attr{
		{Name: xml.Name{Local: "type"}, Value: string(iq.Type)},
	}

	to, err := iq.To.MarshalXMLAttr(xml.Name{Space: "", Local: "to"})
	if err == nil && to.Value != "" {
		attr = append(attr, to)
	if !iq.To.Equal(jid.JID{}) {
		to, err := iq.To.MarshalXMLAttr(xml.Name{Space: "", Local: "to"})
		if err == nil && to.Value != "" {
			attr = append(attr, to)
		}
	}
	from, err := iq.From.MarshalXMLAttr(xml.Name{Space: "", Local: "from"})
	if err == nil && from.Value != "" {
		attr = append(attr, from)
	if !iq.From.Equal(jid.JID{}) {
		from, err := iq.From.MarshalXMLAttr(xml.Name{Space: "", Local: "from"})
		if err == nil && from.Value != "" {
			attr = append(attr, from)
		}
	}

	if iq.Lang != "" {

M stanza/iq_test.go => stanza/iq_test.go +53 -0
@@ 8,11 8,64 @@ import (
	"bytes"
	"encoding/xml"
	"fmt"
	"strings"
	"testing"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/stanza"
)

type iqTest struct {
	to      string
	typ     stanza.IQType
	payload xml.TokenReader
	out     string
	err     error
}

var iqTests = [...]iqTest{
	0: {
		to:      "new@example.net",
		payload: &testReader{},
	},
	1: {
		to:      "new@example.org",
		payload: &testReader{start, start.End()},
		out:     `<ping></ping>`,
		typ:     stanza.GetIQ,
	},
}

func TestIQ(t *testing.T) {
	for i, tc := range iqTests {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			b := new(bytes.Buffer)
			e := xml.NewEncoder(b)
			iq := stanza.WrapIQ(stanza.IQ{To: jid.MustParse(tc.to), Type: tc.typ}, tc.payload)
			if _, err := xmlstream.Copy(e, iq); err != tc.err {
				t.Errorf("Unexpected error: want=`%v', got=`%v'", tc.err, err)
			}
			if err := e.Flush(); err != nil {
				t.Fatalf("Error flushing: %q", err)
			}

			o := b.String()
			jidattr := fmt.Sprintf(`to="%s"`, tc.to)
			if !strings.Contains(o, jidattr) {
				t.Errorf("Expected output to have attr `%s',\ngot=`%s'", jidattr, o)
			}
			typeattr := fmt.Sprintf(`type="%s"`, string(tc.typ))
			if !strings.Contains(o, typeattr) {
				t.Errorf("Expected output to have attr `%s',\ngot=`%s'", typeattr, o)
			}
			if !strings.Contains(o, tc.out) {
				t.Errorf("Expected output to contain payload `%s',\ngot=`%s'", tc.out, o)
			}
		})
	}
}

func TestMarshalIQTypeAttr(t *testing.T) {
	for i, tc := range [...]struct {
		iqtype stanza.IQType

M stanza/stanza_test.go => stanza/stanza_test.go +0 -50
@@ 32,56 32,6 @@ var start = xml.StartElement{
	Name: xml.Name{Local: "ping"},
}

type iqTest struct {
	to      string
	typ     stanza.IQType
	payload xml.TokenReader
	out     string
	err     error
}

var iqTests = [...]iqTest{
	0: {
		to:      "new@example.net",
		payload: &testReader{},
	},
	1: {
		to:      "new@example.org",
		payload: &testReader{start, start.End()},
		out:     `<ping></ping>`,
		typ:     stanza.GetIQ,
	},
}

func TestIQ(t *testing.T) {
	for i, tc := range iqTests {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			b := new(bytes.Buffer)
			e := xml.NewEncoder(b)
			iq := stanza.WrapIQ(&stanza.IQ{To: jid.MustParse(tc.to), Type: tc.typ}, tc.payload)
			if _, err := xmlstream.Copy(e, iq); err != tc.err {
				t.Errorf("Unexpected error: want=`%v', got=`%v'", tc.err, err)
			}
			if err := e.Flush(); err != nil {
				t.Fatalf("Error flushing: %q", err)
			}

			o := b.String()
			jidattr := fmt.Sprintf(`to="%s"`, tc.to)
			if !strings.Contains(o, jidattr) {
				t.Errorf("Expected output to have attr `%s',\ngot=`%s'", jidattr, o)
			}
			typeattr := fmt.Sprintf(`type="%s"`, string(tc.typ))
			if !strings.Contains(o, typeattr) {
				t.Errorf("Expected output to have attr `%s',\ngot=`%s'", typeattr, o)
			}
			if !strings.Contains(o, tc.out) {
				t.Errorf("Expected output to contain payload `%s',\ngot=`%s'", tc.out, o)
			}
		})
	}
}

type messageTest struct {
	to      string
	typ     stanza.MessageType