1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-12 01:22:21 +02:00
echo/middleware/auth_test.go
Vishal Rana 73fa05f826 Added panic recover middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
2015-05-17 22:54:29 -07:00

76 lines
1.8 KiB
Go

package middleware
import (
"encoding/base64"
"net/http"
"testing"
"github.com/labstack/echo"
)
func TestBasicAuth(t *testing.T) {
req, _ := http.NewRequest(echo.POST, "/", nil)
res := &echo.Response{}
c := echo.NewContext(req, res, echo.New())
fn := func(u, p string) bool {
if u == "joe" && p == "secret" {
return true
}
return false
}
ba := BasicAuth(fn)
//-------------------
// Valid credentials
//-------------------
auth := Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
req.Header.Set(echo.Authorization, auth)
if ba(c) != nil {
t.Error("expected `pass`")
}
// Case insensitive
auth = "basic " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
req.Header.Set(echo.Authorization, auth)
if ba(c) != nil {
t.Error("expected `pass`, with case insensitive header.")
}
//---------------------
// Invalid credentials
//---------------------
// Incorrect password
auth = Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe: password"))
req.Header.Set(echo.Authorization, auth)
ba = BasicAuth(fn)
if ba(c) == nil {
t.Error("expected `fail`, with incorrect password.")
}
// Empty Authorization header
req.Header.Set(echo.Authorization, "")
ba = BasicAuth(fn)
if ba(c) == nil {
t.Error("expected `fail`, with empty Authorization header.")
}
// Invalid Authorization header
auth = base64.StdEncoding.EncodeToString([]byte(" :secret"))
req.Header.Set(echo.Authorization, auth)
ba = BasicAuth(fn)
if ba(c) == nil {
t.Error("expected `fail`, with invalid Authorization header.")
}
// Invalid scheme
auth = "Base " + base64.StdEncoding.EncodeToString([]byte(" :secret"))
req.Header.Set(echo.Authorization, auth)
ba = BasicAuth(fn)
if ba(c) == nil {
t.Error("expected `fail`, with invalid scheme.")
}
}