~samwhited/xmpp

bcd0724232b81162065161d395ef69b1d98b34d4 — Sam Whited 5 years ago e5fadb6
Make Cancel default stanza error type

Add tests
3 files changed, 43 insertions(+), 13 deletions(-)

M errors.go
M errors_test.go
M stanzatype_string.go
M errors.go => errors.go +9 -11
@@ 16,13 16,13 @@ import (
type errorType int

const (
	// An error with type Auth indicates that an operation should be retried after
	// providing credentials.
	Auth errorType = iota

	// An error with type Cancel indicates that the error cannot be remedied and
	// the operation should not be retried.
	Cancel
	Cancel errorType = iota

	// An error with type Auth indicates that an operation should be retried after
	// providing credentials.
	Auth

	// An error with type Continue indicates that the operation can proceed (the
	// condition was only a warning).


@@ 106,18 106,16 @@ func (e StanzaError) Error() string {
	return string(e.Condition)
}

func (se StanzaError) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
func (se StanzaError) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
	start = xml.StartElement{
		Name: xml.Name{Space: ``, Local: "error"},
		Attr: []xml.Attr{},
	}
	typattr, err := se.Type.MarshalXMLAttr(xml.Name{Space: "", Local: "type"})
	if err != nil {
		return err
	}
	typattr, _ := se.Type.MarshalXMLAttr(xml.Name{Space: "", Local: "type"})
	start.Attr = append(start.Attr, typattr)
	if se.By != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Space: "", Local: "by"}, Value: ""})
		a, _ := se.By.MarshalXMLAttr(xml.Name{Space: "", Local: "by"})
		start.Attr = append(start.Attr, a)
	}
	if err = e.EncodeToken(start); err != nil {
		return err

M errors_test.go => errors_test.go +32 -0
@@ 5,8 5,11 @@
package xmpp

import (
	"encoding/xml"
	"fmt"
	"testing"

	"mellium.im/xmpp/jid"
)

var (


@@ 26,3 29,32 @@ func TestErrorReturnsCondition(t *testing.T) {
		t.Errorf("Expected stanza error to return text `Text` but got %s", s.Error())
	}
}

func TestMarshalStanzaError(t *testing.T) {
	for _, data := range []struct {
		se  StanzaError
		xml string
		err bool
	}{
		{StanzaError{}, "", true},
		{StanzaError{Condition: UnexpectedRequest}, `<error type="cancel"><unexpected-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></unexpected-request></error>`, false},
		{StanzaError{Type: Cancel, Condition: UnexpectedRequest}, `<error type="cancel"><unexpected-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></unexpected-request></error>`, false},
		{StanzaError{Type: Wait, Condition: UndefinedCondition}, `<error type="wait"><undefined-condition xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></undefined-condition></error>`, false},
		{StanzaError{Type: Modify, By: jid.MustParse("test@example.net"), Condition: SubscriptionRequired}, `<error type="modify" by="test@example.net"><subscription-required xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></subscription-required></error>`, false},
		{StanzaError{Type: Continue, Condition: ServiceUnavailable, Text: "test"}, `<error type="continue"><service-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></service-unavailable><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="und">test</text></error>`, false},
	} {
		b, err := xml.Marshal(data.se)
		switch {
		case data.err && err == nil:
			t.Errorf("Expected an error when marshaling stanza error %v", data.se)
			continue
		case !data.err && err != nil:
			t.Error(err)
			continue
		case err != nil:
			continue
		case string(b) != data.xml:
			t.Errorf("Expected marshaling stanza error %v to be `%s` but got `%s`.", data.se, data.xml, string(b))
		}
	}
}

M stanzatype_string.go => stanzatype_string.go +2 -2
@@ 15,9 15,9 @@ func (i iqType) String() string {
	return _iqType_name[_iqType_index[i]:_iqType_index[i+1]]
}

const _errorType_name = "AuthCancelContinueModifyWait"
const _errorType_name = "CancelAuthContinueModifyWait"

var _errorType_index = [...]uint8{0, 4, 10, 18, 24, 28}
var _errorType_index = [...]uint8{0, 6, 10, 18, 24, 28}

func (i errorType) String() string {
	if i < 0 || i >= errorType(len(_errorType_index)-1) {