Files
tyapi-server/internal/shared/validator/authorization_url_test.go
2025-08-26 14:43:27 +08:00

109 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package validator
import (
"testing"
"github.com/go-playground/validator/v10"
)
func TestValidateAuthorizationURL(t *testing.T) {
validate := validator.New()
RegisterCustomValidators(validate)
tests := []struct {
name string
url string
wantErr bool
}{
{
name: "有效的PDF URL",
url: "https://example.com/document.pdf",
wantErr: false,
},
{
name: "有效的JPG URL",
url: "https://example.com/image.jpg",
wantErr: false,
},
{
name: "有效的JPEG URL",
url: "https://example.com/image.jpeg",
wantErr: false,
},
{
name: "有效的PNG URL",
url: "https://example.com/image.png",
wantErr: false,
},
{
name: "有效的BMP URL",
url: "https://example.com/image.bmp",
wantErr: false,
},
{
name: "HTTP协议的PDF URL",
url: "http://example.com/document.pdf",
wantErr: false,
},
{
name: "带查询参数的PDF URL",
url: "https://example.com/document.pdf?version=1.0",
wantErr: false,
},
{
name: "带路径的PDF URL",
url: "https://example.com/files/documents/contract.pdf",
wantErr: false,
},
{
name: "无效的URL格式",
url: "not-a-url",
wantErr: true,
},
{
name: "不支持的文件类型",
url: "https://example.com/document.doc",
wantErr: true,
},
{
name: "不支持的文件类型2",
url: "https://example.com/document.txt",
wantErr: true,
},
{
name: "没有文件扩展名",
url: "https://example.com/document",
wantErr: true,
},
{
name: "FTP协议不支持",
url: "ftp://example.com/document.pdf",
wantErr: true,
},
{
name: "空字符串",
url: "",
wantErr: true,
},
{
name: "大写扩展名",
url: "https://example.com/document.PDF",
wantErr: false,
},
{
name: "混合大小写扩展名",
url: "https://example.com/document.JpG",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validate.Var(tt.url, "authorization_url")
if (err != nil) != tt.wantErr {
t.Errorf("validateAuthorizationURL() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}