github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/http/server_test.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package http
    19  
    20  import (
    21  	"crypto/tls"
    22  	"fmt"
    23  	"net/http"
    24  	"reflect"
    25  	"testing"
    26  
    27  	"github.com/minio/pkg/v2/certs"
    28  )
    29  
    30  func TestNewServer(t *testing.T) {
    31  	nonLoopBackIP := getNonLoopBackIP(t)
    32  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    33  		fmt.Fprintf(w, "Hello, world")
    34  	})
    35  
    36  	testCases := []struct {
    37  		addrs   []string
    38  		handler http.Handler
    39  		certFn  certs.GetCertificateFunc
    40  	}{
    41  		{[]string{"127.0.0.1:9000"}, handler, nil},
    42  		{[]string{nonLoopBackIP + ":9000"}, handler, nil},
    43  		{[]string{"127.0.0.1:9000", nonLoopBackIP + ":9000"}, handler, nil},
    44  		{[]string{"127.0.0.1:9000"}, handler, getCert},
    45  		{[]string{nonLoopBackIP + ":9000"}, handler, getCert},
    46  		{[]string{"127.0.0.1:9000", nonLoopBackIP + ":9000"}, handler, getCert},
    47  	}
    48  
    49  	for i, testCase := range testCases {
    50  		server := NewServer(testCase.addrs).
    51  			UseHandler(testCase.handler).
    52  			UseShutdownTimeout(DefaultShutdownTimeout)
    53  		if testCase.certFn != nil {
    54  			server = server.UseTLSConfig(&tls.Config{
    55  				PreferServerCipherSuites: true,
    56  				GetCertificate:           testCase.certFn,
    57  			})
    58  		}
    59  		if server == nil {
    60  			t.Fatalf("Case %v: server: expected: <non-nil>, got: <nil>", (i + 1))
    61  		} else if !reflect.DeepEqual(server.Addrs, testCase.addrs) {
    62  			t.Fatalf("Case %v: server.Addrs: expected: %v, got: %v", (i + 1), testCase.addrs, server.Addrs)
    63  		}
    64  
    65  		if testCase.certFn == nil {
    66  			if server.TLSConfig != nil {
    67  				t.Fatalf("Case %v: server.TLSConfig: expected: <nil>, got: %v", (i + 1), server.TLSConfig)
    68  			}
    69  		} else {
    70  			if server.TLSConfig == nil {
    71  				t.Fatalf("Case %v: server.TLSConfig: expected: <non-nil>, got: <nil>", (i + 1))
    72  			}
    73  		}
    74  
    75  		if server.ShutdownTimeout != DefaultShutdownTimeout {
    76  			t.Fatalf("Case %v: server.ShutdownTimeout: expected: %v, got: %v", (i + 1), DefaultShutdownTimeout, server.ShutdownTimeout)
    77  		}
    78  
    79  		if server.MaxHeaderBytes != DefaultMaxHeaderBytes {
    80  			t.Fatalf("Case %v: server.MaxHeaderBytes: expected: %v, got: %v", (i + 1), DefaultMaxHeaderBytes, server.MaxHeaderBytes)
    81  		}
    82  	}
    83  }