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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

init_test.go 2.3KB

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