package middleware import ( "net/http" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestTokenFromAuthorizationHeader(t *testing.T) { req, err := http.NewRequest("GET", "/", http.NoBody) require.Nil(t, err) req.Header.Set("Authorization", "abc") assert.Equal(t, "abc", TokenFromAuthorizationHeader(req)) req, err = http.NewRequest("GET", "/", http.NoBody) require.Nil(t, err) req.Header.Set("Authorization", "abcdefghe") assert.Equal(t, "abcdefghe", TokenFromAuthorizationHeader(req)) req, err = http.NewRequest("GET", "/", http.NoBody) require.Nil(t, err) req.Header.Set("Authorization", "Bearer abc") assert.Equal(t, "abc", TokenFromAuthorizationHeader(req)) } func TestTokenFromHeader(t *testing.T) { req, err := http.NewRequest("GET", "/", http.NoBody) require.Nil(t, err) req.Header.Set("X-Token", "abc") assert.Equal(t, "abc", TokenFromHeader("X-Token")(req)) } func TestTokenFromQuery(t *testing.T) { req, err := http.NewRequest("GET", "/?token=abc", http.NoBody) require.Nil(t, err) assert.Equal(t, "abc", TokenFromQuery("token")(req)) } func TestTokenFromCookie(t *testing.T) { req, err := http.NewRequest("GET", "/", http.NoBody) require.Nil(t, err) req.AddCookie(&http.Cookie{Name: "token", Value: "abc"}) assert.Equal(t, "abc", TokenFromCookie("token")(req)) req, err = http.NewRequest("GET", "/", http.NoBody) require.Nil(t, err) assert.Equal(t, "", TokenFromCookie("token")(req)) }