github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/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 "github.com/hxx258456/ccgo/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 cnew.Close() 117 118 // Keep one connection in StateIdle (idle after a request) 119 cidle := dial() 120 defer cidle.Close() 121 cidle.Write([]byte("HEAD / HTTP/1.1\r\nHost: foo\r\n\r\n")) 122 _, err := http.ReadResponse(bufio.NewReader(cidle), nil) 123 if err != nil { 124 t.Fatal(err) 125 } 126 127 ts.Close() // test we don't hang here forever. 128 } 129 130 // Issue 14290 131 func testServerCloseClientConnections(t *testing.T, newServer newServerFunc) { 132 var s *Server 133 s = newServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 134 s.CloseClientConnections() 135 })) 136 defer s.Close() 137 res, err := http.Get(s.URL) 138 if err == nil { 139 res.Body.Close() 140 t.Fatalf("Unexpected response: %#v", res) 141 } 142 } 143 144 // Tests that the Server.Client method works and returns an http.Client that can hit 145 // NewTLSServer without cert warnings. 146 func testServerClient(t *testing.T, newTLSServer newServerFunc) { 147 ts := newTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 148 w.Write([]byte("hello")) 149 })) 150 defer ts.Close() 151 client := ts.Client() 152 res, err := client.Get(ts.URL) 153 if err != nil { 154 t.Fatal(err) 155 } 156 got, err := io.ReadAll(res.Body) 157 res.Body.Close() 158 if err != nil { 159 t.Fatal(err) 160 } 161 if string(got) != "hello" { 162 t.Errorf("got %q, want hello", string(got)) 163 } 164 } 165 166 // Tests that the Server.Client.Transport interface is implemented 167 // by a *http.Transport. 168 func testServerClientTransportType(t *testing.T, newServer newServerFunc) { 169 ts := newServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 170 })) 171 defer ts.Close() 172 client := ts.Client() 173 if _, ok := client.Transport.(*http.Transport); !ok { 174 t.Errorf("got %T, want *http.Transport", client.Transport) 175 } 176 } 177 178 // Tests that the TLS Server.Client.Transport interface is implemented 179 // by a *http.Transport. 180 func testTLSServerClientTransportType(t *testing.T, newTLSServer newServerFunc) { 181 ts := newTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 182 })) 183 defer ts.Close() 184 client := ts.Client() 185 if _, ok := client.Transport.(*http.Transport); !ok { 186 t.Errorf("got %T, want *http.Transport", client.Transport) 187 } 188 } 189 190 type onlyCloseListener struct { 191 net.Listener 192 } 193 194 func (onlyCloseListener) Close() error { return nil } 195 196 // Issue 19729: panic in Server.Close for values created directly 197 // without a constructor (so the unexported client field is nil). 198 func TestServerZeroValueClose(t *testing.T) { 199 ts := &Server{ 200 Listener: onlyCloseListener{}, 201 Config: &http.Server{}, 202 } 203 204 ts.Close() // tests that it doesn't panic 205 } 206 207 func TestTLSServerWithHTTP2(t *testing.T) { 208 modes := []struct { 209 name string 210 wantProto string 211 }{ 212 {"http1", "HTTP/1.1"}, 213 {"http2", "HTTP/2.0"}, 214 } 215 216 for _, tt := range modes { 217 t.Run(tt.name, func(t *testing.T) { 218 cst := NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 219 w.Header().Set("X-Proto", r.Proto) 220 })) 221 222 switch tt.name { 223 case "http2": 224 cst.EnableHTTP2 = true 225 cst.StartTLS() 226 default: 227 cst.Start() 228 } 229 230 defer cst.Close() 231 232 res, err := cst.Client().Get(cst.URL) 233 if err != nil { 234 t.Fatalf("Failed to make request: %v", err) 235 } 236 if g, w := res.Header.Get("X-Proto"), tt.wantProto; g != w { 237 t.Fatalf("X-Proto header mismatch:\n\tgot: %q\n\twant: %q", g, w) 238 } 239 }) 240 } 241 }