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