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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package network_test
  2. import (
  3. "context"
  4. "net"
  5. "net/http"
  6. "net/http/httptest"
  7. "net/url"
  8. "strings"
  9. "time"
  10. "github.com/9seconds/mtg/v2/mtglib/network"
  11. socks5 "github.com/armon/go-socks5"
  12. "github.com/mccutchen/go-httpbin/httpbin"
  13. "github.com/stretchr/testify/mock"
  14. )
  15. type ConnMock struct {
  16. mock.Mock
  17. }
  18. func (c *ConnMock) Read(b []byte) (int, error) {
  19. args := c.Called(b)
  20. return args.Int(0), args.Error(1)
  21. }
  22. func (c *ConnMock) Write(b []byte) (int, error) {
  23. args := c.Called(b)
  24. return args.Int(0), args.Error(1)
  25. }
  26. func (c *ConnMock) Close() error {
  27. return c.Called().Error(0)
  28. }
  29. func (c *ConnMock) LocalAddr() net.Addr {
  30. return c.Called().Get(0).(net.Addr)
  31. }
  32. func (c *ConnMock) RemoteAddr() net.Addr {
  33. return c.Called().Get(0).(net.Addr)
  34. }
  35. func (c *ConnMock) SetDeadline(t time.Time) error {
  36. return c.Called(t).Error(0)
  37. }
  38. func (c *ConnMock) SetReadDeadline(t time.Time) error {
  39. return c.Called(t).Error(0)
  40. }
  41. func (c *ConnMock) SetWriteDeadline(t time.Time) error {
  42. return c.Called(t).Error(0)
  43. }
  44. type DialerMock struct {
  45. mock.Mock
  46. }
  47. func (d *DialerMock) Dial(network, address string) (net.Conn, error) {
  48. args := d.Called(network, address)
  49. return args.Get(0).(net.Conn), args.Error(1)
  50. }
  51. func (d *DialerMock) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
  52. args := d.Called(ctx, network, address)
  53. return args.Get(0).(net.Conn), args.Error(1)
  54. }
  55. type HTTPServerTestSuite struct {
  56. httpServer *httptest.Server
  57. }
  58. func (suite *HTTPServerTestSuite) SetupSuite() {
  59. suite.httpServer = httptest.NewServer(httpbin.NewHTTPBin().Handler())
  60. }
  61. func (suite *HTTPServerTestSuite) TearDownSuite() {
  62. suite.httpServer.Close()
  63. }
  64. func (suite *HTTPServerTestSuite) HTTPServerAddress() string {
  65. return strings.TrimPrefix(suite.httpServer.URL, "http://")
  66. }
  67. func (suite *HTTPServerTestSuite) MakeURL(path string) string {
  68. return suite.httpServer.URL + path
  69. }
  70. func (suite *HTTPServerTestSuite) MakeHTTPClient(dialer network.Dialer) *http.Client {
  71. return &http.Client{
  72. Transport: &http.Transport{
  73. DialContext: dialer.DialContext,
  74. },
  75. }
  76. }
  77. type Socks5ServerTestSuite struct {
  78. socks5Listener net.Listener
  79. socks5Server *socks5.Server
  80. }
  81. func (suite *Socks5ServerTestSuite) SetupSuite() {
  82. suite.socks5Listener, _ = net.Listen("tcp", "127.0.0.1:0")
  83. suite.socks5Server, _ = socks5.New(&socks5.Config{
  84. Credentials: socks5.StaticCredentials{
  85. "user": "password",
  86. },
  87. })
  88. go suite.socks5Server.Serve(suite.socks5Listener)
  89. }
  90. func (suite *Socks5ServerTestSuite) TearDownSuite() {
  91. suite.socks5Listener.Close()
  92. }
  93. func (suite *Socks5ServerTestSuite) MakeSocks5URL(user, password string) *url.URL {
  94. return &url.URL{
  95. Scheme: "socks5",
  96. User: url.UserPassword(user, password),
  97. Host: suite.socks5Listener.Addr().String(),
  98. }
  99. }