~samwhited/xmpp

ref: 52be89df867be9fc4e8ba83b0c3880887a6485ba xmpp/session_test.go -rw-r--r-- 1.5 KiB
52be89dfSam Whited form: Rename Option and FieldOption types 4 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2016 Sam Whited.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp

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

// ping is an XEP-0199 ping
type ping struct {
	IQ
	Ping struct{} `xml:"urn:xmpp:ping ping"`
}

type dummyMsg struct {
	Message
	Dummy struct{}
}

type dummyPresence struct {
	Presence
	Dummy struct{}
}

func newDummySession() (*bytes.Buffer, *Session) {
	b := new(bytes.Buffer)
	s := &Session{
		rw:         b,
		features:   make(map[string]interface{}),
		negotiated: make(map[string]struct{}),
	}
	s.out.e = xml.NewEncoder(b)
	return b, s
}

func TestSendDoesNotMutateStanza(t *testing.T) {
	_, s := newDummySession()
	p := &ping{}
	err := s.Send(p)
	if err != nil {
		t.Fatal(err)
	}
	p2 := ping{}
	if *p != p2 {
		t.Fatalf("Sending mutated original struct")
	}
}

func TestEnforceStanzaSemantics(t *testing.T) {
	t.Run("IQ", func(t *testing.T) {
		s := enforceStanzaSemantics(ping{})
		if s.(ping).ID == "" {
			t.Fatal("Expected ID to be set")
		}
	})
	t.Run("Message", func(t *testing.T) {
		s := enforceStanzaSemantics(dummyMsg{})
		if s.(dummyMsg).ID == "" {
			t.Fatal("Expected ID to be set")
		}
	})
	t.Run("Message", func(t *testing.T) {
		s := enforceStanzaSemantics(dummyPresence{})
		if s.(dummyPresence).ID == "" {
			t.Fatal("Expected ID to be set")
		}
	})
	t.Run("Nonza", func(t *testing.T) {
		var a interface{}
		a = struct{}{}
		b := enforceStanzaSemantics(a)
		if a != b {
			t.Fatal("Expected the same type of nonza to be returned")
		}
	})
}