~samwhited/xmpp

ref: 9e167719f2a8302e0694db8a93cc139ab25f5fb6 xmpp/version/version_test.go -rw-r--r-- 2.4 KiB
9e167719Sam Whited xmpp: simplify xml wrapping 10 months 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2021 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 version_test

import (
	"context"
	"encoding/xml"
	"reflect"
	"strconv"
	"strings"
	"testing"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/internal/xmpptest"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/stanza"
	"mellium.im/xmpp/version"
)

var (
	_ xmlstream.Marshaler = (*version.Query)(nil)
	_ xmlstream.WriterTo  = (*version.Query)(nil)
)

var marshalTests = [...]struct {
	in  version.Query
	out string
}{
	0: {
		in:  version.Query{},
		out: `<query xmlns="` + version.NS + `"></query>`,
	},
	1: {
		in: version.Query{
			Name:    "name",
			Version: "ver",
			OS:      "os",
		},
		out: `<query xmlns="` + version.NS + `"><name>name</name><version>ver</version><os>os</os></query>`,
	},
}

func TestMarshal(t *testing.T) {
	for i, tc := range marshalTests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			t.Run("marshal", func(t *testing.T) {
				b, err := xml.Marshal(tc.in)
				if err != nil {
					t.Fatalf("error marshaling IQ: %v", err)
				}
				if string(b) != tc.out {
					t.Errorf("wrong output:\nwant=%s,\n got=%s", tc.out, b)
				}
			})
			t.Run("encode", func(t *testing.T) {
				var buf strings.Builder
				e := xml.NewEncoder(&buf)
				_, err := tc.in.WriteXML(e)
				if err != nil {
					t.Fatalf("error writing XML: %v", err)
				}
				if err = e.Flush(); err != nil {
					t.Fatalf("error flushing XML: %v", err)
				}
				if out := buf.String(); out != tc.out {
					t.Errorf("wrong output:\nwant=%s,\n got=%s", tc.out, out)
				}
			})
		})
	}
}

func TestGet(t *testing.T) {
	query := version.Query{
		Name:    "name",
		Version: "ver",
		OS:      "os",
	}
	cs := xmpptest.NewClientServer(
		xmpptest.ServerHandlerFunc(func(e xmlstream.TokenReadEncoder, start *xml.StartElement) error {
			iq, err := stanza.NewIQ(*start)
			if err != nil {
				return err
			}
			_, err = xmlstream.Copy(e, iq.Result(query.TokenReader()))
			return err
		}),
	)
	resp, err := version.Get(context.Background(), cs.Client, jid.JID{})
	if err != nil {
		t.Fatalf("error querying version: %v", err)
	}
	expectedName := xml.Name{Space: version.NS, Local: "query"}
	if resp.XMLName != expectedName {
		t.Errorf("wrong XML name: want=%v, got=%v", expectedName, resp.XMLName)
	}
	resp.XMLName = xml.Name{}
	if !reflect.DeepEqual(resp, query) {
		t.Errorf("unexpected response: want=%v, got=%v", query, resp)
	}
}