Files
Agent-System-Backend/internal/server/artifact_download_test.go
T
weichen f1302ed5b3 Enable canonical versioned Go module consumption
Why: Allow consumers to download an immutable module revision without nested source checkouts or local path replacements.

Evidence: SELF package tests and the prior canonical-path rejection are recorded in the Dev dependency-workspace task card; remote consumption is replayed against the committed candidate.

Impact: Change the module identity and its Go imports while preserving API behavior. Existing historical submodule revisions remain buildable.

Refs: Versioned dependencies U1/U3; feature/dependency-workspace
2026-10-13 15:47:16 +00:00

65 lines
2.6 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 server
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"git-ssh.u1680832.nyat.app/weichen/Agent-System-Backend.git/internal/store"
core "git-ssh.u1680832.nyat.app/weichen/Agent-System-Core.git"
)
// TestArtifactContentDisposition:?download=1 切换 attachment;中文名走 RFC 5987
// filename* 权威编码;默认 inline 便于浏览器内预览。
func TestArtifactContentDisposition(t *testing.T) {
srv, st := newTestServer(t)
h := srv.Handler()
a := &core.Artifact{ProjectID: "p1", Owner: store.OwnerDefault, Name: "会议纪要.docx", Kind: "document"}
if err := st.CreateArtifact(a, []byte("PK\x03\x04 docx bytes")); err != nil {
t.Fatal(err)
}
get := func(query string) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodGet, "/api/artifacts/"+a.ID+"/content"+query, nil)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec
}
inline := get("")
if cd := inline.Header().Get("Content-Disposition"); !strings.HasPrefix(cd, "inline") ||
!strings.Contains(cd, `filename*=UTF-8''%E4%BC%9A%E8%AE%AE%E7%BA%AA%E8%A6%81.docx`) {
t.Errorf("inline disposition mismatch: %s", cd)
}
if ct := inline.Header().Get("Content-Type"); ct != "application/vnd.openxmlformats-officedocument.wordprocessingml.document" {
t.Errorf("docx content-type fallback mismatch: %s", ct)
}
dl := get("?download=1")
if cd := dl.Header().Get("Content-Disposition"); !strings.HasPrefix(cd, "attachment") ||
!strings.Contains(cd, `filename="____.docx"`) { // ASCII 兜底:4 个汉字 → 4 个下划线
t.Errorf("attachment disposition mismatch: %s", cd)
}
}
// TestContentTypeByExtension:MIME 为空时按扩展名回退(docx 显式表 + csv 显式表),
// 未知扩展回落 text/plain,image kind 回落二进制流。
func TestContentTypeByExtension(t *testing.T) {
if ct := contentTypeOf(core.Artifact{Name: "a.DOCX"}); ct != "application/vnd.openxmlformats-officedocument.wordprocessingml.document" {
t.Errorf("docx ext (uppercase) mismatch: %s", ct)
}
if ct := contentTypeOf(core.Artifact{Name: "b.csv"}); !strings.HasPrefix(ct, "text/csv") {
t.Errorf("csv ext mismatch: %s", ct)
}
if ct := contentTypeOf(core.Artifact{Name: "c.bin", Kind: "image"}); ct != "application/octet-stream" {
t.Errorf("image fallback mismatch: %s", ct)
}
if ct := contentTypeOf(core.Artifact{Name: "d"}); !strings.HasPrefix(ct, "text/plain") {
t.Errorf("plain fallback mismatch: %s", ct)
}
if ct := contentTypeOf(core.Artifact{Name: "e.md", MIME: "text/x-override"}); ct != "text/x-override" {
t.Errorf("explicit mime must win: %s", ct)
}
}