webhook_test.go 1.2 KB

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