utils_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package utils_test
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "net/url"
  6. "strconv"
  7. "testing"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "github.com/labring/aiproxy/core/controller/utils"
  11. "github.com/smartystreets/goconvey/convey"
  12. )
  13. func TestParsePageParams(t *testing.T) {
  14. gin.SetMode(gin.TestMode)
  15. convey.Convey("ParsePageParams", t, func() {
  16. convey.Convey("should parse valid page and per_page", func() {
  17. w := httptest.NewRecorder()
  18. c, _ := gin.CreateTestContext(w)
  19. c.Request = &http.Request{
  20. URL: &url.URL{
  21. RawQuery: "page=2&per_page=20",
  22. },
  23. }
  24. page, perPage := utils.ParsePageParams(c)
  25. convey.So(page, convey.ShouldEqual, 2)
  26. convey.So(perPage, convey.ShouldEqual, 20)
  27. })
  28. convey.Convey("should parse 'p' alias for page", func() {
  29. w := httptest.NewRecorder()
  30. c, _ := gin.CreateTestContext(w)
  31. c.Request = &http.Request{
  32. URL: &url.URL{
  33. RawQuery: "p=3&per_page=15",
  34. },
  35. }
  36. page, perPage := utils.ParsePageParams(c)
  37. convey.So(page, convey.ShouldEqual, 3)
  38. convey.So(perPage, convey.ShouldEqual, 15)
  39. })
  40. convey.Convey("should return 0 for missing params", func() {
  41. w := httptest.NewRecorder()
  42. c, _ := gin.CreateTestContext(w)
  43. c.Request = &http.Request{
  44. URL: &url.URL{},
  45. }
  46. page, perPage := utils.ParsePageParams(c)
  47. convey.So(page, convey.ShouldEqual, 0)
  48. convey.So(perPage, convey.ShouldEqual, 0)
  49. })
  50. })
  51. }
  52. func TestParseTimeRange(t *testing.T) {
  53. gin.SetMode(gin.TestMode)
  54. convey.Convey("ParseTimeRange", t, func() {
  55. now := time.Now()
  56. startTimeStr := strconv.FormatInt(now.Add(-time.Hour).Unix(), 10)
  57. endTimeStr := strconv.FormatInt(now.Unix(), 10)
  58. convey.Convey("should parse valid timestamps", func() {
  59. w := httptest.NewRecorder()
  60. c, _ := gin.CreateTestContext(w)
  61. c.Request = &http.Request{
  62. URL: &url.URL{
  63. RawQuery: "start_timestamp=" + startTimeStr + "&end_timestamp=" + endTimeStr,
  64. },
  65. }
  66. start, end := utils.ParseTimeRange(c, 0)
  67. // Timestamps are in seconds, so we check if they are roughly equal (ignoring sub-second differences lost in conversion)
  68. convey.So(start.Unix(), convey.ShouldEqual, now.Add(-time.Hour).Unix())
  69. convey.So(end.Unix(), convey.ShouldEqual, now.Unix())
  70. })
  71. convey.Convey("should use default max span if start time is too old", func() {
  72. w := httptest.NewRecorder()
  73. c, _ := gin.CreateTestContext(w)
  74. // Start time 10 days ago
  75. oldStartStr := strconv.FormatInt(now.Add(-24*10*time.Hour).Unix(), 10)
  76. c.Request = &http.Request{
  77. URL: &url.URL{
  78. RawQuery: "start_timestamp=" + oldStartStr,
  79. },
  80. }
  81. start, end := utils.ParseTimeRange(c, time.Hour*24*7) // Max 7 days
  82. // End should be effectively Now() (since not provided)
  83. // Start should be End - 7 days
  84. expectedStart := end.Add(-time.Hour * 24 * 7)
  85. convey.So(end.Unix(), convey.ShouldAlmostEqual, now.Unix(), 5) // Allow 5s diff
  86. convey.So(start.Unix(), convey.ShouldAlmostEqual, expectedStart.Unix(), 5)
  87. })
  88. convey.Convey("should handle millisecond timestamps", func() {
  89. w := httptest.NewRecorder()
  90. c, _ := gin.CreateTestContext(w)
  91. milliStart := now.Add(-time.Hour).UnixMilli()
  92. milliEnd := now.UnixMilli()
  93. c.Request = &http.Request{
  94. URL: &url.URL{
  95. RawQuery: "start_timestamp=" + strconv.FormatInt(
  96. milliStart,
  97. 10,
  98. ) + "&end_timestamp=" + strconv.FormatInt(
  99. milliEnd,
  100. 10,
  101. ),
  102. },
  103. }
  104. start, end := utils.ParseTimeRange(c, 0)
  105. convey.So(start.Unix(), convey.ShouldEqual, now.Add(-time.Hour).Unix())
  106. convey.So(end.Unix(), convey.ShouldEqual, now.Unix())
  107. })
  108. })
  109. }