github.com/iotexproject/iotex-core@v1.14.1-rc1/pkg/util/httputil/httputil_test.go (about)

     1  package httputil
     2  
     3  import (
     4  	"errors"
     5  	"net/http"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestServer(t *testing.T) {
    13  	t.Run("creates a HTTP server with time out settings", func(t *testing.T) {
    14  		var handler http.Handler
    15  		addr := "myAddress"
    16  
    17  		result := NewServer(addr, handler, ReadHeaderTimeout(2*time.Second))
    18  		require.Equal(t, 2*time.Second, result.ReadHeaderTimeout)
    19  		require.Equal(t, DefaultServerConfig.ReadTimeout, result.ReadTimeout)
    20  		require.Equal(t, DefaultServerConfig.WriteTimeout, result.WriteTimeout)
    21  		require.Equal(t, DefaultServerConfig.IdleTimeout, result.IdleTimeout)
    22  		require.Equal(t, addr, result.Addr)
    23  		require.Equal(t, handler, result.Handler)
    24  	})
    25  }
    26  
    27  func TestLimitListener(t *testing.T) {
    28  	t.Run("missing port in address", func(t *testing.T) {
    29  		expectedErr := errors.New("listen tcp: address myAddress: missing port in address")
    30  		result, err := LimitListener("myAddress")
    31  		require.Error(t, err)
    32  		require.Equal(t, expectedErr.Error(), err.Error())
    33  		require.Nil(t, result)
    34  	})
    35  
    36  	t.Run("input empty string", func(t *testing.T) {
    37  		_, err := LimitListener("")
    38  		// fix none root permission error
    39  		if err != nil {
    40  			require.Equal(t, "listen tcp :80: bind: permission denied", err.Error())
    41  		} else {
    42  			require.NoError(t, err)
    43  		}
    44  	})
    45  }