endpoint_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package server
  2. import (
  3. "net/http"
  4. "strconv"
  5. "testing"
  6. "time"
  7. "github.com/gin-gonic/gin"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/network"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  11. )
  12. func TestEndpointParams(t *testing.T) {
  13. port, err := network.GetRandomPort()
  14. if err != nil {
  15. t.Errorf("failed to get random port: %s", err.Error())
  16. return
  17. }
  18. globalHookId := ""
  19. globalHookPath := ""
  20. handler := func(ctx *gin.Context, hook_id string, maxExecutionTime time.Duration, path string) {
  21. globalHookId = hook_id
  22. globalHookPath = path
  23. }
  24. appPointer := &App{
  25. endpointHandler: handler,
  26. }
  27. cancel := appPointer.server(&app.Config{
  28. ServerPort: port,
  29. PluginEndpointEnabled: parser.ToPtr(true),
  30. HealthApiLogEnabled: parser.ToPtr(true),
  31. })
  32. defer cancel()
  33. // test endpoint params
  34. client := &http.Client{}
  35. req, err := http.NewRequest("POST", "http://localhost:"+strconv.Itoa(int(port))+"/e/1111/v1/chat/completions", nil)
  36. if err != nil {
  37. t.Errorf("failed to create request: %s", err.Error())
  38. return
  39. }
  40. _, err = client.Do(req)
  41. if err != nil {
  42. t.Errorf("failed to send request: %s", err.Error())
  43. return
  44. }
  45. if globalHookId != "1111" {
  46. t.Errorf("hook id not match: %s", globalHookId)
  47. return
  48. }
  49. if globalHookPath != "/v1/chat/completions" {
  50. t.Errorf("hook path not match: %s", globalHookPath)
  51. return
  52. }
  53. }