github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/net/http/httptest/server_test.go (about) 1 // Copyright 2012 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. 4 5 package httptest 6 7 import ( 8 "bufio" 9 "io/ioutil" 10 "net" 11 "net/http" 12 "testing" 13 ) 14 15 func TestServer(t *testing.T) { 16 ts := NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 17 w.Write([]byte("hello")) 18 })) 19 defer ts.Close() 20 res, err := http.Get(ts.URL) 21 if err != nil { 22 t.Fatal(err) 23 } 24 got, err := ioutil.ReadAll(res.Body) 25 res.Body.Close() 26 if err != nil { 27 t.Fatal(err) 28 } 29 if string(got) != "hello" { 30 t.Errorf("got %q, want hello", string(got)) 31 } 32 } 33 34 // Issue 12781 35 func TestGetAfterClose(t *testing.T) { 36 ts := NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 37 w.Write([]byte("hello")) 38 })) 39 40 res, err := http.Get(ts.URL) 41 if err != nil { 42 t.Fatal(err) 43 } 44 got, err := ioutil.ReadAll(res.Body) 45 if err != nil { 46 t.Fatal(err) 47 } 48 if string(got) != "hello" { 49 t.Fatalf("got %q, want hello", string(got)) 50 } 51 52 ts.Close() 53 54 res, err = http.Get(ts.URL) 55 if err == nil { 56 body, _ := ioutil.ReadAll(res.Body) 57 t.Fatalf("Unexpected response after close: %v, %v, %s", res.Status, res.Header, body) 58 } 59 } 60 61 func TestServerCloseBlocking(t *testing.T) { 62 ts := NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 63 w.Write([]byte("hello")) 64 })) 65 dial := func() net.Conn { 66 c, err := net.Dial("tcp", ts.Listener.Addr().String()) 67 if err != nil { 68 t.Fatal(err) 69 } 70 return c 71 } 72 73 // Keep one connection in StateNew (connected, but not sending anything) 74 cnew := dial() 75 defer cnew.Close() 76 77 // Keep one connection in StateIdle (idle after a request) 78 cidle := dial() 79 defer cidle.Close() 80 cidle.Write([]byte("HEAD / HTTP/1.1\r\nHost: foo\r\n\r\n")) 81 _, err := http.ReadResponse(bufio.NewReader(cidle), nil) 82 if err != nil { 83 t.Fatal(err) 84 } 85 86 ts.Close() // test we don't hang here forever. 87 } 88 89 // Issue 14290 90 func TestServerCloseClientConnections(t *testing.T) { 91 var s *Server 92 s = NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 93 s.CloseClientConnections() 94 })) 95 defer s.Close() 96 res, err := http.Get(s.URL) 97 if err == nil { 98 res.Body.Close() 99 t.Fatalf("Unexpected response: %#v", res) 100 } 101 } 102 103 // Tests that the Server.Client method works and returns an http.Client that can hit 104 // NewTLSServer without cert warnings. 105 func TestServerClient(t *testing.T) { 106 ts := NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 107 w.Write([]byte("hello")) 108 })) 109 defer ts.Close() 110 client := ts.Client() 111 res, err := client.Get(ts.URL) 112 if err != nil { 113 t.Fatal(err) 114 } 115 got, err := ioutil.ReadAll(res.Body) 116 res.Body.Close() 117 if err != nil { 118 t.Fatal(err) 119 } 120 if string(got) != "hello" { 121 t.Errorf("got %q, want hello", string(got)) 122 } 123 } 124 125 // Tests that the Server.Client.Transport interface is implemented 126 // by a *http.Transport. 127 func TestServerClientTransportType(t *testing.T) { 128 ts := NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 129 })) 130 defer ts.Close() 131 client := ts.Client() 132 if _, ok := client.Transport.(*http.Transport); !ok { 133 t.Errorf("got %T, want *http.Transport", client.Transport) 134 } 135 } 136 137 // Tests that the TLS Server.Client.Transport interface is implemented 138 // by a *http.Transport. 139 func TestTLSServerClientTransportType(t *testing.T) { 140 ts := NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 141 })) 142 defer ts.Close() 143 client := ts.Client() 144 if _, ok := client.Transport.(*http.Transport); !ok { 145 t.Errorf("got %T, want *http.Transport", client.Transport) 146 } 147 } 148 149 type onlyCloseListener struct { 150 net.Listener 151 } 152 153 func (onlyCloseListener) Close() error { return nil } 154 155 // Issue 19729: panic in Server.Close for values created directly 156 // without a constructor (so the unexported client field is nil). 157 func TestServerZeroValueClose(t *testing.T) { 158 ts := &Server{ 159 Listener: onlyCloseListener{}, 160 Config: &http.Server{}, 161 } 162 163 ts.Close() // tests that it doesn't panic 164 }