1
// Copyright 2013 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
21
func TestNextProtoUpgrade(t *testing.T) {
24
ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) {
25
fmt.Fprintf(w, "path=%s,proto=", r.URL.Path)
27
w.Write([]byte(r.TLS.NegotiatedProtocol))
29
if r.RemoteAddr == "" {
30
t.Error("request with no RemoteAddr")
33
t.Errorf("request with nil Body")
37
NextProtos: []string{"unhandled-proto", "tls-0.9"},
39
ts.Config.TLSNextProto = map[string]func(*Server, *tls.Conn, Handler){
40
"tls-0.9": handleTLSProtocol09,
45
// Normal request, without NPN.
48
res, err := c.Get(ts.URL)
52
body, err := ioutil.ReadAll(res.Body)
56
if want := "path=/,proto="; string(body) != want {
57
t.Errorf("plain request = %q; want %q", body, want)
61
// Request to an advertised but unhandled NPN protocol.
62
// Server will hang up.
64
certPool := x509.NewCertPool()
65
certPool.AddCert(ts.Certificate())
67
TLSClientConfig: &tls.Config{
69
NextProtos: []string{"unhandled-proto"},
72
defer tr.CloseIdleConnections()
76
res, err := c.Get(ts.URL)
78
defer res.Body.Close()
81
t.Errorf("expected error on unhandled-proto request; got: %s", buf.Bytes())
85
// Request using the "tls-0.9" protocol, which we register here.
86
// It is HTTP/0.9 over TLS.
89
tlsConfig := c.Transport.(*Transport).TLSClientConfig
90
tlsConfig.NextProtos = []string{"tls-0.9"}
91
conn, err := tls.Dial("tcp", ts.Listener.Addr().String(), tlsConfig)
95
conn.Write([]byte("GET /foo\n"))
96
body, err := ioutil.ReadAll(conn)
100
if want := "path=/foo,proto=tls-0.9"; string(body) != want {
101
t.Errorf("plain request = %q; want %q", body, want)
106
// handleTLSProtocol09 implements the HTTP/0.9 protocol over TLS, for the
107
// TestNextProtoUpgrade test.
108
func handleTLSProtocol09(srv *Server, conn *tls.Conn, h Handler) {
109
br := bufio.NewReader(conn)
110
line, err := br.ReadString('\n')
114
line = strings.TrimSpace(line)
115
path := strings.TrimPrefix(line, "GET ")
119
req, _ := NewRequest("GET", path, nil)
120
req.Proto = "HTTP/0.9"
123
rw := &http09Writer{conn, make(Header)}
127
type http09Writer struct {
132
func (w http09Writer) Header() Header { return w.h }
133
func (w http09Writer) WriteHeader(int) {} // no headers