mirror of https://github.com/coder/coder.git
fix: actually test `httpapi.WebsocketCloseSprintf` (#6261)
This commit is contained in:
parent
a79f4a095d
commit
19ae411f05
|
@ -182,7 +182,7 @@ func WebsocketCloseSprintf(format string, vars ...any) string {
|
|||
if len(msg) > websocketCloseMaxLen {
|
||||
// Trim the string to 123 bytes. If we accidentally cut in the middle of
|
||||
// a UTF-8 character, remove it from the string.
|
||||
return strings.ToValidUTF8(string(msg[123]), "")
|
||||
return strings.ToValidUTF8(msg[:websocketCloseMaxLen], "")
|
||||
}
|
||||
|
||||
return msg
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
|
@ -122,15 +123,28 @@ func TestRead(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func WebsocketCloseMsg(t *testing.T) {
|
||||
func TestWebsocketCloseMsg(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("Sprintf", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var (
|
||||
msg = "this is my message %q %q"
|
||||
opts = []any{"colin", "kyle"}
|
||||
)
|
||||
|
||||
expected := fmt.Sprintf(msg, opts...)
|
||||
got := httpapi.WebsocketCloseSprintf(msg, opts...)
|
||||
assert.Equal(t, expected, got)
|
||||
})
|
||||
|
||||
t.Run("TruncateSingleByteCharacters", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
msg := strings.Repeat("d", 255)
|
||||
trunc := httpapi.WebsocketCloseSprintf(msg)
|
||||
assert.LessOrEqual(t, len(trunc), 123)
|
||||
assert.Equal(t, len(trunc), 123)
|
||||
})
|
||||
|
||||
t.Run("TruncateMultiByteCharacters", func(t *testing.T) {
|
||||
|
@ -138,6 +152,6 @@ func WebsocketCloseMsg(t *testing.T) {
|
|||
|
||||
msg := strings.Repeat("こんにちは", 10)
|
||||
trunc := httpapi.WebsocketCloseSprintf(msg)
|
||||
assert.LessOrEqual(t, len(trunc), 123)
|
||||
assert.Equal(t, len(trunc), 123)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue