~samwhited/xmpp

ref: a42ef11e9469b0b65746f979e1bdf5155d1c13dd xmpp/stanza/iq_test.go -rw-r--r-- 1.6 KiB
a42ef11eSam Whited all: update copyright message 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2016 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package stanza_test

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

	"mellium.im/xmpp/stanza"
)

func TestMarshalIQTypeAttr(t *testing.T) {
	for i, tc := range [...]struct {
		iqtype stanza.IQType
		value  string
	}{
		0: {stanza.IQType(""), ""},
		1: {stanza.GetIQ, "get"},
		2: {stanza.SetIQ, "set"},
		3: {stanza.ResultIQ, "result"},
		4: {stanza.ErrorIQ, "error"},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			b, err := xml.Marshal(stanza.IQ{Type: tc.iqtype})
			if err != nil {
				t.Fatal("Got unexpected error while marshaling IQ:", err)
			}

			if err == nil && !bytes.Contains(b, []byte(fmt.Sprintf(`type="%s"`, tc.iqtype))) {
				t.Errorf(`Expected output to contain type="%s", found: %s`, tc.iqtype, b)
			}
		})
	}
}

func TestUnmarshalIQTypeAttr(t *testing.T) {
	for i, tc := range [...]struct {
		iq     string
		iqtype stanza.IQType
	}{
		0: {`<iq/>`, stanza.IQType("")},
		1: {`<iq type=""/>`, stanza.IQType("")},
		2: {`<iq type="get"/>`, stanza.GetIQ},
		3: {`<iq type="error"/>`, stanza.ErrorIQ},
		4: {`<iq type="result"/>`, stanza.ResultIQ},
		5: {`<iq type="set"/>`, stanza.SetIQ},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			iq := stanza.IQ{}
			switch err := xml.Unmarshal([]byte(tc.iq), &iq); {
			case err != nil:
				t.Errorf("Got unexpected error while unmarshaling IQ: %v", err)
			case tc.iqtype != iq.Type:
				t.Errorf("Wrong type when unmarshaling IQ: want=%s, got=%s", tc.iqtype, iq.Type)
			}
		})
	}
}