~samwhited/xmpp

d390c65259df9d65451c7919907b9bf404b057ed — Sam Whited 4 years ago 256a70e
internal/xmpptest: new package for testing
2 files changed, 62 insertions(+), 0 deletions(-)

A internal/xmpptest/session.go
A internal/xmpptest/session_test.go
A internal/xmpptest/session.go => internal/xmpptest/session.go +35 -0
@@ 0,0 1,35 @@
// Copyright 2017 Sam Whited.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

// Package xmpptest provides utilities for XMPP testing.
package xmpptest // import "mellium.im/xmpp/internal/xmpptest"

import (
	"context"
	"io"

	"mellium.im/xmpp"
	"mellium.im/xmpp/jid"
)

// NewSession returns a new XMPP session with the state bits set to
// state|xmpp.Ready.
//
// NewSession panics on error for ease of use in testing, where a panic is
// acceptable.
func NewSession(state xmpp.SessionState, rw io.ReadWriter) *xmpp.Session {
	location := jid.MustParse("example.net")
	origin := jid.MustParse("test@example.net")

	s, err := xmpp.NegotiateSession(
		context.Background(), location, origin, rw,
		func(_ context.Context, _ *xmpp.Session, _ interface{}) (xmpp.SessionState, io.ReadWriter, interface{}, error) {
			return state | xmpp.Ready, nil, nil, nil
		},
	)
	if err != nil {
		panic(err)
	}
	return s
}

A internal/xmpptest/session_test.go => internal/xmpptest/session_test.go +27 -0
@@ 0,0 1,27 @@
// Copyright 2017 Sam Whited.
// 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"
	"testing"

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

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)
	}
}