Feat: sign http request / read running mode from config file

This commit is contained in:
HFO4
2019-12-23 13:27:18 +08:00
parent fd7b6e33c8
commit 90827b2441
13 changed files with 199 additions and 45 deletions

View File

@@ -3,6 +3,8 @@ package auth
import (
"github.com/HFO4/cloudreve/pkg/util"
"github.com/stretchr/testify/assert"
"net/http"
"strings"
"testing"
"time"
)
@@ -46,3 +48,25 @@ func TestCheckURI(t *testing.T) {
asserts.Error(CheckURI(sign))
}
}
func TestSignRequest(t *testing.T) {
asserts := assert.New(t)
General = HMACAuth{SecretKey: []byte(util.RandStringRunes(256))}
// 非上传请求
{
req, err := http.NewRequest("POST", "http://127.0.0.1/api/v3/upload", strings.NewReader("I am body."))
asserts.NoError(err)
req = SignRequest(req, 10)
asserts.NotEmpty(req.Header["Authorization"])
}
// 上传请求
{
req, err := http.NewRequest("POST", "http://127.0.0.1/api/v3/upload", strings.NewReader("I am body."))
asserts.NoError(err)
req.Header["X-Policy"] = []string{"I am Policy"}
req = SignRequest(req, 10)
asserts.NotEmpty(req.Header["Authorization"])
}
}