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