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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

init_test.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package doppel
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "net"
  6. "net/http"
  7. "net/http/httptest"
  8. "time"
  9. "github.com/9seconds/mtg/v2/essentials"
  10. "github.com/stretchr/testify/mock"
  11. "github.com/stretchr/testify/suite"
  12. )
  13. type SimpleNetwork struct {
  14. }
  15. func (s SimpleNetwork) Dial(network, address string) (essentials.Conn, error) {
  16. return s.DialContext(context.Background(), network, address)
  17. }
  18. func (s SimpleNetwork) DialContext(ctx context.Context, network, address string) (essentials.Conn, error) {
  19. d := &net.Dialer{}
  20. conn, err := d.DialContext(ctx, network, address)
  21. if err != nil {
  22. return nil, err
  23. }
  24. return conn.(*net.TCPConn), nil
  25. }
  26. func (s SimpleNetwork) MakeHTTPClient(dialFunc func(ctx context.Context, network, address string) (essentials.Conn, error)) *http.Client {
  27. if dialFunc == nil {
  28. dialFunc = s.DialContext
  29. }
  30. return &http.Client{
  31. Transport: &http.Transport{
  32. TLSClientConfig: &tls.Config{
  33. InsecureSkipVerify: true, //nolint: gosec
  34. },
  35. DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
  36. return dialFunc(ctx, network, address)
  37. },
  38. },
  39. }
  40. }
  41. type TLSServerTestSuite struct {
  42. suite.Suite
  43. tlsServer *httptest.Server
  44. ctx context.Context
  45. ctxCancel context.CancelFunc
  46. network SimpleNetwork
  47. urls []string
  48. }
  49. func (suite *TLSServerTestSuite) SetupSuite() {
  50. suite.tlsServer = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  51. w.WriteHeader(http.StatusOK)
  52. w.Header().Add("Hello", "how long")
  53. if _, err := w.Write([]byte{1, 2, 3}); err != nil {
  54. panic(err)
  55. }
  56. time.Sleep(5 * time.Millisecond)
  57. if _, err := w.Write([]byte{1, 2, 3}); err != nil {
  58. panic(err)
  59. }
  60. }))
  61. suite.urls = []string{suite.tlsServer.URL}
  62. }
  63. func (suite *TLSServerTestSuite) SetupTest() {
  64. ctx, cancel := context.WithCancel(context.Background())
  65. suite.ctx = ctx
  66. suite.ctxCancel = cancel
  67. }
  68. func (suite *TLSServerTestSuite) TearDownTest() {
  69. suite.ctxCancel()
  70. suite.tlsServer.CloseClientConnections()
  71. }
  72. func (suite *TLSServerTestSuite) TearDownSuite() {
  73. suite.tlsServer.Close()
  74. }
  75. type LoggerMock struct {
  76. mock.Mock
  77. }
  78. func (l *LoggerMock) Info(msg string) {
  79. l.Called(msg)
  80. }
  81. func (l *LoggerMock) WarningError(msg string, err error) {
  82. l.Called(msg, err)
  83. }