2014-09-26 09:28:28 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2/gobottest"
|
2014-09-26 09:28:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasicAuth(t *testing.T) {
|
|
|
|
a := initTestAPI()
|
|
|
|
|
|
|
|
a.AddHandler(BasicAuth("admin", "password"))
|
|
|
|
|
|
|
|
request, _ := http.NewRequest("GET", "/api/", nil)
|
|
|
|
request.SetBasicAuth("admin", "password")
|
|
|
|
response := httptest.NewRecorder()
|
|
|
|
a.ServeHTTP(response, request)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, response.Code, 200)
|
2014-09-26 09:28:28 +08:00
|
|
|
|
|
|
|
request, _ = http.NewRequest("GET", "/api/", nil)
|
|
|
|
request.SetBasicAuth("admin", "wrongPassword")
|
|
|
|
response = httptest.NewRecorder()
|
|
|
|
a.ServeHTTP(response, request)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, response.Code, 401)
|
2014-09-26 09:28:28 +08:00
|
|
|
}
|