2021-04-15 11:02:02 +00:00
|
|
|
package powchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-16 19:12:27 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/network/authorization"
|
2021-04-15 11:02:02 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHttpEndpoint(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
url := "http://test"
|
|
|
|
|
|
|
|
t.Run("URL", func(t *testing.T) {
|
|
|
|
endpoint := HttpEndpoint(url)
|
|
|
|
assert.Equal(t, url, endpoint.Url)
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.None, endpoint.Auth.Method)
|
2021-04-15 11:02:02 +00:00
|
|
|
})
|
|
|
|
t.Run("URL with separator", func(t *testing.T) {
|
|
|
|
endpoint := HttpEndpoint(url + ",")
|
|
|
|
assert.Equal(t, url, endpoint.Url)
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.None, endpoint.Auth.Method)
|
2021-04-15 11:02:02 +00:00
|
|
|
})
|
|
|
|
t.Run("URL with whitespace", func(t *testing.T) {
|
|
|
|
endpoint := HttpEndpoint(" " + url + " ,")
|
|
|
|
assert.Equal(t, url, endpoint.Url)
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.None, endpoint.Auth.Method)
|
2021-04-15 11:02:02 +00:00
|
|
|
})
|
|
|
|
t.Run("Basic auth", func(t *testing.T) {
|
|
|
|
endpoint := HttpEndpoint(url + ",Basic username:password")
|
|
|
|
assert.Equal(t, url, endpoint.Url)
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.Basic, endpoint.Auth.Method)
|
2021-04-15 11:02:02 +00:00
|
|
|
assert.Equal(t, "dXNlcm5hbWU6cGFzc3dvcmQ=", endpoint.Auth.Value)
|
|
|
|
})
|
|
|
|
t.Run("Basic auth with whitespace", func(t *testing.T) {
|
|
|
|
endpoint := HttpEndpoint(url + ", Basic username:password ")
|
|
|
|
assert.Equal(t, url, endpoint.Url)
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.Basic, endpoint.Auth.Method)
|
2021-04-15 11:02:02 +00:00
|
|
|
assert.Equal(t, "dXNlcm5hbWU6cGFzc3dvcmQ=", endpoint.Auth.Value)
|
|
|
|
})
|
|
|
|
t.Run("Basic auth with incorrect format", func(t *testing.T) {
|
|
|
|
hook.Reset()
|
|
|
|
endpoint := HttpEndpoint(url + ",Basic username:password foo")
|
|
|
|
assert.Equal(t, url, endpoint.Url)
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.None, endpoint.Auth.Method)
|
2021-04-15 11:02:02 +00:00
|
|
|
assert.LogsContain(t, hook, "Skipping authorization")
|
|
|
|
})
|
|
|
|
t.Run("Bearer auth", func(t *testing.T) {
|
|
|
|
endpoint := HttpEndpoint(url + ",Bearer token")
|
|
|
|
assert.Equal(t, url, endpoint.Url)
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.Bearer, endpoint.Auth.Method)
|
2021-04-15 11:02:02 +00:00
|
|
|
assert.Equal(t, "token", endpoint.Auth.Value)
|
|
|
|
})
|
|
|
|
t.Run("Bearer auth with whitespace", func(t *testing.T) {
|
|
|
|
endpoint := HttpEndpoint(url + ", Bearer token ")
|
|
|
|
assert.Equal(t, url, endpoint.Url)
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.Bearer, endpoint.Auth.Method)
|
2021-04-15 11:02:02 +00:00
|
|
|
assert.Equal(t, "token", endpoint.Auth.Value)
|
|
|
|
})
|
|
|
|
t.Run("Bearer auth with incorrect format", func(t *testing.T) {
|
|
|
|
hook.Reset()
|
|
|
|
endpoint := HttpEndpoint(url + ",Bearer token foo")
|
|
|
|
assert.Equal(t, url, endpoint.Url)
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.None, endpoint.Auth.Method)
|
2021-04-15 11:02:02 +00:00
|
|
|
assert.LogsContain(t, hook, "Skipping authorization")
|
|
|
|
})
|
|
|
|
t.Run("Too many separators", func(t *testing.T) {
|
|
|
|
endpoint := HttpEndpoint(url + ",Bearer token,foo")
|
|
|
|
assert.Equal(t, url, endpoint.Url)
|
2021-09-16 19:12:27 +00:00
|
|
|
assert.Equal(t, authorization.None, endpoint.Auth.Method)
|
2021-04-15 11:02:02 +00:00
|
|
|
assert.LogsContain(t, hook, "Skipping authorization")
|
|
|
|
})
|
|
|
|
}
|