endpoint_test.go 1.3 KB

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