endpoint_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. })
  31. defer cancel()
  32. // test endpoint params
  33. client := &http.Client{}
  34. req, err := http.NewRequest("POST", "http://localhost:"+strconv.Itoa(int(port))+"/e/1111/v1/chat/completions", nil)
  35. if err != nil {
  36. t.Errorf("failed to create request: %s", err.Error())
  37. return
  38. }
  39. _, err = client.Do(req)
  40. if err != nil {
  41. t.Errorf("failed to send request: %s", err.Error())
  42. return
  43. }
  44. if globalHookId != "1111" {
  45. t.Errorf("hook id not match: %s", globalHookId)
  46. return
  47. }
  48. if globalHookPath != "/v1/chat/completions" {
  49. t.Errorf("hook path not match: %s", globalHookPath)
  50. return
  51. }
  52. }