69 lines
2.2 KiB
Go
69 lines
2.2 KiB
Go
package web
|
||||
|
|
|
|||
|
|
import (
|
|||
|
|
"net/http"
|
|||
|
|
"net/http/httptest"
|
|||
|
|
"net/url"
|
|||
|
|
"strings"
|
|||
|
|
"testing"
|
|||
|
|
|
|||
|
|
"github.com/gin-gonic/gin"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
func sybPostContext(t *testing.T, values url.Values) (*gin.Context, *httptest.ResponseRecorder) {
|
|||
|
|
t.Helper()
|
|||
|
|
gin.SetMode(gin.TestMode)
|
|||
|
|
recorder := httptest.NewRecorder()
|
|||
|
|
context, _ := gin.CreateTestContext(recorder)
|
|||
|
|
request := httptest.NewRequest(http.MethodPost, "/syb/sync", strings.NewReader(values.Encode()))
|
|||
|
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|||
|
|
context.Request = request
|
|||
|
|
return context, recorder
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestSybRedirectLogin_保留指定日期并打开登录弹窗(t *testing.T) {
|
|||
|
|
context, recorder := sybPostContext(t, url.Values{
|
|||
|
|
"order_no": {"ORDER-1"},
|
|||
|
|
"date_from": {"2026-08-01"},
|
|||
|
|
"date_to": {"2026-08-09"},
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
(&Handler{}).sybRedirectLogin(context, "")
|
|||
|
|
if context.Writer.Status() != http.StatusSeeOther {
|
|||
|
|
t.Fatalf("状态码 = %d,期望 %d", context.Writer.Status(), http.StatusSeeOther)
|
|||
|
|
}
|
|||
|
|
location, err := url.Parse(recorder.Header().Get("Location"))
|
|||
|
|
if err != nil {
|
|||
|
|
t.Fatalf("解析 Location 失败: %v", err)
|
|||
|
|
}
|
|||
|
|
query := location.Query()
|
|||
|
|
for name, want := range map[string]string{
|
|||
|
|
"login": "1", "order_no": "ORDER-1",
|
|||
|
|
"date_from": "2026-08-01", "date_to": "2026-08-09",
|
|||
|
|
} {
|
|||
|
|
if got := query.Get(name); got != want {
|
|||
|
|
t.Errorf("%s = %q,期望 %q;Location=%s", name, got, want, location.String())
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestSybRedirectRange_保留输入并重新打开日期弹窗(t *testing.T) {
|
|||
|
|
context, recorder := sybPostContext(t, url.Values{
|
|||
|
|
"date_from": {"2026-08-09"},
|
|||
|
|
"date_to": {"2026-08-01"},
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
(&Handler{}).sybRedirectRange(context, "开始日期不能晚于结束日期")
|
|||
|
|
location, err := url.Parse(recorder.Header().Get("Location"))
|
|||
|
|
if err != nil {
|
|||
|
|
t.Fatalf("解析 Location 失败: %v", err)
|
|||
|
|
}
|
|||
|
|
query := location.Query()
|
|||
|
|
if query.Get("range_sync") != "1" || query.Get("range_error") == "" {
|
|||
|
|
t.Fatalf("日期错误后应该重开弹窗并显示错误,Location=%s", location.String())
|
|||
|
|
}
|
|||
|
|
if query.Get("date_from") != "2026-08-09" || query.Get("date_to") != "2026-08-01" {
|
|||
|
|
t.Fatalf("日期错误后应该保留两端输入,Location=%s", location.String())
|
|||
|
|
}
|
|||
|
|
}
|