Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 1f2a6c5957
Fetching contributors…

Cannot retrieve contributors at this time

122 lines (92 sloc) 2.465 kB
package nats
import (
"reflect"
"bufio"
"strings"
"testing"
)
// Create a bufio.Reader
func createReader(payload string) *bufio.Reader {
sio := strings.NewReader(payload)
return bufio.NewReader(sio)
}
func testReadMatch(t *testing.T, payload string, expected readObject) {
var rd *bufio.Reader
rd = createReader(payload)
var obj readObject
obj, _ = read(rd)
if !reflect.DeepEqual(expected, obj) {
t.Errorf("Expected: %#v, got: %#v", expected, obj)
return
}
}
func testReadError(t *testing.T, payload string) {
var rd *bufio.Reader
rd = createReader(payload)
var err error
_, err = read(rd)
if err == nil {
t.Errorf("Expected not nil")
return
}
}
func TestReadMessage(t *testing.T) {
var expected = &readMessage{
Subscription: []byte("sub"),
SubscriptionId: 1234,
ReplyTo: nil,
Payload: []byte("some message"),
}
testReadMatch(t, "msg sub 1234 12\r\nsome message\r\n", expected)
}
func TestReadMessageWithNegativeSubscriptionId(t *testing.T) {
testReadError(t, "msg sub -1234 12\r\nsome message\r\n")
}
func TestReadMessageWithInvalidSubscriptionId(t *testing.T) {
testReadError(t, "msg sub xxxx 12\r\nsome message\r\n")
}
func TestReadMessageWithNegativeByteCount(t *testing.T) {
testReadError(t, "msg sub 1234 -12\r\nsome message\r\n")
}
func TestReadMessageWithInvalidByteCount(t *testing.T) {
testReadError(t, "msg sub 1234 xx\r\nsome message\r\n")
}
func TestReadMessageWithEarlyEof(t *testing.T) {
testReadError(t, "msg sub 1234 12\r\nsome message\r")
}
func TestReadOk(t *testing.T) {
var expected = &readOk{}
testReadMatch(t, "+ok\r\n", expected)
}
func TestReadErrWithoutPayload(t *testing.T) {
var expected = &readErr{
Payload: nil,
}
testReadMatch(t, "-err\r\n", expected)
}
func TestReadErrWithPayload(t *testing.T) {
var expected = &readErr{
Payload: []byte("foo"),
}
testReadMatch(t, "-err foo\r\n", expected)
}
func TestReadErrWithPayloadWithWhiteSpace(t *testing.T) {
var expected = &readErr{
Payload: []byte("foo bar qux"),
}
testReadMatch(t, "-err foo bar qux\r\n", expected)
}
func TestReadPing(t *testing.T) {
var expected = &readPing{}
testReadMatch(t, "ping\r\n", expected)
}
func TestReadPong(t *testing.T) {
var expected = &readPong{}
testReadMatch(t, "pong\r\n", expected)
}
func TestReadInfo(t *testing.T) {
var expected = &readInfo{
ServerId: "some id",
}
testReadMatch(t, "info {\"server_id\":\"some id\" }\r\n", expected)
}
Jump to Line
Something went wrong with that request. Please try again.