~samwhited/xmpp

f049ab8dabe39d7cf5b125095ce050d2bfd2c6fd — Sam Whited 1 year, 7 months ago c94affa
all: remove unnecessary pointer in WrapPresence

And while we're testing the new API, clean up existing tests.
6 files changed, 65 insertions(+), 64 deletions(-)

M doc.go
M echobot_example_test.go
M send_test.go
M stanza/presence.go
M stanza/presence_test.go
M stanza/stanza_test.go
M doc.go => doc.go +1 -1
@@ 88,7 88,7 @@
// These can be sent with the Send and SendElement methods.
//
//     // Send initial presence to let the server know we want to receive messages.
//     _, err = session.Send(context.TODO(), stanza.WrapPresence(nil, stanza.AvailablePresence, nil))
//     _, err = session.Send(context.TODO(), stanza.WrapPresence(jid.JID{}, stanza.AvailablePresence, nil))
//
// For Send to correctly handle IQ responses, and to make the common case of
// polling for incoming XML on the input stream—and possibly writing to the

M echobot_example_test.go => echobot_example_test.go +1 -1
@@ 49,7 49,7 @@ func Example_echobot() {
	}()

	// Send initial presence to let the server know we want to receive messages.
	_, err = s.Send(context.TODO(), stanza.WrapPresence(nil, stanza.AvailablePresence, nil))
	_, err = s.Send(context.TODO(), stanza.WrapPresence(jid.JID{}, stanza.AvailablePresence, nil))
	if err != nil {
		log.Printf("Error sending initial presence: %q", err)
		return

M send_test.go => send_test.go +1 -1
@@ 69,7 69,7 @@ var sendTests = [...]struct {
		writesBody: true,
	},
	3: {
		r:          stanza.WrapPresence(&to, stanza.AvailablePresence, nil),
		r:          stanza.WrapPresence(to, stanza.AvailablePresence, nil),
		writesBody: true,
	},
	4: {

M stanza/presence.go => stanza/presence.go +5 -2
@@ 12,9 12,12 @@ import (
)

// WrapPresence wraps a payload in a presence stanza.
func WrapPresence(to *jid.JID, typ PresenceType, payload xml.TokenReader) xml.TokenReader {
//
// If to is the zero value for jid.JID, no to attribute is set on the resulting
// presence.
func WrapPresence(to jid.JID, typ PresenceType, payload xml.TokenReader) xml.TokenReader {
	attrs := make([]xml.Attr, 0, 2)
	if to != nil {
	if !to.Equal(jid.JID{}) {
		attrs = append(attrs, xml.Attr{Name: xml.Name{Local: "to"}, Value: to.String()})
	}
	if typ != AvailablePresence {

M stanza/presence_test.go => stanza/presence_test.go +57 -2
@@ 8,11 8,66 @@ import (
	"bytes"
	"encoding/xml"
	"fmt"
	"strconv"
	"testing"

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

var exampleJID = jid.MustParse("example.net")

var wrapPresenceTests = [...]struct {
	to      jid.JID
	typ     stanza.PresenceType
	payload xml.TokenReader
	out     string
}{
	0: {out: "<presence></presence>"},
	1: {
		to:  exampleJID,
		out: `<presence to="example.net"></presence>`,
	},
	2: {
		typ: stanza.SubscribedPresence,
		out: `<presence type="subscribed"></presence>`,
	},
	3: {
		to:  exampleJID,
		typ: stanza.SubscribedPresence,
		out: `<presence to="example.net" type="subscribed"></presence>`,
	},
	4: {
		payload: &testReader{},
		out:     `<presence></presence>`,
	},
	5: {
		payload: &testReader{start, start.End()},
		out:     `<presence><ping></ping></presence>`,
	},
}

func TestWrapPresence(t *testing.T) {
	for i, tc := range wrapPresenceTests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			buf := &bytes.Buffer{}
			e := xml.NewEncoder(buf)
			presence := stanza.WrapPresence(tc.to, tc.typ, tc.payload)
			_, err := xmlstream.Copy(e, presence)
			if err != nil {
				t.Fatalf("Error encoding stream: %q", err)
			}
			if err := e.Flush(); err != nil {
				t.Fatalf("Error flushing stream: %q", err)
			}
			if s := buf.String(); s != tc.out {
				t.Fatalf("Wrong encoding:\nwant=\n%q,\ngot=\n%q", tc.out, s)
			}
		})
	}
}

func TestMarshalPresenceTypeAttr(t *testing.T) {
	for i, tc := range [...]struct {
		presencetype stanza.PresenceType


@@ 21,7 76,7 @@ func TestMarshalPresenceTypeAttr(t *testing.T) {
		0: {stanza.PresenceType(""), ""},
		1: {stanza.ErrorPresence, "error"},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			b, err := xml.Marshal(stanza.Presence{Type: tc.presencetype})
			if err != nil {
				t.Fatal("Unexpected error while marshaling:", err)


@@ 51,7 106,7 @@ func TestUnmarshalPresenceTypeAttr(t *testing.T) {
		1: {`<presence type=""/>`, stanza.PresenceType("")},
		2: {`<presence type="probe"/>`, stanza.ProbePresence},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			presence := stanza.Presence{}
			switch err := xml.Unmarshal([]byte(tc.presence), &presence); {
			case err != nil:

M stanza/stanza_test.go => stanza/stanza_test.go +0 -57
@@ 131,60 131,3 @@ func TestMessage(t *testing.T) {
		})
	}
}

type presenceTest struct {
	to      string
	typ     stanza.PresenceType
	payload xml.TokenReader
	out     string
	err     error
}

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

func TestPresence(t *testing.T) {
	for i, tc := range presenceTests {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			b := new(bytes.Buffer)
			e := xml.NewEncoder(b)
			j := jid.MustParse(tc.to)
			presence := stanza.WrapPresence(&j, tc.typ, tc.payload)
			if _, err := xmlstream.Copy(e, presence); 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)
			}
			if tc.typ == stanza.AvailablePresence {
				if strings.Contains(o, "type=") {
					t.Errorf("Expected empty type attr got=`%s'", o)
				}
			} else {
				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)
			}
		})
	}
}