存档功能
This commit is contained in:
95
internal/handler/version_test.go
Normal file
95
internal/handler/version_test.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestParseVersionFromName(t *testing.T) {
|
||||
cases := []struct {
|
||||
name, want string
|
||||
}{
|
||||
{"PixelArcade-Setup-1.0.1.exe", "1.0.1"},
|
||||
{"PixelArcade-Setup-2.10.0.msi", "2.10.0"},
|
||||
{"foo.zip", ""},
|
||||
{"Setup.exe", ""},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if g := parseVersionFromName(c.name); g != c.want {
|
||||
t.Fatalf("parseVersionFromName(%q)=%q, want %q", c.name, g, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareSemver(t *testing.T) {
|
||||
if compareSemver("1.0.1", "1.0.0") <= 0 {
|
||||
t.Fatal("1.0.1 should be newer than 1.0.0")
|
||||
}
|
||||
if compareSemver("1.0.0", "1.0.0") != 0 {
|
||||
t.Fatal("equal versions")
|
||||
}
|
||||
if compareSemver("1.0.0", "1.2.0") >= 0 {
|
||||
t.Fatal("1.0.0 should be older than 1.2.0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanonicalDownloadPath(t *testing.T) {
|
||||
cases := []struct{ in, want string }{
|
||||
{"", ""},
|
||||
{"/download/PixelArcade-Setup-1.0.1.exe", "/api/download/PixelArcade-Setup-1.0.1.exe"},
|
||||
{"/api/download/foo.exe", "/api/download/foo.exe"},
|
||||
{"https://game.nailaoyun.cn/download/foo.exe", "/api/download/foo.exe"},
|
||||
{"http://127.0.0.1:8080/download/foo.exe", "/api/download/foo.exe"},
|
||||
{"https://cdn.example.com/releases/foo.exe", "https://cdn.example.com/releases/foo.exe"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if g := canonicalDownloadPath(c.in); g != c.want {
|
||||
t.Fatalf("canonicalDownloadPath(%q)=%q, want %q", c.in, g, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionDownloadAttachment(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
dir := t.TempDir()
|
||||
old, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.Chdir(old) })
|
||||
if err := os.Chdir(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(releaseDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
name := "PixelArcade-Setup-1.2.3.exe"
|
||||
body := append([]byte("MZ"), []byte(strings.Repeat("x", 80))...)
|
||||
if err := os.WriteFile(filepath.Join(releaseDir, name), body, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/api/download/"+name, nil)
|
||||
c.Params = gin.Params{{Key: "filename", Value: name}}
|
||||
VersionDownload(c)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("status %d", w.Code)
|
||||
}
|
||||
cd := w.Header().Get("Content-Disposition")
|
||||
if !strings.Contains(cd, name) || !strings.Contains(cd, "attachment") {
|
||||
t.Fatalf("Content-Disposition %q", cd)
|
||||
}
|
||||
if w.Header().Get("Content-Type") != "application/octet-stream" {
|
||||
t.Fatalf("Content-Type %q", w.Header().Get("Content-Type"))
|
||||
}
|
||||
if !strings.HasPrefix(w.Body.String(), "MZ") {
|
||||
t.Fatal("body should start with MZ")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user