~samwhited/xmpp

ref: 7da78eb2b3b378064f931bf2bf2a9c07adbf315d xmpp/internal/xmpptest/session_test.go -rw-r--r-- 1.4 KiB
7da78eb2Sam Whited xmpptest: add a client/server test mechanism 3 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
// Copyright 2017 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 xmpptest_test

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

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

func TestNewSession(t *testing.T) {
	state := xmpp.Secure | xmpp.InputStreamClosed
	buf := new(bytes.Buffer)
	s := xmpptest.NewSession(state, buf)

	if mask := s.State(); mask != state|xmpp.Ready {
		t.Errorf("Got invalid state value: want=%d, got=%d", state, mask)
	}

	if out := buf.String(); out != "" {
		t.Errorf("Buffer wrote unexpected tokens: `%s'", out)
	}
}

func TestNewClient(t *testing.T) {
	state := xmpp.Secure
	s := xmpptest.NewClientServer(state, xmpp.HandlerFunc(func(t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
		iq, err := stanza.NewIQ(*start)
		if err != nil {
			panic(err)
		}
		r := iq.Result(nil)
		_, err = xmlstream.Copy(t, r)
		return err
	}))
	origIQ := struct {
		stanza.IQ
	}{
		IQ: stanza.IQ{
			ID: "123",
		},
	}
	resp, err := s.EncodeIQ(context.Background(), origIQ)
	if err != nil {
		t.Errorf("error encoding IQ: %v", err)
	}
	iq := stanza.IQ{}
	err = xml.NewTokenDecoder(resp).Decode(&iq)
	if err != nil {
		t.Errorf("error decoding response: %v", err)
	}
	if iq.ID != origIQ.ID {
		t.Errorf("Response IQ had wrong ID: want=%s, got=%s", origIQ.ID, iq.ID)
	}
}