~samwhited/xmpp

ref: de40e6f9107a5d25ae8c8edad9289fd0c4b12e2f xmpp/disco/info_test.go -rw-r--r-- 3.0 KiB
de40e6f9Sam Whited roster: add type to IQ response in test 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
104
// 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 disco_test

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

	"mellium.im/xmpp/disco"
)

func TestMarshalQuery(t *testing.T) {
	const expected = `<query xmlns="http://jabber.org/protocol/disco#info" node="test"></query>`
	query := disco.InfoQuery{Node: "test"}
	t.Run("Marshal", func(t *testing.T) {
		b, err := xml.Marshal(query)
		if err != nil {
			t.Fatalf("unexpected error marshaling query: %v", err)
		}
		if !bytes.Equal(b, []byte(expected)) {
			t.Fatalf("wrong output:\nwant=%s,\n got=%s", expected, b)
		}
	})
	t.Run("Encode", func(t *testing.T) {
		var buf bytes.Buffer
		e := xml.NewEncoder(&buf)
		_, err := query.WriteXML(e)
		if err != nil {
			t.Fatalf("unexpected error marshaling query: %v", err)
		}
		err = e.Flush()
		if err != nil {
			t.Fatalf("unexpected error flushing: %v", err)
		}
		if out := buf.String(); out != expected {
			t.Fatalf("wrong output:\nwant=%s,\n got=%s", expected, out)
		}
	})
}

func TestUnmarshal(t *testing.T) {
	infoResp := `<query node="test" xmlns='http://jabber.org/protocol/disco#info'>
  <identity
      category='conference'
      type='text'
      name='Play-Specific Chatrooms'
			xml:lang='en'/>
  <identity
      category='directory'
      type='chatroom'
      name='Play-Specific Chatrooms'/>
  <feature var='http://jabber.org/protocol/disco#info'/>
  <feature var='http://jabber.org/protocol/disco#items'/>
  <feature var='http://jabber.org/protocol/muc'/>
  <x xmlns='jabber:x:data' type='result'>
    <field var='FORM_TYPE' type='hidden'>
      <value>http://jabber.org/network/serverinfo</value>
    </field>
    <field var='c2s_port'>
      <value>5222</value>
    </field>
  </x>
</query>`
	var info disco.Info
	err := xml.Unmarshal([]byte(infoResp), &info)
	if err != nil {
		t.Fatalf("unexpected error unmarshaling: %v", err)
	}
	if info.Node != "test" {
		t.Errorf("node did not unmarshal correctly: want=test, got=%s", info.Node)
	}
	if l := len(info.Identity); l != 2 {
		t.Errorf("wrong number of identities: want=2, got=%d", l)
	}
	ident := disco.Identity{
		XMLName:  xml.Name{Space: disco.NSInfo, Local: "identity"},
		Category: "conference",
		Type:     "text",
		Name:     "Play-Specific Chatrooms",
		Lang:     "en",
	}
	if ident != info.Identity[0] {
		t.Errorf("wrong identity: want=%v, got=%v", ident, info.Identity[0])
	}
	if l := len(info.Features); l != 3 {
		t.Errorf("wrong number of features: want=3, got=%d", l)
	}
	if v := info.Features[0].Var; v != disco.NSInfo {
		t.Errorf("wrong first feature: want=%s, got=%s", disco.NSInfo, v)
	}
	if info.Form == nil {
		t.Errorf("form was not unmarshaled")
	}
	const serverInfo = "http://jabber.org/network/serverinfo"
	if s, ok := info.Form.GetString("FORM_TYPE"); !ok || s != serverInfo {
		t.Errorf("wrong value for FORM_TYPE: want=%s, got=%s", serverInfo, s)
	}
	if s, ok := info.Form.GetString("c2s_port"); !ok || s != "5222" {
		t.Errorf("wrong value for FORM_TYPE: want=5222, got=%s", s)
	}
}