Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

public_config_updater_test.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package dc
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "sync"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/require"
  9. "github.com/stretchr/testify/suite"
  10. )
  11. type PublicConfigUpdaterTestSuite struct {
  12. UpdaterTestSuiteBase
  13. u *PublicConfigUpdater
  14. lock sync.Mutex
  15. srv *httptest.Server
  16. responseHandler func(w http.ResponseWriter)
  17. }
  18. func (s *PublicConfigUpdaterTestSuite) SetupSuite() {
  19. s.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  20. s.lock.Lock()
  21. s.responseHandler(w)
  22. s.lock.Unlock()
  23. }))
  24. }
  25. func (s *PublicConfigUpdaterTestSuite) TearDownSuite() {
  26. s.srv.Close()
  27. }
  28. func (s *PublicConfigUpdaterTestSuite) SetupTest() {
  29. s.UpdaterTestSuiteBase.SetupTest()
  30. tg, err := New("prefer-ipv4")
  31. require.NoError(s.T(), err)
  32. s.u = NewPublicConfigUpdater(tg, s.loggerMock, s.srv.Client())
  33. }
  34. func (s *PublicConfigUpdaterTestSuite) Test502StatusCode() {
  35. s.responseHandler = func(w http.ResponseWriter) {
  36. w.WriteHeader(http.StatusBadGateway)
  37. }
  38. s.u.Run(s.ctx, s.srv.URL, "tcp4")
  39. time.Sleep(100 * time.Millisecond)
  40. s.ctxCancel()
  41. s.u.Wait()
  42. s.Len(s.u.tg.view.publicConfigs.v4, 0)
  43. }
  44. func (s *PublicConfigUpdaterTestSuite) TestEmptyFile() {
  45. s.responseHandler = func(w http.ResponseWriter) {
  46. w.WriteHeader(http.StatusOK)
  47. }
  48. s.u.Run(s.ctx, s.srv.URL, "tcp4")
  49. time.Sleep(100 * time.Millisecond)
  50. s.ctxCancel()
  51. s.u.Wait()
  52. s.Len(s.u.tg.view.publicConfigs.v4, 0)
  53. }
  54. func (s *PublicConfigUpdaterTestSuite) TestGarbage() {
  55. result := `
  56. proxy_for -1 -1;
  57. proxy_for 100 100.10.0.0:3333;
  58. lala 0 0
  59. `
  60. s.responseHandler = func(w http.ResponseWriter) {
  61. w.WriteHeader(http.StatusOK)
  62. w.Write([]byte(result)) //nolint: errcheck
  63. }
  64. s.u.Run(s.ctx, s.srv.URL, "tcp4")
  65. time.Sleep(100 * time.Millisecond)
  66. s.ctxCancel()
  67. s.u.Wait()
  68. s.Len(s.u.tg.view.publicConfigs.v4, 0)
  69. }
  70. func (s *PublicConfigUpdaterTestSuite) TestOk() {
  71. result := `
  72. proxy_for 203 100.10.0.0:3333;
  73. proxy_for -100 101.10.0.0:3333;
  74. `
  75. s.responseHandler = func(w http.ResponseWriter) {
  76. w.WriteHeader(http.StatusOK)
  77. w.Write([]byte(result)) //nolint: errcheck
  78. }
  79. s.u.Run(s.ctx, s.srv.URL, "tcp4")
  80. time.Sleep(100 * time.Millisecond)
  81. s.ctxCancel()
  82. s.u.Wait()
  83. s.Len(s.u.tg.view.publicConfigs.v4, 1)
  84. s.Len(s.u.tg.view.publicConfigs.v4[203], 1)
  85. s.Equal("100.10.0.0:3333", s.u.tg.view.publicConfigs.v4[203][0].Address)
  86. }
  87. func TestPublicConfigUpdater(t *testing.T) {
  88. suite.Run(t, &PublicConfigUpdaterTestSuite{})
  89. }