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