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-03-22 02:27:14 +02:00
|
|
|
rq := test.NewRequest(echo.GET, "/", nil)
|
|
|
|
rs := test.NewResponseRecorder()
|
2016-04-17 01:02:22 +02:00
|
|
|
c := e.NewContext(rq, rs)
|
2016-03-19 05:02:02 +02:00
|
|
|
f := func(u, p string) bool {
|
2015-05-12 00:43:54 +02:00
|
|
|
if u == "joe" && p == "secret" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2016-04-02 23:19:39 +02:00
|
|
|
h := BasicAuth(f)(func(c echo.Context) error {
|
2016-02-08 09:02:37 +02:00
|
|
|
return c.String(http.StatusOK, "test")
|
2016-04-02 23:19:39 +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-04-06 16:28:53 +02:00
|
|
|
rq.Header().Set(echo.HeaderAuthorization, auth)
|
2016-04-02 23:19:39 +02:00
|
|
|
assert.NoError(t, h(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-04-06 16:28:53 +02:00
|
|
|
rq.Header().Set(echo.HeaderAuthorization, auth)
|
2016-04-02 23:19:39 +02:00
|
|
|
he := h(c).(*echo.HTTPError)
|
2016-03-11 17:53:54 +02:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
2016-04-06 16:28:53 +02:00
|
|
|
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.HeaderWWWAuthenticate))
|
2015-05-18 07:54:29 +02:00
|
|
|
|
|
|
|
// Empty Authorization header
|
2016-04-06 16:28:53 +02:00
|
|
|
rq.Header().Set(echo.HeaderAuthorization, "")
|
2016-04-02 23:19:39 +02:00
|
|
|
he = h(c).(*echo.HTTPError)
|
2016-03-11 17:53:54 +02:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
2016-04-06 16:28:53 +02:00
|
|
|
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.HeaderWWWAuthenticate))
|
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-04-06 16:28:53 +02:00
|
|
|
rq.Header().Set(echo.HeaderAuthorization, auth)
|
2016-04-02 23:19:39 +02:00
|
|
|
he = h(c).(*echo.HTTPError)
|
2016-03-11 17:53:54 +02:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
2016-04-06 16:28:53 +02:00
|
|
|
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.HeaderWWWAuthenticate))
|
2015-05-12 00:43:54 +02:00
|
|
|
}
|