Files
EdgeCommon/pkg/serverconfigs/http_auth_method_basic_test.go
GoEdgeLab 2f3981704e v1.4.1
2024-07-27 13:29:26 +08:00

60 lines
1.1 KiB
Go

// Copyright 2021 GoEdge CDN goedge.cdn@gmail.com. All rights reserved.
package serverconfigs
import (
"encoding/base64"
"net/http"
"testing"
"github.com/iwind/TeaGo/assert"
"github.com/iwind/TeaGo/maps"
)
func TestHTTPAuthBasicMethodUser_Validate(t *testing.T) {
var a = assert.NewAssertion(t)
{
user := &HTTPAuthBasicMethodUser{
Password: "123456",
}
b, err := user.Validate("123456")
if err != nil {
t.Fatal(err)
}
a.IsTrue(b)
}
{
user := &HTTPAuthBasicMethodUser{
Password: "654321",
}
b, err := user.Validate("123456")
if err != nil {
t.Fatal(err)
}
a.IsFalse(b)
}
}
func TestHTTPAuthBasicMethod_Filter(t *testing.T) {
var method = &HTTPAuthBasicMethod{}
err := method.Init(map[string]interface{}{
"users": []maps.Map{
{
"username": "hello",
"password": "world",
},
},
})
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest(http.MethodGet, "http://teaos.cn/", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("hello:world")))
t.Log(method.Filter(req, nil, nil))
}