Skip to content

Commit

Permalink
Add IsWebSocketUpgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
garyburd committed Mar 9, 2016
1 parent c45a635 commit a622679
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,10 @@ func Subprotocols(r *http.Request) []string {
}
return protocols
}

// IsWebSocketUpgrade returns true if the client requested upgrade to the
// WebSocket protocol.
func IsWebSocketUpgrade(r *http.Request) bool {
return tokenListContainsValue(r.Header, "Connection", "upgrade") &&
tokenListContainsValue(r.Header, "Upgrade", "websocket")
}
18 changes: 18 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,21 @@ func TestSubprotocols(t *testing.T) {
}
}
}

var isWebSocketUpgradeTests = []struct {
ok bool
h http.Header
}{
{false, http.Header{"Upgrade": {"websocket"}}},
{false, http.Header{"Connection": {"upgrade"}}},
{true, http.Header{"Connection": {"upgRade"}, "Upgrade": {"WebSocket"}}},
}

func TestIsWebSocketUpgrade(t *testing.T) {
for _, tt := range isWebSocketUpgradeTests {
ok := IsWebSocketUpgrade(&http.Request{Header: tt.h})
if tt.ok != ok {
t.Errorf("IsWebSocketUpgrade(%v) returned %v, want %v", tt.h, ok, tt.ok)
}
}
}

0 comments on commit a622679

Please sign in to comment.