endpoint_test.go 1.3 KB

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