join_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright 2019 The Gorilla WebSocket Authors. 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. "io"
  8. "strings"
  9. "testing"
  10. )
  11. func TestJoinMessages(t *testing.T) {
  12. messages := []string{"a", "bc", "def", "ghij", "klmno", "0", "12", "345", "6789"}
  13. for _, readChunk := range []int{1, 2, 3, 4, 5, 6, 7} {
  14. for _, term := range []string{"", ","} {
  15. var connBuf bytes.Buffer
  16. wc := newTestConn(nil, &connBuf, true)
  17. rc := newTestConn(&connBuf, nil, false)
  18. for _, m := range messages {
  19. wc.WriteMessage(BinaryMessage, []byte(m))
  20. }
  21. var result bytes.Buffer
  22. _, err := io.CopyBuffer(&result, JoinMessages(rc, term), make([]byte, readChunk))
  23. if IsUnexpectedCloseError(err, CloseAbnormalClosure) {
  24. t.Errorf("readChunk=%d, term=%q: unexpected error %v", readChunk, term, err)
  25. }
  26. want := strings.Join(messages, term) + term
  27. if result.String() != want {
  28. t.Errorf("readChunk=%d, term=%q, got %q, want %q", readChunk, term, result.String(), want)
  29. }
  30. }
  31. }
  32. }