2021-09-16 19:12:27 +00:00
|
|
|
package network
|
2021-04-15 11:02:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-16 19:12:27 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/network/authorization"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
2021-04-15 11:02:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestToHeaderValue(t *testing.T) {
|
|
|
|
t.Run("None", func(t *testing.T) {
|
|
|
|
data := &AuthorizationData{
|
2021-09-16 19:12:27 +00:00
|
|
|
Method: authorization.None,
|
2021-04-15 11:02:02 +00:00
|
|
|
Value: "foo",
|
|
|
|
}
|
|
|
|
header, err := data.ToHeaderValue()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "", header)
|
|
|
|
})
|
|
|
|
t.Run("Basic", func(t *testing.T) {
|
|
|
|
data := &AuthorizationData{
|
2021-09-16 19:12:27 +00:00
|
|
|
Method: authorization.Basic,
|
2021-04-15 11:02:02 +00:00
|
|
|
Value: "foo",
|
|
|
|
}
|
|
|
|
header, err := data.ToHeaderValue()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "Basic foo", header)
|
|
|
|
})
|
|
|
|
t.Run("Bearer", func(t *testing.T) {
|
|
|
|
data := &AuthorizationData{
|
2021-09-16 19:12:27 +00:00
|
|
|
Method: authorization.Bearer,
|
2021-04-15 11:02:02 +00:00
|
|
|
Value: "foo",
|
|
|
|
}
|
|
|
|
header, err := data.ToHeaderValue()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "Bearer foo", header)
|
|
|
|
})
|
|
|
|
t.Run("Unknown", func(t *testing.T) {
|
|
|
|
data := &AuthorizationData{
|
|
|
|
Method: 99,
|
|
|
|
Value: "foo",
|
|
|
|
}
|
|
|
|
_, err := data.ToHeaderValue()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMethod(t *testing.T) {
|
|
|
|
t.Run("None", func(t *testing.T) {
|
|
|
|
method := Method("")
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.None, method)
|
2021-04-15 11:02:02 +00:00
|
|
|
method = Method("foo")
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.None, method)
|
2021-04-15 11:02:02 +00:00
|
|
|
})
|
|
|
|
t.Run("Basic", func(t *testing.T) {
|
|
|
|
method := Method("Basic")
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.Basic, method)
|
2021-04-15 11:02:02 +00:00
|
|
|
})
|
|
|
|
t.Run("Basic different text case", func(t *testing.T) {
|
|
|
|
method := Method("bAsIc")
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.Basic, method)
|
2021-04-15 11:02:02 +00:00
|
|
|
})
|
|
|
|
t.Run("Bearer", func(t *testing.T) {
|
|
|
|
method := Method("Bearer")
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.Bearer, method)
|
2021-04-15 11:02:02 +00:00
|
|
|
})
|
|
|
|
t.Run("Bearer different text case", func(t *testing.T) {
|
|
|
|
method := Method("bEaReR")
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.Bearer, method)
|
2021-04-15 11:02:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEndpointEquals(t *testing.T) {
|
|
|
|
e := Endpoint{
|
|
|
|
Url: "Url",
|
|
|
|
Auth: AuthorizationData{
|
2021-09-16 19:12:27 +00:00
|
|
|
Method: authorization.Basic,
|
2021-04-15 11:02:02 +00:00
|
|
|
Value: "Basic username:password",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("equal", func(t *testing.T) {
|
|
|
|
other := Endpoint{
|
|
|
|
Url: "Url",
|
|
|
|
Auth: AuthorizationData{
|
2021-09-16 19:12:27 +00:00
|
|
|
Method: authorization.Basic,
|
2021-04-15 11:02:02 +00:00
|
|
|
Value: "Basic username:password",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, true, e.Equals(other))
|
|
|
|
})
|
|
|
|
t.Run("different URL", func(t *testing.T) {
|
|
|
|
other := Endpoint{
|
|
|
|
Url: "Different",
|
|
|
|
Auth: AuthorizationData{
|
2021-09-16 19:12:27 +00:00
|
|
|
Method: authorization.Basic,
|
2021-04-15 11:02:02 +00:00
|
|
|
Value: "Basic username:password",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, false, e.Equals(other))
|
|
|
|
})
|
|
|
|
t.Run("different auth data", func(t *testing.T) {
|
|
|
|
other := Endpoint{
|
|
|
|
Url: "Url",
|
|
|
|
Auth: AuthorizationData{
|
2021-09-16 19:12:27 +00:00
|
|
|
Method: authorization.Bearer,
|
2021-04-15 11:02:02 +00:00
|
|
|
Value: "Bearer token",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, false, e.Equals(other))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizationDataEquals(t *testing.T) {
|
|
|
|
d := AuthorizationData{
|
2021-09-16 19:12:27 +00:00
|
|
|
Method: authorization.Basic,
|
2021-04-15 11:02:02 +00:00
|
|
|
Value: "username:password",
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("equal", func(t *testing.T) {
|
|
|
|
other := AuthorizationData{
|
2021-09-16 19:12:27 +00:00
|
|
|
Method: authorization.Basic,
|
2021-04-15 11:02:02 +00:00
|
|
|
Value: "username:password",
|
|
|
|
}
|
|
|
|
assert.Equal(t, true, d.Equals(other))
|
|
|
|
})
|
|
|
|
t.Run("different method", func(t *testing.T) {
|
|
|
|
other := AuthorizationData{
|
2021-09-16 19:12:27 +00:00
|
|
|
Method: authorization.None,
|
2021-04-15 11:02:02 +00:00
|
|
|
Value: "username:password",
|
|
|
|
}
|
|
|
|
assert.Equal(t, false, d.Equals(other))
|
|
|
|
})
|
|
|
|
t.Run("different value", func(t *testing.T) {
|
|
|
|
other := AuthorizationData{
|
2021-09-16 19:12:27 +00:00
|
|
|
Method: authorization.Basic,
|
2021-04-15 11:02:02 +00:00
|
|
|
Value: "different:different",
|
|
|
|
}
|
|
|
|
assert.Equal(t, false, d.Equals(other))
|
|
|
|
})
|
|
|
|
}
|