Skip to content

Commit 537b26b

Browse files
committed
Change options to be pointer structures
Closes #122
1 parent 679ddb8 commit 537b26b

File tree

8 files changed

+50
-38
lines changed

8 files changed

+50
-38
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ For a production quality example that shows off the full API, see the [echo exam
3434

3535
```go
3636
http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
37-
c, err := websocket.Accept(w, r, websocket.AcceptOptions{})
37+
c, err := websocket.Accept(w, r, nil)
3838
if err != nil {
3939
// ...
4040
}
@@ -64,7 +64,7 @@ in net/http](https://github.com/golang/go/issues/26937#issuecomment-415855861) t
6464
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
6565
defer cancel()
6666

67-
c, _, err := websocket.Dial(ctx, "ws://localhost:8080", websocket.DialOptions{})
67+
c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)
6868
if err != nil {
6969
// ...
7070
}

accept.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,19 @@ func verifyClientRequest(w http.ResponseWriter, r *http.Request) error {
8484
//
8585
// If an error occurs, Accept will always write an appropriate response so you do not
8686
// have to.
87-
func Accept(w http.ResponseWriter, r *http.Request, opts AcceptOptions) (*Conn, error) {
87+
func Accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn, error) {
8888
c, err := accept(w, r, opts)
8989
if err != nil {
9090
return nil, xerrors.Errorf("failed to accept websocket connection: %w", err)
9191
}
9292
return c, nil
9393
}
9494

95-
func accept(w http.ResponseWriter, r *http.Request, opts AcceptOptions) (*Conn, error) {
95+
func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn, error) {
96+
if opts == nil {
97+
opts = &AcceptOptions{}
98+
}
99+
96100
err := verifyClientRequest(w, r)
97101
if err != nil {
98102
return nil, err

accept_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestAccept(t *testing.T) {
1515
w := httptest.NewRecorder()
1616
r := httptest.NewRequest("GET", "/", nil)
1717

18-
_, err := Accept(w, r, AcceptOptions{})
18+
_, err := Accept(w, r, nil)
1919
if err == nil {
2020
t.Fatalf("unexpected error value: %v", err)
2121
}
@@ -32,7 +32,7 @@ func TestAccept(t *testing.T) {
3232
r.Header.Set("Sec-WebSocket-Version", "13")
3333
r.Header.Set("Sec-WebSocket-Key", "meow123")
3434

35-
_, err := Accept(w, r, AcceptOptions{})
35+
_, err := Accept(w, r, nil)
3636
if err == nil || !strings.Contains(err.Error(), "http.Hijacker") {
3737
t.Fatalf("unexpected error value: %v", err)
3838
}

dial.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,23 @@ type DialOptions struct {
4141
// This function requires at least Go 1.12 to succeed as it uses a new feature
4242
// in net/http to perform WebSocket handshakes and get a writable body
4343
// from the transport. See https://github.com/golang/go/issues/26937#issuecomment-415855861
44-
func Dial(ctx context.Context, u string, opts DialOptions) (*Conn, *http.Response, error) {
44+
func Dial(ctx context.Context, u string, opts *DialOptions) (*Conn, *http.Response, error) {
4545
c, r, err := dial(ctx, u, opts)
4646
if err != nil {
4747
return nil, r, xerrors.Errorf("failed to websocket dial: %w", err)
4848
}
4949
return c, r, nil
5050
}
5151

52-
func dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Response, err error) {
52+
func dial(ctx context.Context, u string, opts *DialOptions) (_ *Conn, _ *http.Response, err error) {
53+
if opts == nil {
54+
opts = &DialOptions{}
55+
}
56+
57+
// Shallow copy to ensure defaults do not affect user passed options.
58+
opts2 := *opts
59+
opts = &opts2
60+
5361
if opts.HTTPClient == nil {
5462
opts.HTTPClient = http.DefaultClient
5563
}

dial_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestBadDials(t *testing.T) {
1414
testCases := []struct {
1515
name string
1616
url string
17-
opts DialOptions
17+
opts *DialOptions
1818
}{
1919
{
2020
name: "badURL",
@@ -27,7 +27,7 @@ func TestBadDials(t *testing.T) {
2727
{
2828
name: "badHTTPClient",
2929
url: "ws://nhooyr.io",
30-
opts: DialOptions{
30+
opts: &DialOptions{
3131
HTTPClient: &http.Client{
3232
Timeout: time.Minute,
3333
},

example_echo_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func Example_echo() {
6868
func echoServer(w http.ResponseWriter, r *http.Request) error {
6969
log.Printf("serving %v", r.RemoteAddr)
7070

71-
c, err := websocket.Accept(w, r, websocket.AcceptOptions{
71+
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
7272
Subprotocols: []string{"echo"},
7373
})
7474
if err != nil {
@@ -128,7 +128,7 @@ func client(url string) error {
128128
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
129129
defer cancel()
130130

131-
c, _, err := websocket.Dial(ctx, url, websocket.DialOptions{
131+
c, _, err := websocket.Dial(ctx, url, &websocket.DialOptions{
132132
Subprotocols: []string{"echo"},
133133
})
134134
if err != nil {

example_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// message from the client and then closes the connection.
1515
func ExampleAccept() {
1616
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
17-
c, err := websocket.Accept(w, r, websocket.AcceptOptions{})
17+
c, err := websocket.Accept(w, r, nil)
1818
if err != nil {
1919
log.Println(err)
2020
return
@@ -46,7 +46,7 @@ func ExampleDial() {
4646
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
4747
defer cancel()
4848

49-
c, _, err := websocket.Dial(ctx, "ws://localhost:8080", websocket.DialOptions{})
49+
c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)
5050
if err != nil {
5151
log.Fatal(err)
5252
}
@@ -64,7 +64,7 @@ func ExampleDial() {
6464
// on which you will only write and do not expect to read data messages.
6565
func Example_writeOnly() {
6666
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
67-
c, err := websocket.Accept(w, r, websocket.AcceptOptions{})
67+
c, err := websocket.Accept(w, r, nil)
6868
if err != nil {
6969
log.Println(err)
7070
return

websocket_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestHandshake(t *testing.T) {
4444
{
4545
name: "handshake",
4646
server: func(w http.ResponseWriter, r *http.Request) error {
47-
c, err := websocket.Accept(w, r, websocket.AcceptOptions{
47+
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
4848
Subprotocols: []string{"myproto"},
4949
})
5050
if err != nil {
@@ -54,7 +54,7 @@ func TestHandshake(t *testing.T) {
5454
return nil
5555
},
5656
client: func(ctx context.Context, u string) error {
57-
c, resp, err := websocket.Dial(ctx, u, websocket.DialOptions{
57+
c, resp, err := websocket.Dial(ctx, u, &websocket.DialOptions{
5858
Subprotocols: []string{"myproto"},
5959
})
6060
if err != nil {
@@ -81,7 +81,7 @@ func TestHandshake(t *testing.T) {
8181
{
8282
name: "defaultSubprotocol",
8383
server: func(w http.ResponseWriter, r *http.Request) error {
84-
c, err := websocket.Accept(w, r, websocket.AcceptOptions{})
84+
c, err := websocket.Accept(w, r, nil)
8585
if err != nil {
8686
return err
8787
}
@@ -93,7 +93,7 @@ func TestHandshake(t *testing.T) {
9393
return nil
9494
},
9595
client: func(ctx context.Context, u string) error {
96-
c, _, err := websocket.Dial(ctx, u, websocket.DialOptions{
96+
c, _, err := websocket.Dial(ctx, u, &websocket.DialOptions{
9797
Subprotocols: []string{"meow"},
9898
})
9999
if err != nil {
@@ -110,7 +110,7 @@ func TestHandshake(t *testing.T) {
110110
{
111111
name: "subprotocol",
112112
server: func(w http.ResponseWriter, r *http.Request) error {
113-
c, err := websocket.Accept(w, r, websocket.AcceptOptions{
113+
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
114114
Subprotocols: []string{"echo", "lar"},
115115
})
116116
if err != nil {
@@ -124,7 +124,7 @@ func TestHandshake(t *testing.T) {
124124
return nil
125125
},
126126
client: func(ctx context.Context, u string) error {
127-
c, _, err := websocket.Dial(ctx, u, websocket.DialOptions{
127+
c, _, err := websocket.Dial(ctx, u, &websocket.DialOptions{
128128
Subprotocols: []string{"poof", "echo"},
129129
})
130130
if err != nil {
@@ -141,7 +141,7 @@ func TestHandshake(t *testing.T) {
141141
{
142142
name: "badOrigin",
143143
server: func(w http.ResponseWriter, r *http.Request) error {
144-
c, err := websocket.Accept(w, r, websocket.AcceptOptions{})
144+
c, err := websocket.Accept(w, r, nil)
145145
if err == nil {
146146
c.Close(websocket.StatusInternalError, "")
147147
return xerrors.New("expected error regarding bad origin")
@@ -151,7 +151,7 @@ func TestHandshake(t *testing.T) {
151151
client: func(ctx context.Context, u string) error {
152152
h := http.Header{}
153153
h.Set("Origin", "http://unauthorized.com")
154-
c, _, err := websocket.Dial(ctx, u, websocket.DialOptions{
154+
c, _, err := websocket.Dial(ctx, u, &websocket.DialOptions{
155155
HTTPHeader: h,
156156
})
157157
if err == nil {
@@ -164,7 +164,7 @@ func TestHandshake(t *testing.T) {
164164
{
165165
name: "acceptSecureOrigin",
166166
server: func(w http.ResponseWriter, r *http.Request) error {
167-
c, err := websocket.Accept(w, r, websocket.AcceptOptions{})
167+
c, err := websocket.Accept(w, r, nil)
168168
if err != nil {
169169
return err
170170
}
@@ -174,7 +174,7 @@ func TestHandshake(t *testing.T) {
174174
client: func(ctx context.Context, u string) error {
175175
h := http.Header{}
176176
h.Set("Origin", u)
177-
c, _, err := websocket.Dial(ctx, u, websocket.DialOptions{
177+
c, _, err := websocket.Dial(ctx, u, &websocket.DialOptions{
178178
HTTPHeader: h,
179179
})
180180
if err != nil {
@@ -187,7 +187,7 @@ func TestHandshake(t *testing.T) {
187187
{
188188
name: "acceptInsecureOrigin",
189189
server: func(w http.ResponseWriter, r *http.Request) error {
190-
c, err := websocket.Accept(w, r, websocket.AcceptOptions{
190+
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
191191
InsecureSkipVerify: true,
192192
})
193193
if err != nil {
@@ -199,7 +199,7 @@ func TestHandshake(t *testing.T) {
199199
client: func(ctx context.Context, u string) error {
200200
h := http.Header{}
201201
h.Set("Origin", "https://example.com")
202-
c, _, err := websocket.Dial(ctx, u, websocket.DialOptions{
202+
c, _, err := websocket.Dial(ctx, u, &websocket.DialOptions{
203203
HTTPHeader: h,
204204
})
205205
if err != nil {
@@ -219,7 +219,7 @@ func TestHandshake(t *testing.T) {
219219
if cookie.Value != "myvalue" {
220220
return xerrors.Errorf("expected %q but got %q", "myvalue", cookie.Value)
221221
}
222-
c, err := websocket.Accept(w, r, websocket.AcceptOptions{})
222+
c, err := websocket.Accept(w, r, nil)
223223
if err != nil {
224224
return err
225225
}
@@ -245,7 +245,7 @@ func TestHandshake(t *testing.T) {
245245
hc := &http.Client{
246246
Jar: jar,
247247
}
248-
c, _, err := websocket.Dial(ctx, u, websocket.DialOptions{
248+
c, _, err := websocket.Dial(ctx, u, &websocket.DialOptions{
249249
HTTPClient: hc,
250250
})
251251
if err != nil {
@@ -801,7 +801,7 @@ func TestConn(t *testing.T) {
801801
},
802802
client: func(ctx context.Context, c *websocket.Conn) error {
803803
_, _, err := c.Read(ctx)
804-
if err == nil || strings.Contains(err.Error(), "opcode") {
804+
if err == nil || !strings.Contains(err.Error(), "opcode") {
805805
return xerrors.Errorf("expected error that contains opcode: %+v", err)
806806
}
807807
return nil
@@ -839,7 +839,7 @@ func TestConn(t *testing.T) {
839839
tls := rand.Intn(2) == 1
840840

841841
s, closeFn := testServer(t, func(w http.ResponseWriter, r *http.Request) error {
842-
c, err := websocket.Accept(w, r, websocket.AcceptOptions{})
842+
c, err := websocket.Accept(w, r, nil)
843843
if err != nil {
844844
return err
845845
}
@@ -854,7 +854,7 @@ func TestConn(t *testing.T) {
854854
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
855855
defer cancel()
856856

857-
opts := websocket.DialOptions{}
857+
opts := &websocket.DialOptions{}
858858
if tls {
859859
opts.HTTPClient = s.Client()
860860
}
@@ -920,7 +920,7 @@ func TestAutobahnServer(t *testing.T) {
920920
}
921921

922922
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
923-
c, err := websocket.Accept(w, r, websocket.AcceptOptions{
923+
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
924924
Subprotocols: []string{"echo"},
925925
})
926926
if err != nil {
@@ -1120,7 +1120,7 @@ func TestAutobahnClient(t *testing.T) {
11201120

11211121
var cases int
11221122
func() {
1123-
c, _, err := websocket.Dial(ctx, wsServerURL+"/getCaseCount", websocket.DialOptions{})
1123+
c, _, err := websocket.Dial(ctx, wsServerURL+"/getCaseCount", nil)
11241124
if err != nil {
11251125
t.Fatal(err)
11261126
}
@@ -1147,15 +1147,15 @@ func TestAutobahnClient(t *testing.T) {
11471147
ctx, cancel := context.WithTimeout(ctx, time.Second*45)
11481148
defer cancel()
11491149

1150-
c, _, err := websocket.Dial(ctx, fmt.Sprintf(wsServerURL+"/runCase?case=%v&agent=main", i), websocket.DialOptions{})
1150+
c, _, err := websocket.Dial(ctx, fmt.Sprintf(wsServerURL+"/runCase?case=%v&agent=main", i), nil)
11511151
if err != nil {
11521152
t.Fatal(err)
11531153
}
11541154
echoLoop(ctx, c)
11551155
}()
11561156
}
11571157

1158-
c, _, err := websocket.Dial(ctx, fmt.Sprintf(wsServerURL+"/updateReports?agent=main"), websocket.DialOptions{})
1158+
c, _, err := websocket.Dial(ctx, fmt.Sprintf(wsServerURL+"/updateReports?agent=main"), nil)
11591159
if err != nil {
11601160
t.Fatal(err)
11611161
}
@@ -1207,7 +1207,7 @@ func checkWSTestIndex(t *testing.T, path string) {
12071207

12081208
func benchConn(b *testing.B, echo, stream bool, size int) {
12091209
s, closeFn := testServer(b, func(w http.ResponseWriter, r *http.Request) error {
1210-
c, err := websocket.Accept(w, r, websocket.AcceptOptions{})
1210+
c, err := websocket.Accept(w, r, nil)
12111211
if err != nil {
12121212
return err
12131213
}
@@ -1225,7 +1225,7 @@ func benchConn(b *testing.B, echo, stream bool, size int) {
12251225
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
12261226
defer cancel()
12271227

1228-
c, _, err := websocket.Dial(ctx, wsURL, websocket.DialOptions{})
1228+
c, _, err := websocket.Dial(ctx, wsURL, nil)
12291229
if err != nil {
12301230
b.Fatal(err)
12311231
}

0 commit comments

Comments
 (0)