~samwhited/xmpp

ref: f049ab8dabe39d7cf5b125095ce050d2bfd2c6fd xmpp/send_test.go -rw-r--r-- 3.2 KiB
f049ab8dSam Whited all: remove unnecessary pointer in WrapPresence 1 year, 9 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
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
139
140
141
142
143
144
145
146
147
148
// Copyright 2019 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 xmpp_test

import (
	"bytes"
	"context"
	"encoding/xml"
	"errors"
	"io"
	"strconv"
	"testing"
	"time"

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

const (
	testIQID = "123"
)

type errReader struct{ err error }

func (r errReader) Token() (xml.Token, error) {
	return nil, r.err
}

type toksReader []xml.Token

func (r *toksReader) Token() (xml.Token, error) {
	if len(*r) == 0 {
		return nil, io.EOF
	}

	var t xml.Token
	t, *r = (*r)[0], (*r)[1:]
	return t, nil
}

var (
	errExpected = errors.New("Expected error")
	to          = jid.MustParse("test@example.net")
)

var sendTests = [...]struct {
	r          xml.TokenReader
	err        error
	resp       xml.TokenReader
	writesBody bool
}{
	0: {
		r:   errReader{err: errExpected},
		err: errExpected,
	},
	1: {
		r: &toksReader{
			xml.EndElement{Name: xml.Name{Local: "iq"}},
		},
		err: xmpp.ErrNotStart,
	},
	2: {
		r:          stanza.WrapMessage(to, stanza.NormalMessage, nil),
		writesBody: true,
	},
	3: {
		r:          stanza.WrapPresence(to, stanza.AvailablePresence, nil),
		writesBody: true,
	},
	4: {
		r:          stanza.WrapIQ(&stanza.IQ{Type: stanza.ResultIQ}, nil),
		writesBody: true,
	},
	5: {
		r:          stanza.WrapIQ(&stanza.IQ{Type: stanza.ErrorIQ}, nil),
		writesBody: true,
	},
	6: {
		r:          stanza.WrapIQ(&stanza.IQ{ID: testIQID, Type: stanza.GetIQ}, nil),
		writesBody: true,
		resp:       stanza.WrapIQ(&stanza.IQ{ID: testIQID, Type: stanza.ResultIQ}, nil),
	},
	7: {
		r:          stanza.WrapIQ(&stanza.IQ{ID: testIQID, Type: stanza.SetIQ}, nil),
		writesBody: true,
		resp:       stanza.WrapIQ(&stanza.IQ{ID: testIQID, Type: stanza.ErrorIQ}, nil),
	},
}

func TestSend(t *testing.T) {
	for i, tc := range sendTests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			br := &bytes.Buffer{}
			bw := &bytes.Buffer{}
			s := xmpptest.NewSession(0, struct {
				io.Reader
				io.Writer
			}{
				Reader: br,
				Writer: bw,
			})
			if tc.resp != nil {
				e := xml.NewEncoder(br)
				_, err := xmlstream.Copy(e, tc.resp)
				if err != nil {
					t.Logf("error responding: %q", err)
				}
				err = e.Flush()
				if err != nil {
					t.Logf("error flushing after responding: %q", err)
				}
			}

			go func() {
				err := s.Serve(nil)
				if err != nil && err != io.EOF {
					panic(err)
				}
			}()

			ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second))
			defer cancel()
			resp, err := s.Send(ctx, tc.r)
			if err != tc.err {
				t.Errorf("Unexpected error, want=%q, got=%q", tc.err, err)
			}
			if empty := bw.Len() != 0; tc.writesBody != empty {
				t.Errorf("Unexpected body, want=%t, got=%t", tc.writesBody, empty)
			}
			switch {
			case resp == nil && tc.resp != nil:
				t.Fatalf("Expected response, but got none")
			case resp != nil && tc.resp == nil:
				buf := &bytes.Buffer{}
				_, err := xmlstream.Copy(xml.NewEncoder(buf), resp)
				if err != nil {
					t.Fatalf("Error encoding unexpected response")
				}
				t.Fatalf("Did not expect response, but got: %s", buf.String())
			}
		})
	}
}