Files
wx_service/internal/common/qiniu/handler/upload_handler_test.go
T
2026-02-28 16:45:19 +08:00

104 lines
3.2 KiB
Go

package handler
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"wx_service/config"
qiniuservice "wx_service/internal/common/qiniu/service"
)
func TestQiniuCallbackSuccess(t *testing.T) {
t.Parallel()
gin.SetMode(gin.TestMode)
cfg := config.QiniuConfig{AccessKey: "ak-test", SecretKey: "sk-test"}
h := NewUploadHandler(qiniuservice.NewQiniuService(cfg))
body := "key=uploads/test.png&hash=abc&fsize=12&mimeType=image%2Fpng"
req := httptest.NewRequest(http.MethodPost, "/api/v1/common/upload/qiniu/callback", strings.NewReader(body))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Authorization", "QBox "+cfg.AccessKey+":"+signQiniu(req.URL.Path+"\n"+body, cfg.SecretKey))
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = req
h.QiniuCallback(c)
if w.Code != http.StatusOK {
t.Fatalf("status=%d, want=200, body=%s", w.Code, w.Body.String())
}
if !strings.Contains(w.Body.String(), `"code":200`) {
t.Fatalf("unexpected response body: %s", w.Body.String())
}
}
func TestQiniuCallbackInvalidSignature(t *testing.T) {
t.Parallel()
gin.SetMode(gin.TestMode)
cfg := config.QiniuConfig{AccessKey: "ak-test", SecretKey: "sk-test"}
h := NewUploadHandler(qiniuservice.NewQiniuService(cfg))
body := "key=uploads/test.png&hash=abc"
req := httptest.NewRequest(http.MethodPost, "/api/v1/common/upload/qiniu/callback", strings.NewReader(body))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Authorization", "QBox ak-test:bad-sign")
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = req
h.QiniuCallback(c)
if w.Code != http.StatusUnauthorized {
t.Fatalf("status=%d, want=401, body=%s", w.Code, w.Body.String())
}
}
func TestQiniuCallbackMissingKey(t *testing.T) {
t.Parallel()
gin.SetMode(gin.TestMode)
cfg := config.QiniuConfig{AccessKey: "ak-test", SecretKey: "sk-test"}
h := NewUploadHandler(qiniuservice.NewQiniuService(cfg))
body := "hash=abc&fsize=12"
req := httptest.NewRequest(http.MethodPost, "/api/v1/common/upload/qiniu/callback", strings.NewReader(body))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Authorization", "QBox "+cfg.AccessKey+":"+signQiniu(req.URL.Path+"\n"+body, cfg.SecretKey))
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = req
h.QiniuCallback(c)
if w.Code != http.StatusBadRequest {
t.Fatalf("status=%d, want=400, body=%s", w.Code, w.Body.String())
}
}
func TestParseQiniuCallbackPayloadJSON(t *testing.T) {
t.Parallel()
raw := []byte(`{"key":"uploads/test.png","hash":"abc","fsize":321,"mimeType":"image/png"}`)
got, err := parseQiniuCallbackPayload("application/json", raw)
if err != nil {
t.Fatalf("parseQiniuCallbackPayload: %v", err)
}
if got.Key != "uploads/test.png" || got.Hash != "abc" || got.Fsize != 321 {
t.Fatalf("unexpected payload: %+v", got)
}
}
func signQiniu(signing, secret string) string {
mac := hmac.New(sha1.New, []byte(secret))
_, _ = mac.Write([]byte(signing))
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(mac.Sum(nil))
}