2023-11-16 03:51:52 +08:00
|
|
|
//nolint:usestdlibvars,noctx // ok here
|
2014-09-26 09:28:28 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
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)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 200, response.Code)
|
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)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 401, response.Code)
|
2014-09-26 09:28:28 +08:00
|
|
|
}
|