Skip to content

Commit 9e32354

Browse files
committed
Fix randString method in tests
1 parent b6b56b7 commit 9e32354

File tree

5 files changed

+76
-15
lines changed

5 files changed

+76
-15
lines changed

assert_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func assertJSONRead(t *testing.T, ctx context.Context, c *websocket.Conn, exp in
4545

4646
func randString(t *testing.T, n int) string {
4747
s := strings.ToValidUTF8(string(randBytes(t, n)), "_")
48+
s = strings.ReplaceAll(s, "\x00", "_")
4849
if len(s) > n {
4950
return s[:n]
5051
}

ci/image/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM golang:1
22

33
RUN apt-get update
4-
RUN apt-get install -y chromium
4+
RUN apt-get install -y chromium npm
55

66
ENV GOFLAGS="-mod=readonly"
77
ENV PAGER=cat

conn_test.go

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,71 @@ import (
1818
"nhooyr.io/websocket"
1919
)
2020

21+
func TestFuzz(t *testing.T) {
22+
t.Parallel()
23+
24+
s, closeFn := testServer(t, func(w http.ResponseWriter, r *http.Request) {
25+
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
26+
CompressionOptions: websocket.CompressionOptions{
27+
Mode: websocket.CompressionContextTakeover,
28+
},
29+
})
30+
assert.Success(t, "accept", err)
31+
defer c.Close(websocket.StatusInternalError, "")
32+
33+
err = echoLoop(r.Context(), c)
34+
assertCloseStatus(t, websocket.StatusNormalClosure, err)
35+
}, false)
36+
defer closeFn()
37+
38+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
39+
defer cancel()
40+
41+
opts := &websocket.DialOptions{
42+
CompressionOptions: websocket.CompressionOptions{
43+
Mode: websocket.CompressionContextTakeover,
44+
},
45+
}
46+
opts.HTTPClient = s.Client()
47+
48+
c, _, err := websocket.Dial(ctx, wsURL(s), opts)
49+
assert.Success(t, "dial", err)
50+
assertJSONEcho(t, ctx, c, 8393)
51+
}
52+
2153
func TestConn(t *testing.T) {
2254
t.Parallel()
2355

2456
t.Run("json", func(t *testing.T) {
2557
s, closeFn := testServer(t, func(w http.ResponseWriter, r *http.Request) {
2658
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
27-
Subprotocols: []string{"echo"},
28-
InsecureSkipVerify: true,
59+
Subprotocols: []string{"echo"},
2960
CompressionOptions: websocket.CompressionOptions{
30-
Mode: websocket.CompressionContextTakeover,
31-
Threshold: 1,
61+
Mode: websocket.CompressionContextTakeover,
3262
},
3363
})
3464
assert.Success(t, "accept", err)
3565
defer c.Close(websocket.StatusInternalError, "")
3666

3767
err = echoLoop(r.Context(), c)
38-
t.Logf("server: %v", err)
3968
assertCloseStatus(t, websocket.StatusNormalClosure, err)
4069
}, false)
4170
defer closeFn()
4271

43-
wsURL := strings.Replace(s.URL, "http", "ws", 1)
44-
4572
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
4673
defer cancel()
4774

4875
opts := &websocket.DialOptions{
4976
Subprotocols: []string{"echo"},
5077
CompressionOptions: websocket.CompressionOptions{
51-
Mode: websocket.CompressionContextTakeover,
52-
Threshold: 1,
78+
Mode: websocket.CompressionContextTakeover,
5379
},
5480
}
5581
opts.HTTPClient = s.Client()
5682

57-
c, _, err := websocket.Dial(ctx, wsURL, opts)
83+
c, _, err := websocket.Dial(ctx, wsURL(s), opts)
5884
assert.Success(t, "dial", err)
59-
assertJSONEcho(t, ctx, c, 2)
85+
assertJSONEcho(t, ctx, c, 8393)
6086
})
6187
}
6288

@@ -149,3 +175,7 @@ func echoLoop(ctx context.Context, c *websocket.Conn) error {
149175
}
150176
}
151177
}
178+
179+
func wsURL(s *httptest.Server) string {
180+
return strings.Replace(s.URL, "http", "ws", 1)
181+
}

internal/errd/wrap.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
11
package errd
22

3-
import "golang.org/x/xerrors"
3+
import (
4+
"fmt"
5+
6+
"golang.org/x/xerrors"
7+
)
8+
9+
type wrapError struct {
10+
msg string
11+
err error
12+
frame xerrors.Frame
13+
}
14+
15+
func (e *wrapError) Error() string {
16+
return fmt.Sprint(e)
17+
}
18+
19+
func (e *wrapError) Format(s fmt.State, v rune) { xerrors.FormatError(e, s, v) }
20+
21+
func (e *wrapError) FormatError(p xerrors.Printer) (next error) {
22+
p.Print(e.msg)
23+
e.frame.Format(p)
24+
return e.err
25+
}
26+
27+
func (e *wrapError) Unwrap() error {
28+
return e.err
29+
}
430

531
// Wrap wraps err with xerrors.Errorf if err is non nil.
632
// Intended for use with defer and a named error return.
733
// Inspired by https://github.com/golang/go/issues/32676.
834
func Wrap(err *error, f string, v ...interface{}) {
935
if *err != nil {
10-
*err = xerrors.Errorf(f+": %w", append(v, *err)...)
36+
*err = &wrapError{
37+
msg: fmt.Sprintf(f, v...),
38+
err: *err,
39+
frame: xerrors.Caller(1),
40+
}
1141
}
1242
}

write.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (mw *msgWriter) Write(p []byte) (_ int, err error) {
145145
return 0, xerrors.New("cannot use closed writer")
146146
}
147147

148-
// TODO can make threshold detection robust across writes by writing to buffer
148+
// TODO can make threshold detection robust across writes by writing to bufio writer
149149
if mw.flate ||
150150
mw.c.flate() && len(p) >= mw.c.flateThreshold {
151151
mw.ensureFlate()

0 commit comments

Comments
 (0)