go.net/websocket: fix handshake error.

If client sent no subprotocol (e.g. no Sec-WebSocket-Protocol),
websocket server responded with the following header
  HTTP/1.1 101 Switching Protocols
  Upgrade: websocket
  Connection: Upgrade
  Sec-WebSocket-Accept: E7SRWRnZL9RuGFLuZ0j4508nqdg=
  Sec-WebSocket-Protocol:
So, client may close the connection because it contains
wrong empty Sec-WebSocket-Protocol header.

If client didn't offer any subprotocol, don't set
config.Protocol, so that not emit empty Sec-WebSocket-Protocol.

Fixes golang/go#5457.

R=golang-dev, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/9379044
diff --git a/websocket/hybi_test.go b/websocket/hybi_test.go
index 01ed9e9..9db0ef9 100644
--- a/websocket/hybi_test.go
+++ b/websocket/hybi_test.go
@@ -243,10 +243,14 @@
 	if code != http.StatusSwitchingProtocols {
 		t.Errorf("status expected %q but got %q", http.StatusSwitchingProtocols, code)
 	}
+	expectedProtocols := []string{"chat", "superchat"}
+	if fmt.Sprintf("%v", config.Protocol) != fmt.Sprintf("%v", expectedProtocols) {
+		t.Errorf("protocol expected %q but got %q", expectedProtocols, config.Protocol)
+	}
 	b := bytes.NewBuffer([]byte{})
 	bw := bufio.NewWriter(b)
 
-	config.Protocol = []string{"chat"}
+	config.Protocol = config.Protocol[:1]
 
 	err = handshaker.AcceptHandshake(bw)
 	if err != nil {
@@ -265,6 +269,51 @@
 	}
 }
 
+func TestHybiServerHandshakeNoSubProtocol(t *testing.T) {
+	config := new(Config)
+	handshaker := &hybiServerHandshaker{Config: config}
+	br := bufio.NewReader(strings.NewReader(`GET /chat HTTP/1.1
+Host: server.example.com
+Upgrade: websocket
+Connection: Upgrade
+Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
+Origin: http://example.com
+Sec-WebSocket-Version: 13
+
+`))
+	req, err := http.ReadRequest(br)
+	if err != nil {
+		t.Fatal("request", err)
+	}
+	code, err := handshaker.ReadHandshake(br, req)
+	if err != nil {
+		t.Errorf("handshake failed: %v", err)
+	}
+	if code != http.StatusSwitchingProtocols {
+		t.Errorf("status expected %q but got %q", http.StatusSwitchingProtocols, code)
+	}
+	if len(config.Protocol) != 0 {
+		t.Errorf("len(config.Protocol) expected 0, but got %q", len(config.Protocol))
+	}
+	b := bytes.NewBuffer([]byte{})
+	bw := bufio.NewWriter(b)
+
+	err = handshaker.AcceptHandshake(bw)
+	if err != nil {
+		t.Errorf("handshake response failed: %v", err)
+	}
+	expectedResponse := strings.Join([]string{
+		"HTTP/1.1 101 Switching Protocols",
+		"Upgrade: websocket",
+		"Connection: Upgrade",
+		"Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",
+		"", ""}, "\r\n")
+
+	if b.String() != expectedResponse {
+		t.Errorf("handshake expected %q but got %q", expectedResponse, b.String())
+	}
+}
+
 func TestHybiServerHandshakeHybi08(t *testing.T) {
 	config := new(Config)
 	handshaker := &hybiServerHandshaker{Config: config}