github.com/OpsMx/go-app-base@v0.0.24/httputil/http_test.go (about)

     1  // Copyright 2022 OpsMx, Inc
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package httputil
    16  
    17  import (
    18  	"crypto/tls"
    19  	"fmt"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func Test_StatusCodeOK(t *testing.T) {
    26  	var tests = []struct {
    27  		code int
    28  		want bool
    29  	}{
    30  		{100, false},
    31  		{199, false},
    32  		{200, true},
    33  		{250, true},
    34  		{299, true},
    35  		{304, false},
    36  	}
    37  
    38  	for _, tt := range tests {
    39  		testname := fmt.Sprintf("%d", tt.code)
    40  		t.Run(testname, func(t *testing.T) {
    41  			ans := StatusCodeOK(tt.code)
    42  			require.Equal(t, tt.want, ans)
    43  		})
    44  	}
    45  }
    46  
    47  func TestClientConfig_applyDefaults(t *testing.T) {
    48  	tests := []struct {
    49  		name     string
    50  		provided ClientConfig
    51  		wanted   ClientConfig
    52  	}{
    53  		{
    54  			"all defaults",
    55  			ClientConfig{},
    56  			*defaultClientConfig,
    57  		}, {
    58  			"DialTimeout set",
    59  			ClientConfig{DialTimeout: 1234},
    60  			ClientConfig{
    61  				DialTimeout:           1234,
    62  				ClientTimeout:         defaultClientConfig.DialTimeout,
    63  				TLSHandshakeTimeout:   defaultClientConfig.TLSHandshakeTimeout,
    64  				ResponseHeaderTimeout: defaultClientConfig.ResponseHeaderTimeout,
    65  				MaxIdleConnections:    defaultClientConfig.MaxIdleConnections,
    66  			},
    67  		}, {
    68  			"ClientTimeout set",
    69  			ClientConfig{ClientTimeout: 1234},
    70  			ClientConfig{
    71  				DialTimeout:           defaultClientConfig.DialTimeout,
    72  				ClientTimeout:         1234,
    73  				TLSHandshakeTimeout:   defaultClientConfig.TLSHandshakeTimeout,
    74  				ResponseHeaderTimeout: defaultClientConfig.ResponseHeaderTimeout,
    75  				MaxIdleConnections:    defaultClientConfig.MaxIdleConnections,
    76  			},
    77  		}, {
    78  			"TLSHandshakeTimeout set",
    79  			ClientConfig{TLSHandshakeTimeout: 1234},
    80  			ClientConfig{
    81  				DialTimeout:           defaultClientConfig.DialTimeout,
    82  				ClientTimeout:         defaultClientConfig.DialTimeout,
    83  				TLSHandshakeTimeout:   1234,
    84  				ResponseHeaderTimeout: defaultClientConfig.ResponseHeaderTimeout,
    85  				MaxIdleConnections:    defaultClientConfig.MaxIdleConnections,
    86  			},
    87  		}, {
    88  			"ResponseHeaderTimeout set",
    89  			ClientConfig{ResponseHeaderTimeout: 1234},
    90  			ClientConfig{
    91  				DialTimeout:           defaultClientConfig.DialTimeout,
    92  				ClientTimeout:         defaultClientConfig.DialTimeout,
    93  				TLSHandshakeTimeout:   defaultClientConfig.TLSHandshakeTimeout,
    94  				ResponseHeaderTimeout: 1234,
    95  				MaxIdleConnections:    defaultClientConfig.MaxIdleConnections,
    96  			},
    97  		}, {
    98  			"MaxIdleConnections set",
    99  			ClientConfig{MaxIdleConnections: 1234},
   100  			ClientConfig{
   101  				DialTimeout:           defaultClientConfig.DialTimeout,
   102  				ClientTimeout:         defaultClientConfig.DialTimeout,
   103  				TLSHandshakeTimeout:   defaultClientConfig.TLSHandshakeTimeout,
   104  				ResponseHeaderTimeout: defaultClientConfig.ResponseHeaderTimeout,
   105  				MaxIdleConnections:    1234,
   106  			},
   107  		},
   108  	}
   109  	for _, tt := range tests {
   110  		t.Run(tt.name, func(t *testing.T) {
   111  			found := tt.wanted
   112  			found.applyDefaults()
   113  			require.Equal(t, tt.wanted, found)
   114  		})
   115  	}
   116  }
   117  
   118  func Test_SetClientConfig(t *testing.T) {
   119  	t.Run("sets", func(t *testing.T) {
   120  		defaultClientConfig = nil
   121  		c := ClientConfig{DialTimeout: 9876}
   122  		SetClientConfig(c)
   123  		require.Equal(t, 9876, defaultClientConfig.DialTimeout)
   124  	})
   125  }
   126  
   127  func Test_SetTLSConfig(t *testing.T) {
   128  	t.Run("sets", func(t *testing.T) {
   129  		defaultTLSConfig = nil
   130  		c := tls.Config{}
   131  		SetTLSConfig(&c)
   132  		require.NotNil(t, defaultTLSConfig)
   133  	})
   134  }