gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/gmhttp/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" 10 "net" 11 "testing" 12 13 http "gitee.com/ks-custle/core-gm/gmhttp" 14 ) 15 16 type newServerFunc func(http.Handler) *Server 17 18 var newServers = map[string]newServerFunc{ 19 "NewServer": NewServer, 20 "NewTLSServer": NewTLSServer, 21 22 // The manual variants of newServer create a Server manually by only filling 23 // in the exported fields of Server. 24 "NewServerManual": func(h http.Handler) *Server { 25 ts := &Server{Listener: newLocalListener(), Config: &http.Server{Handler: h}} 26 ts.Start() 27 return ts 28 }, 29 "NewTLSServerManual": func(h http.Handler) *Server { 30 ts := &Server{Listener: newLocalListener(), Config: &http.Server{Handler: h}} 31 ts.StartTLS() 32 return ts 33 }, 34 } 35 36 func TestServer(t *testing.T) { 37 for _, name := range []string{"NewServer", "NewServerManual"} { 38 t.Run(name, func(t *testing.T) { 39 newServer := newServers[name] 40 t.Run("Server", func(t *testing.T) { testServer(t, newServer) }) 41 t.Run("GetAfterClose", func(t *testing.T) { testGetAfterClose(t, newServer) }) 42 t.Run("ServerCloseBlocking", func(t *testing.T) { testServerCloseBlocking(t, newServer) }) 43 t.Run("ServerCloseClientConnections", func(t *testing.T) { testServerCloseClientConnections(t, newServer) }) 44 t.Run("ServerClientTransportType", func(t *testing.T) { testServerClientTransportType(t, newServer) }) 45 }) 46 } 47 for _, name := range []string{"NewTLSServer", "NewTLSServerManual"} { 48 t.Run(name, func(t *testing.T) { 49 newServer := newServers[name] 50 t.Run("ServerClient", func(t *testing.T) { testServerClient(t, newServer) }) 51 t.Run("TLSServerClientTransportType", func(t *testing.T) { testTLSServerClientTransportType(t, newServer) }) 52 }) 53 } 54 } 55 56 func testServer(t *testing.T, newServer newServerFunc) { 57 ts := newServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 58 _, _ = w.Write([]byte("hello")) 59 })) 60 defer ts.Close() 61 res, err := http.Get(ts.URL) 62 if err != nil { 63 t.Fatal(err) 64 } 65 got, err := io.ReadAll(res.Body) 66 _ = res.Body.Close() 67 if err != nil { 68 t.Fatal(err) 69 } 70 if string(got) != "hello" { 71 t.Errorf("got %q, want hello", string(got)) 72 } 73 } 74 75 // Issue 12781 76 func testGetAfterClose(t *testing.T, newServer newServerFunc) { 77 ts := newServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 78 _, _ = w.Write([]byte("hello")) 79 })) 80 81 res, err := http.Get(ts.URL) 82 if err != nil { 83 t.Fatal(err) 84 } 85 got, err := io.ReadAll(res.Body) 86 if err != nil { 87 t.Fatal(err) 88 } 89 if string(got) != "hello" { 90 t.Fatalf("got %q, want hello", string(got)) 91 } 92 93 ts.Close() 94 95 res, err = http.Get(ts.URL) 96 if err == nil { 97 body, _ := io.ReadAll(res.Body) 98 t.Fatalf("Unexpected response after close: %v, %v, %s", res.Status, res.Header, body) 99 } 100 } 101 102 func testServerCloseBlocking(t *testing.T, newServer newServerFunc) { 103 ts := newServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 104 _, _ = w.Write([]byte("hello")) 105 })) 106 dial := func() net.Conn { 107 c, err := net.Dial("tcp", ts.Listener.Addr().String()) 108 if err != nil { 109 t.Fatal(err) 110 } 111 return c 112 } 113 114 // Keep one connection in StateNew (connected, but not sending anything) 115 cnew := dial() 116 defer func(cnew net.Conn) { 117 _ = cnew.Close() 118 }(cnew) 119 120 // Keep one connection in StateIdle (idle after a request) 121 cidle := dial() 122 defer func(cidle net.Conn) { 123 _ = cidle.Close() 124 }(cidle) 125 _, _ = cidle.Write([]byte("HEAD / HTTP/1.1\r\nHost: foo\r\n\r\n")) 126 _, err := http.ReadResponse(bufio.NewReader(cidle), nil) 127 if err != nil { 128 t.Fatal(err) 129 } 130 131 ts.Close() // test we don't hang here forever. 132 } 133 134 // Issue 14290 135 func testServerCloseClientConnections(t *testing.T, newServer newServerFunc) { 136 var s *Server 137 s = newServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 138 s.CloseClientConnections() 139 })) 140 defer s.Close() 141 res, err := http.Get(s.URL) 142 if err == nil { 143 _ = res.Body.Close() 144 t.Fatalf("Unexpected response: %#v", res) 145 } 146 } 147 148 // Tests that the Server.Client method works and returns an http.Client that can hit 149 // NewTLSServer without cert warnings. 150 func testServerClient(t *testing.T, newTLSServer newServerFunc) { 151 ts := newTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 152 _, _ = w.Write([]byte("hello")) 153 })) 154 defer ts.Close() 155 client := ts.Client() 156 res, err := client.Get(ts.URL) 157 if err != nil { 158 t.Fatal(err) 159 } 160 got, err := io.ReadAll(res.Body) 161 _ = res.Body.Close() 162 if err != nil { 163 t.Fatal(err) 164 } 165 if string(got) != "hello" { 166 t.Errorf("got %q, want hello", string(got)) 167 } 168 } 169 170 // Tests that the Server.Client.Transport interface is implemented 171 // by a *http.Transport. 172 func testServerClientTransportType(t *testing.T, newServer newServerFunc) { 173 ts := newServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 174 })) 175 defer ts.Close() 176 client := ts.Client() 177 if _, ok := client.Transport.(*http.Transport); !ok { 178 t.Errorf("got %T, want *http.Transport", client.Transport) 179 } 180 } 181 182 // Tests that the TLS Server.Client.Transport interface is implemented 183 // by a *http.Transport. 184 func testTLSServerClientTransportType(t *testing.T, newTLSServer newServerFunc) { 185 ts := newTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 186 })) 187 defer ts.Close() 188 client := ts.Client() 189 if _, ok := client.Transport.(*http.Transport); !ok { 190 t.Errorf("got %T, want *http.Transport", client.Transport) 191 } 192 } 193 194 type onlyCloseListener struct { 195 net.Listener 196 } 197 198 func (onlyCloseListener) Close() error { return nil } 199 200 // Issue 19729: panic in Server.Close for values created directly 201 // without a constructor (so the unexported client field is nil). 202 func TestServerZeroValueClose(t *testing.T) { 203 ts := &Server{ 204 Listener: onlyCloseListener{}, 205 Config: &http.Server{}, 206 } 207 208 ts.Close() // tests that it doesn't panic 209 } 210 211 func TestTLSServerWithHTTP2(t *testing.T) { 212 modes := []struct { 213 name string 214 wantProto string 215 }{ 216 {"http1", "HTTP/1.1"}, 217 {"http2", "HTTP/2.0"}, 218 } 219 220 for _, tt := range modes { 221 t.Run(tt.name, func(t *testing.T) { 222 cst := NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 223 w.Header().Set("X-Proto", r.Proto) 224 })) 225 226 switch tt.name { 227 case "http2": 228 cst.EnableHTTP2 = true 229 cst.StartTLS() 230 default: 231 cst.Start() 232 } 233 234 defer cst.Close() 235 236 res, err := cst.Client().Get(cst.URL) 237 if err != nil { 238 t.Fatalf("Failed to make request: %v", err) 239 } 240 if g, w := res.Header.Get("X-Proto"), tt.wantProto; g != w { 241 t.Fatalf("X-Proto header mismatch:\n\tgot: %q\n\twant: %q", g, w) 242 } 243 }) 244 } 245 }