~samwhited/xmpp

xmpp/oob/oob_test.go -rw-r--r-- 3.5 KiB
c9743d9fSam Whited docs: update changelog to include deprecations 8 days 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2020 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 oob_test

import (
	"encoding/xml"
	"strings"
	"testing"

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

var (
	_ xmlstream.WriterTo  = oob.IQ{}
	_ xmlstream.Marshaler = oob.IQ{}
	_ xmlstream.WriterTo  = oob.Query{}
	_ xmlstream.Marshaler = oob.Query{}
	_ xmlstream.WriterTo  = oob.Data{}
	_ xmlstream.Marshaler = oob.Data{}
)

func TestEncodeIQ(t *testing.T) {
	j := jid.MustParse("feste@example.net")

	iq := oob.IQ{
		IQ: stanza.IQ{To: j},
		Query: oob.Query{
			URL:  "url",
			Desc: "desc",
		},
	}

	t.Run("marshal", func(t *testing.T) {
		out, err := xml.Marshal(iq)
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		const expected = `<iq id="" to="feste@example.net" from="" type="get"><query xmlns="jabber:iq:oob"><url>url</url><desc>desc</desc></query></iq>`
		if string(out) != expected {
			t.Errorf("wrong encoding:\nwant=%s,\n got=%s", expected, out)
		}
	})

	t.Run("write", func(t *testing.T) {
		var b strings.Builder
		e := xml.NewEncoder(&b)
		_, err := iq.WriteXML(e)
		if err != nil {
			t.Fatalf("error writing XML token stream: %v", err)
		}
		err = e.Flush()
		if err != nil {
			t.Fatalf("error flushing token stream: %v", err)
		}

		const expected = `<iq type="" to="feste@example.net"><query xmlns="jabber:iq:oob"><url>url</url><desc>desc</desc></query></iq>`
		if streamOut := b.String(); streamOut != expected {
			t.Errorf("wrong stream encoding:\nwant=%s,\n got=%s", expected, streamOut)
		}
	})
}

func TestEncodeData(t *testing.T) {
	data := oob.Data{
		URL:  "url",
		Desc: "desc",
	}

	t.Run("marshal", func(t *testing.T) {
		out, err := xml.Marshal(data)
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		const expected = `<x xmlns="jabber:x:oob"><url>url</url><desc>desc</desc></x>`
		if string(out) != expected {
			t.Errorf("wrong encoding:\nwant=%s,\n got=%s", expected, out)
		}
	})

	t.Run("write", func(t *testing.T) {
		var b strings.Builder
		e := xml.NewEncoder(&b)
		_, err := data.WriteXML(e)
		if err != nil {
			t.Fatalf("error writing XML token stream: %v", err)
		}
		err = e.Flush()
		if err != nil {
			t.Fatalf("error flushing token stream: %v", err)
		}

		const expected = `<x xmlns="jabber:x:oob"><url>url</url><desc>desc</desc></x>`
		if streamOut := b.String(); streamOut != expected {
			t.Errorf("wrong stream encoding:\nwant=%s,\n got=%s", expected, streamOut)
		}
	})
}

func TestEncodeQuery(t *testing.T) {
	// This test both ensures that Query's WriteXML method is tested (it is not
	// used when encoding an IQ that contains a query) and makes sure that desc is
	// optional but URL is not.
	query := oob.Query{}

	t.Run("marshal", func(t *testing.T) {
		out, err := xml.Marshal(query)
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		const expected = `<query xmlns="jabber:iq:oob"><url></url></query>`
		if string(out) != expected {
			t.Errorf("wrong encoding:\nwant=%s,\n got=%s", expected, out)
		}
	})

	t.Run("write", func(t *testing.T) {
		var b strings.Builder
		e := xml.NewEncoder(&b)
		_, err := query.WriteXML(e)
		if err != nil {
			t.Fatalf("error writing XML token stream: %v", err)
		}
		err = e.Flush()
		if err != nil {
			t.Fatalf("error flushing token stream: %v", err)
		}

		const expected = `<query xmlns="jabber:iq:oob"><url></url></query>`
		if streamOut := b.String(); streamOut != expected {
			t.Errorf("wrong stream encoding:\nwant=%s,\n got=%s", expected, streamOut)
		}
	})
}