json_test.go 692 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2013 Gary Burd. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package websocket
  5. import (
  6. "bytes"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestJSON(t *testing.T) {
  11. var buf bytes.Buffer
  12. c := fakeNetConn{&buf, &buf}
  13. wc := newConn(c, true, 1024, 1024)
  14. rc := newConn(c, false, 1024, 1024)
  15. var actual, expect struct {
  16. A int
  17. B string
  18. }
  19. expect.A = 1
  20. expect.B = "hello"
  21. if err := WriteJSON(wc, &expect); err != nil {
  22. t.Fatal("write", err)
  23. }
  24. if err := ReadJSON(rc, &actual); err != nil {
  25. t.Fatal("read", err)
  26. }
  27. if !reflect.DeepEqual(&actual, &expect) {
  28. t.Fatal("equal", actual, expect)
  29. }
  30. }