2015-05-12 00:43:54 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2015-05-18 07:54:29 +02:00
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2015-05-12 00:43:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasicAuth(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(echo.POST, "/", nil)
|
2015-05-18 07:54:29 +02:00
|
|
|
res := &echo.Response{}
|
2015-05-12 00:43:54 +02:00
|
|
|
c := echo.NewContext(req, res, echo.New())
|
|
|
|
fn := func(u, p string) bool {
|
|
|
|
if u == "joe" && p == "secret" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2015-05-14 08:07:03 +02:00
|
|
|
ba := BasicAuth(fn)
|
2015-05-12 00:43:54 +02:00
|
|
|
|
|
|
|
//-------------------
|
|
|
|
// Valid credentials
|
|
|
|
//-------------------
|
|
|
|
|
|
|
|
auth := Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
|
|
|
|
req.Header.Set(echo.Authorization, auth)
|
2015-05-14 08:07:03 +02:00
|
|
|
if ba(c) != nil {
|
2015-05-16 21:49:01 +02:00
|
|
|
t.Error("expected `pass`")
|
2015-05-12 00:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Case insensitive
|
|
|
|
auth = "basic " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
|
|
|
|
req.Header.Set(echo.Authorization, auth)
|
2015-05-14 08:07:03 +02:00
|
|
|
if ba(c) != nil {
|
2015-05-18 07:54:29 +02:00
|
|
|
t.Error("expected `pass`, with case insensitive header.")
|
2015-05-12 00:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------
|
|
|
|
// Invalid credentials
|
|
|
|
//---------------------
|
|
|
|
|
2015-05-16 21:49:01 +02:00
|
|
|
// Incorrect password
|
2015-05-21 23:02:29 +02:00
|
|
|
auth = Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:password"))
|
2015-05-12 00:43:54 +02:00
|
|
|
req.Header.Set(echo.Authorization, auth)
|
2015-05-14 08:07:03 +02:00
|
|
|
ba = BasicAuth(fn)
|
2015-05-21 23:02:29 +02:00
|
|
|
he := ba(c).(*echo.HTTPError)
|
2015-05-14 08:07:03 +02:00
|
|
|
if ba(c) == nil {
|
2015-05-18 07:54:29 +02:00
|
|
|
t.Error("expected `fail`, with incorrect password.")
|
2015-05-22 13:40:01 +02:00
|
|
|
} else if he.Code() != http.StatusUnauthorized {
|
|
|
|
t.Errorf("expected status `401`, got %d", he.Code())
|
2015-05-18 07:54:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Empty Authorization header
|
|
|
|
req.Header.Set(echo.Authorization, "")
|
|
|
|
ba = BasicAuth(fn)
|
2015-05-21 23:02:29 +02:00
|
|
|
he = ba(c).(*echo.HTTPError)
|
|
|
|
if he == nil {
|
2015-05-18 07:54:29 +02:00
|
|
|
t.Error("expected `fail`, with empty Authorization header.")
|
2015-05-22 13:40:01 +02:00
|
|
|
} else if he.Code() != http.StatusBadRequest {
|
|
|
|
t.Errorf("expected status `400`, got %d", he.Code())
|
2015-05-12 00:43:54 +02:00
|
|
|
}
|
|
|
|
|
2015-05-18 07:54:29 +02:00
|
|
|
// Invalid Authorization header
|
2015-05-12 15:28:25 +02:00
|
|
|
auth = base64.StdEncoding.EncodeToString([]byte(" :secret"))
|
|
|
|
req.Header.Set(echo.Authorization, auth)
|
2015-05-14 08:07:03 +02:00
|
|
|
ba = BasicAuth(fn)
|
2015-05-21 23:02:29 +02:00
|
|
|
he = ba(c).(*echo.HTTPError)
|
|
|
|
if he == nil {
|
2015-05-18 07:54:29 +02:00
|
|
|
t.Error("expected `fail`, with invalid Authorization header.")
|
2015-05-22 13:40:01 +02:00
|
|
|
} else if he.Code() != http.StatusBadRequest {
|
|
|
|
t.Errorf("expected status `400`, got %d", he.Code())
|
2015-05-12 15:28:25 +02:00
|
|
|
}
|
|
|
|
|
2015-05-12 00:43:54 +02:00
|
|
|
// Invalid scheme
|
2015-05-21 23:02:29 +02:00
|
|
|
auth = "Ace " + base64.StdEncoding.EncodeToString([]byte(" :secret"))
|
2015-05-12 00:43:54 +02:00
|
|
|
req.Header.Set(echo.Authorization, auth)
|
2015-05-14 08:07:03 +02:00
|
|
|
ba = BasicAuth(fn)
|
2015-05-21 23:02:29 +02:00
|
|
|
he = ba(c).(*echo.HTTPError)
|
|
|
|
if he == nil {
|
2015-05-18 07:54:29 +02:00
|
|
|
t.Error("expected `fail`, with invalid scheme.")
|
2015-05-22 13:40:01 +02:00
|
|
|
} else if he.Code() != http.StatusBadRequest {
|
|
|
|
t.Errorf("expected status `400`, got %d", he.Code())
|
2015-05-12 00:43:54 +02:00
|
|
|
}
|
|
|
|
}
|