utils_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package utils_test
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "io"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. "time"
  11. "github.com/labring/aiproxy/core/relay/utils"
  12. "github.com/smartystreets/goconvey/convey"
  13. )
  14. func TestIsStreamResponseWithHeader(t *testing.T) {
  15. convey.Convey("IsStreamResponseWithHeader", t, func() {
  16. convey.Convey("should return true for text/event-stream", func() {
  17. header := http.Header{}
  18. header.Set("Content-Type", "text/event-stream")
  19. convey.So(utils.IsStreamResponseWithHeader(header), convey.ShouldBeTrue)
  20. })
  21. convey.Convey("should return true for application/x-ndjson", func() {
  22. header := http.Header{}
  23. header.Set("Content-Type", "application/x-ndjson")
  24. convey.So(utils.IsStreamResponseWithHeader(header), convey.ShouldBeTrue)
  25. })
  26. convey.Convey("should return false for application/json", func() {
  27. header := http.Header{}
  28. header.Set("Content-Type", "application/json")
  29. convey.So(utils.IsStreamResponseWithHeader(header), convey.ShouldBeFalse)
  30. })
  31. convey.Convey("should return false for empty content type", func() {
  32. header := http.Header{}
  33. convey.So(utils.IsStreamResponseWithHeader(header), convey.ShouldBeFalse)
  34. })
  35. })
  36. }
  37. func TestScannerBuffer(t *testing.T) {
  38. convey.Convey("ScannerBuffer", t, func() {
  39. convey.Convey("should get buffer of correct size", func() {
  40. buf := utils.GetScannerBuffer()
  41. convey.So(len(*buf), convey.ShouldEqual, utils.ScannerBufferSize)
  42. convey.So(cap(*buf), convey.ShouldEqual, utils.ScannerBufferSize)
  43. utils.PutScannerBuffer(buf)
  44. })
  45. })
  46. }
  47. func TestDoRequest(t *testing.T) {
  48. convey.Convey("DoRequest", t, func() {
  49. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  50. w.WriteHeader(http.StatusOK)
  51. _, _ = w.Write([]byte("ok"))
  52. }))
  53. defer ts.Close()
  54. convey.Convey("should make request successfully", func() {
  55. req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, ts.URL, nil)
  56. resp, err := utils.DoRequest(req, time.Second)
  57. convey.So(err, convey.ShouldBeNil)
  58. defer resp.Body.Close()
  59. convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
  60. body, _ := io.ReadAll(resp.Body)
  61. convey.So(string(body), convey.ShouldEqual, "ok")
  62. })
  63. })
  64. }
  65. func TestUnmarshalGeneralOpenAIRequest(t *testing.T) {
  66. convey.Convey("UnmarshalGeneralOpenAIRequest", t, func() {
  67. convey.Convey("should unmarshal valid request", func() {
  68. reqBody := map[string]any{
  69. "model": "gpt-3.5-turbo",
  70. "messages": []map[string]string{
  71. {"role": "user", "content": "hello"},
  72. },
  73. "stream": true,
  74. }
  75. bodyBytes, _ := json.Marshal(reqBody)
  76. req, _ := http.NewRequestWithContext(
  77. context.Background(),
  78. http.MethodPost,
  79. "/",
  80. bytes.NewBuffer(bodyBytes),
  81. )
  82. req.Header.Set("Content-Type", "application/json")
  83. parsedReq, err := utils.UnmarshalGeneralOpenAIRequest(req)
  84. convey.So(err, convey.ShouldBeNil)
  85. convey.So(parsedReq.Model, convey.ShouldEqual, "gpt-3.5-turbo")
  86. convey.So(parsedReq.Stream, convey.ShouldBeTrue)
  87. convey.So(len(parsedReq.Messages), convey.ShouldEqual, 1)
  88. convey.So(parsedReq.Messages[0].Role, convey.ShouldEqual, "user")
  89. })
  90. })
  91. }