mock_hand_sub_http_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package mock_hand_sub_http
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/gofiber/fiber/v2"
  6. "gitp78su.ipnodns.ru/svi/kern/v2/krn/kctx"
  7. "gitp78su.ipnodns.ru/svi/kern/v2/krn/kserv_http"
  8. . "gitp78su.ipnodns.ru/svi/kern/v2/krn/ktypes"
  9. "gitp78su.ipnodns.ru/svi/kern/v2/mock/mock_env"
  10. )
  11. type tester struct {
  12. t *testing.T
  13. ctx IKernelCtx
  14. isBad bool // Признак испорченности обработчика
  15. hand *MockHandSubHttp
  16. }
  17. func TestHandlerHttpSub(t *testing.T) {
  18. sf := &tester{
  19. t: t,
  20. ctx: kctx.GetKernelCtx(),
  21. }
  22. sf.ctx.Set("monolitName", "test_monolit", "comment")
  23. sf.new()
  24. sf.back()
  25. sf.ctx.Cancel()
  26. sf.ctx.Wg().Wait()
  27. }
  28. // Проверка работы обратного вызова
  29. func (sf *tester) back() {
  30. sf.t.Log("back")
  31. sf.backBad1()
  32. sf.backBad2()
  33. sf.backGood1()
  34. }
  35. func (sf *tester) backGood1() {
  36. sf.t.Log("backGood1")
  37. sf.hand.FnBack([]byte("test_msg"))
  38. }
  39. // Что-то случилось на той стороне
  40. func (sf *tester) backBad2() {
  41. sf.t.Log("backBad2")
  42. _ = mock_env.MakeEnv()
  43. kernServ := kserv_http.GetKernelServHttp()
  44. fiberApp := kernServ.Fiber()
  45. sf.hand.WebHook_ = "http://localhost:18200/test/local"
  46. fiberApp.Post("/test/local", sf.testLocal)
  47. go kernServ.Run()
  48. sf.isBad = true
  49. sf.hand.FnBack([]byte("test_msg"))
  50. sf.isBad = false
  51. }
  52. // Эндпоинт на HTTP-сервере
  53. func (sf *tester) testLocal(ctx *fiber.Ctx) error {
  54. if sf.isBad {
  55. return ctx.SendStatus(400)
  56. }
  57. return ctx.SendString("ok")
  58. }
  59. // Нет обработчика для запроса
  60. func (sf *tester) backBad1() {
  61. sf.t.Log("backBad1")
  62. sf.hand.FnBack([]byte("hello_test"))
  63. }
  64. // Создание HTTP-обработчика подписки
  65. func (sf *tester) new() {
  66. sf.t.Log("new")
  67. sf.newBad1()
  68. sf.newGood1()
  69. }
  70. func (sf *tester) newGood1() {
  71. sf.t.Log("newGood1")
  72. defer func() {
  73. if _panic := recover(); _panic != nil {
  74. sf.t.Fatalf("newGood1(): panic=%v", _panic)
  75. }
  76. }()
  77. sf.hand = NewMockHandSubHttp("test_topic", "/test/local").(*MockHandSubHttp)
  78. if sf.hand == nil {
  79. sf.t.Fatalf("newGood1(): handler==nil")
  80. }
  81. if name := sf.hand.Name(); !strings.Contains(string(name), "/test/local_") {
  82. sf.t.Fatalf("newGood1(): name(%v)!='/test/local_'", name)
  83. }
  84. sf.hand.SetName("test_name")
  85. if name := sf.hand.Name(); name != "test_name" {
  86. sf.t.Fatalf("newGood1(): name(%v)!='test_name'", name)
  87. }
  88. if topic := sf.hand.Topic(); topic != "test_topic" {
  89. sf.t.Fatalf("newGood1(): bad topic(%v) 'test_topic'", topic)
  90. }
  91. if msg := sf.hand.Msg(); msg != "" {
  92. sf.t.Fatalf("newGood1(): msg(%v) not empty", msg)
  93. }
  94. }
  95. // Нет нужных полей
  96. func (sf *tester) newBad1() {
  97. sf.t.Log("newBad1")
  98. defer func() {
  99. if _panic := recover(); _panic == nil {
  100. sf.t.Fatalf("newBad1(): panic==nil")
  101. }
  102. }()
  103. _ = NewMockHandSubHttp("", "/test/local")
  104. }