middleware/find_token_test.go

51 lines
1.4 KiB
Go
Raw Permalink Normal View History

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