webhook_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 TestWebhookParams(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. global_hook_id := ""
  17. global_hook_path := ""
  18. handler := func(ctx *gin.Context, hook_id string, path string) {
  19. global_hook_id = hook_id
  20. global_hook_path = path
  21. }
  22. app_pointer := &App{
  23. webhook_handler: handler,
  24. }
  25. cancel := app_pointer.server(&app.Config{
  26. ServerPort: port,
  27. PluginWebhookEnabled: true,
  28. })
  29. defer cancel()
  30. // test webhook params
  31. client := &http.Client{}
  32. req, err := http.NewRequest("POST", "http://localhost:"+strconv.Itoa(int(port))+"/webhook/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 global_hook_id != "1111" {
  43. t.Errorf("hook id not match: %s", global_hook_id)
  44. return
  45. }
  46. if global_hook_path != "/v1/chat/completions" {
  47. t.Errorf("hook path not match: %s", global_hook_path)
  48. return
  49. }
  50. }