github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/net/http/server_test.go (about) 1 // Copyright 2018 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 // Server unit tests 6 7 package http 8 9 import ( 10 "fmt" 11 "testing" 12 "time" 13 ) 14 15 func TestServerTLSHandshakeTimeout(t *testing.T) { 16 tests := []struct { 17 s *Server 18 want time.Duration 19 }{ 20 { 21 s: &Server{}, 22 want: 0, 23 }, 24 { 25 s: &Server{ 26 ReadTimeout: -1, 27 }, 28 want: 0, 29 }, 30 { 31 s: &Server{ 32 ReadTimeout: 5 * time.Second, 33 }, 34 want: 5 * time.Second, 35 }, 36 { 37 s: &Server{ 38 ReadTimeout: 5 * time.Second, 39 WriteTimeout: -1, 40 }, 41 want: 5 * time.Second, 42 }, 43 { 44 s: &Server{ 45 ReadTimeout: 5 * time.Second, 46 WriteTimeout: 4 * time.Second, 47 }, 48 want: 4 * time.Second, 49 }, 50 { 51 s: &Server{ 52 ReadTimeout: 5 * time.Second, 53 ReadHeaderTimeout: 2 * time.Second, 54 WriteTimeout: 4 * time.Second, 55 }, 56 want: 2 * time.Second, 57 }, 58 } 59 for i, tt := range tests { 60 got := tt.s.tlsHandshakeTimeout() 61 if got != tt.want { 62 t.Errorf("%d. got %v; want %v", i, got, tt.want) 63 } 64 } 65 } 66 67 func BenchmarkServerMatch(b *testing.B) { 68 fn := func(w ResponseWriter, r *Request) { 69 fmt.Fprintf(w, "OK") 70 } 71 mux := NewServeMux() 72 mux.HandleFunc("/", fn) 73 mux.HandleFunc("/index", fn) 74 mux.HandleFunc("/home", fn) 75 mux.HandleFunc("/about", fn) 76 mux.HandleFunc("/contact", fn) 77 mux.HandleFunc("/robots.txt", fn) 78 mux.HandleFunc("/products/", fn) 79 mux.HandleFunc("/products/1", fn) 80 mux.HandleFunc("/products/2", fn) 81 mux.HandleFunc("/products/3", fn) 82 mux.HandleFunc("/products/3/image.jpg", fn) 83 mux.HandleFunc("/admin", fn) 84 mux.HandleFunc("/admin/products/", fn) 85 mux.HandleFunc("/admin/products/create", fn) 86 mux.HandleFunc("/admin/products/update", fn) 87 mux.HandleFunc("/admin/products/delete", fn) 88 89 paths := []string{"/", "/notfound", "/admin/", "/admin/foo", "/contact", "/products", 90 "/products/", "/products/3/image.jpg"} 91 b.StartTimer() 92 for i := 0; i < b.N; i++ { 93 if h, p := mux.match(paths[i%len(paths)]); h != nil && p == "" { 94 b.Error("impossible") 95 } 96 } 97 b.StopTimer() 98 }