2015-05-11 15:43:54 -07:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2015-05-17 22:54:29 -07:00
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2016-01-28 23:46:11 -08:00
|
|
|
"github.com/labstack/echo/test"
|
2015-05-30 10:54:55 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-11 15:43:54 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasicAuth(t *testing.T) {
|
2015-12-01 11:22:45 -08:00
|
|
|
e := echo.New()
|
2016-01-28 23:46:11 -08:00
|
|
|
req := test.NewRequest(echo.GET, "/", nil)
|
|
|
|
res := test.NewResponseRecorder()
|
|
|
|
c := echo.NewContext(req, res, e)
|
2015-05-11 15:43:54 -07:00
|
|
|
fn := func(u, p string) bool {
|
|
|
|
if u == "joe" && p == "secret" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2016-02-17 21:49:31 -08:00
|
|
|
h := BasicAuth(fn)(echo.HandlerFunc(func(c echo.Context) error {
|
2016-02-07 23:02:37 -08:00
|
|
|
return c.String(http.StatusOK, "test")
|
2016-02-15 08:11:29 -08:00
|
|
|
}))
|
2015-05-11 15:43:54 -07:00
|
|
|
|
|
|
|
// Valid credentials
|
2016-02-07 23:02:37 -08:00
|
|
|
auth := basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
|
2016-01-09 09:44:18 -08:00
|
|
|
req.Header().Set(echo.Authorization, auth)
|
2016-02-15 08:11:29 -08:00
|
|
|
assert.NoError(t, h.Handle(c))
|
2015-05-11 15:43:54 -07:00
|
|
|
|
|
|
|
//---------------------
|
|
|
|
// Invalid credentials
|
|
|
|
//---------------------
|
|
|
|
|
2015-05-16 12:49:01 -07:00
|
|
|
// Incorrect password
|
2016-02-07 23:02:37 -08:00
|
|
|
auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:password"))
|
2016-01-09 09:44:18 -08:00
|
|
|
req.Header().Set(echo.Authorization, auth)
|
2016-02-15 08:11:29 -08:00
|
|
|
he := h.Handle(c).(*echo.HTTPError)
|
2015-05-30 10:54:55 -07:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, he.Code())
|
2016-02-07 23:02:37 -08:00
|
|
|
assert.Equal(t, basic+" realm=Restricted", res.Header().Get(echo.WWWAuthenticate))
|
2015-05-17 22:54:29 -07:00
|
|
|
|
|
|
|
// Empty Authorization header
|
2016-01-28 23:46:11 -08:00
|
|
|
req.Header().Set(echo.Authorization, "")
|
2016-02-15 08:11:29 -08:00
|
|
|
he = h.Handle(c).(*echo.HTTPError)
|
2015-09-01 11:24:36 -07:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, he.Code())
|
2016-02-07 23:02:37 -08:00
|
|
|
assert.Equal(t, basic+" realm=Restricted", res.Header().Get(echo.WWWAuthenticate))
|
2015-05-11 15:43:54 -07:00
|
|
|
|
2015-05-17 22:54:29 -07:00
|
|
|
// Invalid Authorization header
|
2015-07-05 23:21:05 +10:00
|
|
|
auth = base64.StdEncoding.EncodeToString([]byte("invalid"))
|
2016-01-28 23:46:11 -08:00
|
|
|
req.Header().Set(echo.Authorization, auth)
|
2016-02-15 08:11:29 -08:00
|
|
|
he = h.Handle(c).(*echo.HTTPError)
|
2015-09-01 11:24:36 -07:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, he.Code())
|
2016-02-07 23:02:37 -08:00
|
|
|
assert.Equal(t, basic+" realm=Restricted", res.Header().Get(echo.WWWAuthenticate))
|
2015-05-30 10:54:55 -07:00
|
|
|
|
|
|
|
// WebSocket
|
2016-01-28 23:46:11 -08:00
|
|
|
c.Request().Header().Set(echo.Upgrade, echo.WebSocket)
|
2016-02-15 08:11:29 -08:00
|
|
|
assert.NoError(t, h.Handle(c))
|
2015-05-11 15:43:54 -07:00
|
|
|
}
|