github.com/prysmaticlabs/prysm@v1.4.4/shared/httputils/endpoint_test.go (about)

     1  package httputils
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prysmaticlabs/prysm/shared/httputils/authorizationmethod"
     7  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
     8  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
     9  )
    10  
    11  func TestToHeaderValue(t *testing.T) {
    12  	t.Run("None", func(t *testing.T) {
    13  		data := &AuthorizationData{
    14  			Method: authorizationmethod.None,
    15  			Value:  "foo",
    16  		}
    17  		header, err := data.ToHeaderValue()
    18  		require.NoError(t, err)
    19  		assert.Equal(t, "", header)
    20  	})
    21  	t.Run("Basic", func(t *testing.T) {
    22  		data := &AuthorizationData{
    23  			Method: authorizationmethod.Basic,
    24  			Value:  "foo",
    25  		}
    26  		header, err := data.ToHeaderValue()
    27  		require.NoError(t, err)
    28  		assert.Equal(t, "Basic foo", header)
    29  	})
    30  	t.Run("Bearer", func(t *testing.T) {
    31  		data := &AuthorizationData{
    32  			Method: authorizationmethod.Bearer,
    33  			Value:  "foo",
    34  		}
    35  		header, err := data.ToHeaderValue()
    36  		require.NoError(t, err)
    37  		assert.Equal(t, "Bearer foo", header)
    38  	})
    39  	t.Run("Unknown", func(t *testing.T) {
    40  		data := &AuthorizationData{
    41  			Method: 99,
    42  			Value:  "foo",
    43  		}
    44  		_, err := data.ToHeaderValue()
    45  		require.NotNil(t, err)
    46  	})
    47  }
    48  
    49  func TestMethod(t *testing.T) {
    50  	t.Run("None", func(t *testing.T) {
    51  		method := Method("")
    52  		assert.Equal(t, authorizationmethod.None, method)
    53  		method = Method("foo")
    54  		assert.Equal(t, authorizationmethod.None, method)
    55  	})
    56  	t.Run("Basic", func(t *testing.T) {
    57  		method := Method("Basic")
    58  		assert.Equal(t, authorizationmethod.Basic, method)
    59  	})
    60  	t.Run("Basic different text case", func(t *testing.T) {
    61  		method := Method("bAsIc")
    62  		assert.Equal(t, authorizationmethod.Basic, method)
    63  	})
    64  	t.Run("Bearer", func(t *testing.T) {
    65  		method := Method("Bearer")
    66  		assert.Equal(t, authorizationmethod.Bearer, method)
    67  	})
    68  	t.Run("Bearer different text case", func(t *testing.T) {
    69  		method := Method("bEaReR")
    70  		assert.Equal(t, authorizationmethod.Bearer, method)
    71  	})
    72  }
    73  
    74  func TestEndpointEquals(t *testing.T) {
    75  	e := Endpoint{
    76  		Url: "Url",
    77  		Auth: AuthorizationData{
    78  			Method: authorizationmethod.Basic,
    79  			Value:  "Basic username:password",
    80  		},
    81  	}
    82  
    83  	t.Run("equal", func(t *testing.T) {
    84  		other := Endpoint{
    85  			Url: "Url",
    86  			Auth: AuthorizationData{
    87  				Method: authorizationmethod.Basic,
    88  				Value:  "Basic username:password",
    89  			},
    90  		}
    91  		assert.Equal(t, true, e.Equals(other))
    92  	})
    93  	t.Run("different URL", func(t *testing.T) {
    94  		other := Endpoint{
    95  			Url: "Different",
    96  			Auth: AuthorizationData{
    97  				Method: authorizationmethod.Basic,
    98  				Value:  "Basic username:password",
    99  			},
   100  		}
   101  		assert.Equal(t, false, e.Equals(other))
   102  	})
   103  	t.Run("different auth data", func(t *testing.T) {
   104  		other := Endpoint{
   105  			Url: "Url",
   106  			Auth: AuthorizationData{
   107  				Method: authorizationmethod.Bearer,
   108  				Value:  "Bearer token",
   109  			},
   110  		}
   111  		assert.Equal(t, false, e.Equals(other))
   112  	})
   113  }
   114  
   115  func TestAuthorizationDataEquals(t *testing.T) {
   116  	d := AuthorizationData{
   117  		Method: authorizationmethod.Basic,
   118  		Value:  "username:password",
   119  	}
   120  
   121  	t.Run("equal", func(t *testing.T) {
   122  		other := AuthorizationData{
   123  			Method: authorizationmethod.Basic,
   124  			Value:  "username:password",
   125  		}
   126  		assert.Equal(t, true, d.Equals(other))
   127  	})
   128  	t.Run("different method", func(t *testing.T) {
   129  		other := AuthorizationData{
   130  			Method: authorizationmethod.None,
   131  			Value:  "username:password",
   132  		}
   133  		assert.Equal(t, false, d.Equals(other))
   134  	})
   135  	t.Run("different value", func(t *testing.T) {
   136  		other := AuthorizationData{
   137  			Method: authorizationmethod.Basic,
   138  			Value:  "different:different",
   139  		}
   140  		assert.Equal(t, false, d.Equals(other))
   141  	})
   142  }