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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package cli_test
  2. import (
  3. "bytes"
  4. "context"
  5. "io"
  6. "net"
  7. "net/http"
  8. "os"
  9. "strings"
  10. "time"
  11. "github.com/9seconds/mtg/v2/cli"
  12. "github.com/9seconds/mtg/v2/mtglib/network"
  13. "github.com/jarcoal/httpmock"
  14. "github.com/stretchr/testify/mock"
  15. "github.com/stretchr/testify/suite"
  16. )
  17. type NetworkMock struct {
  18. mock.Mock
  19. }
  20. func (n *NetworkMock) Dial(network, address string) (net.Conn, error) {
  21. args := n.Called(network, address)
  22. return args.Get(0).(net.Conn), args.Error(1)
  23. }
  24. func (n *NetworkMock) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
  25. args := n.Called(ctx, network, address)
  26. return args.Get(0).(net.Conn), args.Error(1)
  27. }
  28. func (n *NetworkMock) DNSResolve(network, hostname string) ([]string, error) {
  29. args := n.Called(network, hostname)
  30. return args.Get(0).([]string), args.Error(1)
  31. }
  32. func (n *NetworkMock) MakeHTTPClient(dialFunc network.DialFunc) *http.Client {
  33. return n.Called(dialFunc).Get(0).(*http.Client)
  34. }
  35. func (n *NetworkMock) IdleTimeout() time.Duration {
  36. return n.Called().Get(0).(time.Duration)
  37. }
  38. func (n *NetworkMock) HTTPTimeout() time.Duration {
  39. return n.Called().Get(0).(time.Duration)
  40. }
  41. type CommonTestSuite struct {
  42. suite.Suite
  43. cli *cli.CLI
  44. networkMock *NetworkMock
  45. httpClient *http.Client
  46. }
  47. func (suite *CommonTestSuite) SetupTest() {
  48. suite.networkMock = &NetworkMock{}
  49. suite.httpClient = &http.Client{}
  50. suite.cli = &cli.CLI{}
  51. httpmock.ActivateNonDefault(suite.httpClient)
  52. suite.networkMock.
  53. On("MakeHTTPClient", mock.Anything).
  54. Maybe().
  55. Return(suite.httpClient)
  56. }
  57. func (suite *CommonTestSuite) TearDownTest() {
  58. suite.networkMock.AssertExpectations(suite.T())
  59. httpmock.DeactivateAndReset()
  60. }
  61. func (suite *CommonTestSuite) CaptureStdout(callback func()) string {
  62. return suite.captureOutput(&os.Stdout, callback)
  63. }
  64. func (suite *CommonTestSuite) CaptureStderr(callback func()) string {
  65. return suite.captureOutput(&os.Stderr, callback)
  66. }
  67. func (suite *CommonTestSuite) captureOutput(filefp **os.File, callback func()) string {
  68. oldFp := *filefp
  69. defer func() {
  70. *filefp = oldFp
  71. }()
  72. reader, writer, _ := os.Pipe()
  73. buf := &bytes.Buffer{}
  74. closeChan := make(chan bool)
  75. go func() {
  76. io.Copy(buf, reader) // nolint: errcheck
  77. close(closeChan)
  78. }()
  79. *filefp = writer
  80. callback()
  81. writer.Close()
  82. <-closeChan
  83. return strings.TrimSpace(buf.String())
  84. }