mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-03 20:40:25 +08:00
60 lines
1.1 KiB
Go
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))
|
|
}
|