123 lines
3.2 KiB
Go
123 lines
3.2 KiB
Go
package hyfile
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestUploadPDF_sendsAuthorizationAndParsesFileId(t *testing.T) {
|
|
SetUploadMinInterval(0)
|
|
t.Cleanup(func() {
|
|
SetUploadMinInterval(defaultUploadMinInterval)
|
|
ResetUploadThrottleForTest()
|
|
})
|
|
ResetUploadThrottleForTest()
|
|
|
|
const wantToken = "accessKey:sign:policyB64"
|
|
var gotAuth string
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotAuth = r.Header.Get("X-Authorization")
|
|
if r.Method != http.MethodPost {
|
|
t.Errorf("method=%s", r.Method)
|
|
}
|
|
if r.Header.Get("Content-Type") != "application/pdf" {
|
|
t.Errorf("content-type=%q want application/pdf", r.Header.Get("Content-Type"))
|
|
}
|
|
body, _ := io.ReadAll(r.Body)
|
|
if !strings.HasPrefix(string(body), "%PDF") {
|
|
t.Errorf("body should be raw pdf, got %q", truncateStr(string(body), 32))
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"success": true,
|
|
"record": map[string]string{"fileId": "abc123"},
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
fileID, err := UploadPDF([]byte("%PDF-1.4 test"), "", srv.URL, wantToken, "")
|
|
if err != nil {
|
|
t.Fatalf("UploadPDF: %v", err)
|
|
}
|
|
if fileID != "abc123" {
|
|
t.Fatalf("fileId=%q", fileID)
|
|
}
|
|
if gotAuth != wantToken {
|
|
t.Fatalf("X-Authorization=%q want %q", gotAuth, wantToken)
|
|
}
|
|
}
|
|
|
|
func TestUploadPDF_forbiddenForward(t *testing.T) {
|
|
disableUploadThrottle(t)
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
io.Copy(io.Discard, r.Body)
|
|
http.Error(w, "forbidden", http.StatusForbidden)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
_, err := UploadPDF([]byte("x"), "x.pdf", srv.URL, "token", "")
|
|
if err == nil || !strings.Contains(err.Error(), "403") {
|
|
t.Fatalf("err=%v", err)
|
|
}
|
|
if !strings.Contains(err.Error(), "forward-go") {
|
|
t.Fatalf("want forward hint, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestUploadPDF_forbiddenGovMessage(t *testing.T) {
|
|
disableUploadThrottle(t)
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
io.Copy(io.Discard, r.Body)
|
|
w.WriteHeader(http.StatusForbidden)
|
|
_, _ = w.Write([]byte(`{"message":"token expired"}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
_, err := UploadPDF([]byte("x"), "x.pdf", srv.URL, "token", "")
|
|
if err == nil || !strings.Contains(err.Error(), "token expired") {
|
|
t.Fatalf("err=%v", err)
|
|
}
|
|
if !strings.Contains(err.Error(), "2.3") {
|
|
t.Fatalf("want 2.3 troubleshooting hints, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestUploadPDF_forbiddenGovRateLimitHint(t *testing.T) {
|
|
disableUploadThrottle(t)
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
io.Copy(io.Discard, r.Body)
|
|
w.WriteHeader(http.StatusForbidden)
|
|
_, _ = w.Write([]byte(`{"message":"请求过于频繁"}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
_, err := UploadPDF([]byte("x"), "x.pdf", srv.URL, "token", "")
|
|
if err == nil || !strings.Contains(err.Error(), "10 秒 1 次") {
|
|
t.Fatalf("err=%v", err)
|
|
}
|
|
}
|
|
|
|
func truncateStr(s string, n int) string {
|
|
if len(s) <= n {
|
|
return s
|
|
}
|
|
return s[:n]
|
|
}
|
|
|
|
func disableUploadThrottle(t *testing.T) {
|
|
t.Helper()
|
|
SetUploadMinInterval(0)
|
|
t.Cleanup(func() {
|
|
SetUploadMinInterval(defaultUploadMinInterval)
|
|
ResetUploadThrottleForTest()
|
|
})
|
|
ResetUploadThrottleForTest()
|
|
}
|