44 lines
1.3 KiB
Go
44 lines
1.3 KiB
Go
package service
|
||
|
||
import (
|
||
"context"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"testing"
|
||
)
|
||
|
||
// fakeSizeUploader 仅用于单测:返回固定 URL,不真正写存储。
|
||
type fakeSizeUploader struct{}
|
||
|
||
func (fakeSizeUploader) Upload(_ context.Context, key string, _ []byte, _ string) (string, error) {
|
||
return "http://example/" + key, nil
|
||
}
|
||
func (fakeSizeUploader) Enabled() bool { return true }
|
||
|
||
// TestDownloadAndUploadRecordsOriginalSize 校验 worker 在下载阶段记录源文件字节数
|
||
// (即 image_size 的来源:io.ReadAll 读到的原图响应体长度)。
|
||
func TestDownloadAndUploadRecordsOriginalSize(t *testing.T) {
|
||
body := []byte("fake-original-image-bytes-payload-of-known-length-1234567890")
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
w.Header().Set("Content-Type", "image/jpeg")
|
||
_, _ = w.Write(body)
|
||
}))
|
||
defer srv.Close()
|
||
|
||
s := &IngestService{
|
||
uploader: fakeSizeUploader{},
|
||
httpClient: &http.Client{},
|
||
}
|
||
|
||
res := s.downloadAndUpload(context.Background(), srv.URL, "runway")
|
||
if res.err != nil {
|
||
t.Fatalf("downloadAndUpload 失败: %v", res.err)
|
||
}
|
||
if res.size != int64(len(body)) {
|
||
t.Fatalf("res.size = %d, want %d(源文件字节数)", res.size, len(body))
|
||
}
|
||
if res.url == "" {
|
||
t.Fatalf("res.url 不应为空(上传应成功)")
|
||
}
|
||
}
|