2013-10-16 20:41:47 +04:00
|
|
|
// Copyright 2013 Gary Burd. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestJSON(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
c := fakeNetConn{&buf, &buf}
|
|
|
|
wc := newConn(c, true, 1024, 1024)
|
|
|
|
rc := newConn(c, false, 1024, 1024)
|
|
|
|
|
|
|
|
var actual, expect struct {
|
|
|
|
A int
|
|
|
|
B string
|
|
|
|
}
|
|
|
|
expect.A = 1
|
|
|
|
expect.B = "hello"
|
|
|
|
|
2013-10-26 12:13:32 +04:00
|
|
|
if err := wc.WriteJSON(&expect); err != nil {
|
2013-10-16 20:41:47 +04:00
|
|
|
t.Fatal("write", err)
|
|
|
|
}
|
|
|
|
|
2013-10-27 22:56:52 +04:00
|
|
|
if err := rc.ReadJSON(&actual); err != nil {
|
2013-10-27 22:50:42 +04:00
|
|
|
t.Fatal("read", err)
|
|
|
|
}
|
|
|
|
|
2013-10-27 22:56:52 +04:00
|
|
|
if !reflect.DeepEqual(&actual, &expect) {
|
|
|
|
t.Fatal("equal", actual, expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeprecatedJSON(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
c := fakeNetConn{&buf, &buf}
|
|
|
|
wc := newConn(c, true, 1024, 1024)
|
|
|
|
rc := newConn(c, false, 1024, 1024)
|
|
|
|
|
|
|
|
var actual, expect struct {
|
|
|
|
A int
|
|
|
|
B string
|
|
|
|
}
|
|
|
|
expect.A = 1
|
|
|
|
expect.B = "hello"
|
|
|
|
|
|
|
|
if err := WriteJSON(wc, &expect); err != nil {
|
|
|
|
t.Fatal("write", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ReadJSON(rc, &actual); err != nil {
|
2013-10-16 20:41:47 +04:00
|
|
|
t.Fatal("read", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(&actual, &expect) {
|
|
|
|
t.Fatal("equal", actual, expect)
|
|
|
|
}
|
|
|
|
}
|