storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/http/server_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2017 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package http
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"reflect"
    23  	"testing"
    24  
    25  	"storj.io/minio/pkg/certs"
    26  )
    27  
    28  func TestNewServer(t *testing.T) {
    29  	nonLoopBackIP := getNonLoopBackIP(t)
    30  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    31  		fmt.Fprintf(w, "Hello, world")
    32  	})
    33  
    34  	testCases := []struct {
    35  		addrs   []string
    36  		handler http.Handler
    37  		certFn  certs.GetCertificateFunc
    38  	}{
    39  		{[]string{"127.0.0.1:9000"}, handler, nil},
    40  		{[]string{nonLoopBackIP + ":9000"}, handler, nil},
    41  		{[]string{"127.0.0.1:9000", nonLoopBackIP + ":9000"}, handler, nil},
    42  		{[]string{"127.0.0.1:9000"}, handler, getCert},
    43  		{[]string{nonLoopBackIP + ":9000"}, handler, getCert},
    44  		{[]string{"127.0.0.1:9000", nonLoopBackIP + ":9000"}, handler, getCert},
    45  	}
    46  
    47  	for i, testCase := range testCases {
    48  		server := NewServer(testCase.addrs, testCase.handler, testCase.certFn)
    49  		if server == nil {
    50  			t.Fatalf("Case %v: server: expected: <non-nil>, got: <nil>", (i + 1))
    51  		}
    52  
    53  		if !reflect.DeepEqual(server.Addrs, testCase.addrs) {
    54  			t.Fatalf("Case %v: server.Addrs: expected: %v, got: %v", (i + 1), testCase.addrs, server.Addrs)
    55  		}
    56  
    57  		// Interfaces are not comparable even with reflection.
    58  		// if !reflect.DeepEqual(server.Handler, testCase.handler) {
    59  		// 	t.Fatalf("Case %v: server.Handler: expected: %v, got: %v", (i + 1), testCase.handler, server.Handler)
    60  		// }
    61  
    62  		if testCase.certFn == nil {
    63  			if server.TLSConfig != nil {
    64  				t.Fatalf("Case %v: server.TLSConfig: expected: <nil>, got: %v", (i + 1), server.TLSConfig)
    65  			}
    66  		} else {
    67  			if server.TLSConfig == nil {
    68  				t.Fatalf("Case %v: server.TLSConfig: expected: <non-nil>, got: <nil>", (i + 1))
    69  			}
    70  		}
    71  
    72  		if server.ShutdownTimeout != DefaultShutdownTimeout {
    73  			t.Fatalf("Case %v: server.ShutdownTimeout: expected: %v, got: %v", (i + 1), DefaultShutdownTimeout, server.ShutdownTimeout)
    74  		}
    75  
    76  		if server.MaxHeaderBytes != DefaultMaxHeaderBytes {
    77  			t.Fatalf("Case %v: server.MaxHeaderBytes: expected: %v, got: %v", (i + 1), DefaultMaxHeaderBytes, server.MaxHeaderBytes)
    78  		}
    79  	}
    80  }