Ver código fonte

Refactor network to a top-level module

tags/v2.0.0-rc1
9seconds 5 anos atrás
pai
commit
f8ad90c845

+ 5
- 5
cli/access_test.go Ver arquivo

@@ -7,6 +7,7 @@ import (
7 7
 
8 8
 	"github.com/9seconds/mtg/v2/config"
9 9
 	"github.com/9seconds/mtg/v2/mtglib"
10
+	"github.com/9seconds/mtg/v2/testlib"
10 11
 	"github.com/jarcoal/httpmock"
11 12
 	"github.com/stretchr/testify/suite"
12 13
 	"github.com/xeipuuv/gojsonschema"
@@ -126,7 +127,7 @@ func (suite *AccessTestSuite) TestGenerateNoCalls() {
126 127
 	suite.cli.Access.PublicIPv4 = net.ParseIP("10.0.0.10")
127 128
 	suite.cli.Access.PublicIPv6 = net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
128 129
 
129
-	output := suite.CaptureStdout(func() {
130
+	output := testlib.CaptureStdout(func() {
130 131
 		suite.NoError(suite.cli.Access.Execute(suite.cli))
131 132
 	})
132 133
 
@@ -150,7 +151,7 @@ func (suite *AccessTestSuite) TestGenerateIPv4Call() {
150 151
 	httpmock.RegisterResponder(http.MethodGet, "https://ifconfig.co",
151 152
 		httpmock.NewStringResponder(http.StatusOK, "10.11.12.13"))
152 153
 
153
-	output := suite.CaptureStdout(func() {
154
+	output := testlib.CaptureStdout(func() {
154 155
 		suite.NoError(suite.cli.Access.Execute(suite.cli))
155 156
 	})
156 157
 
@@ -174,7 +175,7 @@ func (suite *AccessTestSuite) TestIPv4CallFail() {
174 175
 	httpmock.RegisterResponder(http.MethodGet, "https://ifconfig.co",
175 176
 		httpmock.NewStringResponder(http.StatusForbidden, ""))
176 177
 
177
-	output := suite.CaptureStdout(func() {
178
+	output := testlib.CaptureStdout(func() {
178 179
 		suite.NoError(suite.cli.Access.Execute(suite.cli))
179 180
 	})
180 181
 
@@ -191,7 +192,6 @@ func (suite *AccessTestSuite) TestIPv4CallFail() {
191 192
 	suite.Contains(output, suite.cli.Access.Config.Secret.Hex())
192 193
 }
193 194
 
194
-func TestAccess(t *testing.T) {
195
-	t.Parallel()
195
+func TestAccess(t *testing.T) { // nolint: paralleltest
196 196
 	suite.Run(t, &AccessTestSuite{})
197 197
 }

+ 4
- 3
cli/base.go Ver arquivo

@@ -7,11 +7,12 @@ import (
7 7
 	"net/url"
8 8
 
9 9
 	"github.com/9seconds/mtg/v2/config"
10
-	"github.com/9seconds/mtg/v2/mtglib/network"
10
+	"github.com/9seconds/mtg/v2/mtglib"
11
+	"github.com/9seconds/mtg/v2/network"
11 12
 )
12 13
 
13 14
 type base struct {
14
-	Network network.Network
15
+	Network mtglib.Network
15 16
 	Config  *config.Config
16 17
 }
17 18
 
@@ -37,7 +38,7 @@ func (b *base) ReadConfig(path, version string) error {
37 38
 	return nil
38 39
 }
39 40
 
40
-func (b *base) makeNetwork(conf *config.Config, version string) (network.Network, error) {
41
+func (b *base) makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
41 42
 	tcpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultTimeout)
42 43
 	idleTimeout := conf.Network.Timeout.Idle.Value(network.DefaultIdleTimeout)
43 44
 	httpTimeout := conf.Network.Timeout.HTTP.Value(network.DefaultHTTPTimeout)

+ 3
- 2
cli/generate_secret_test.go Ver arquivo

@@ -5,6 +5,7 @@ import (
5 5
 	"testing"
6 6
 
7 7
 	"github.com/9seconds/mtg/v2/mtglib"
8
+	"github.com/9seconds/mtg/v2/testlib"
8 9
 	"github.com/stretchr/testify/suite"
9 10
 )
10 11
 
@@ -19,7 +20,7 @@ func (suite *GenerateSecretTestSuite) SetupTest() {
19 20
 }
20 21
 
21 22
 func (suite *GenerateSecretTestSuite) TestDefault() {
22
-	output := suite.CaptureStdout(func() {
23
+	output := testlib.CaptureStdout(func() {
23 24
 		suite.NoError(suite.cli.GenerateSecret.Run(suite.cli, "dev"))
24 25
 	})
25 26
 	suite.True(strings.HasPrefix(output, "7"))
@@ -33,7 +34,7 @@ func (suite *GenerateSecretTestSuite) TestDefault() {
33 34
 func (suite *GenerateSecretTestSuite) TestHex() {
34 35
 	suite.cli.GenerateSecret.Hex = true
35 36
 
36
-	output := suite.CaptureStdout(func() {
37
+	output := testlib.CaptureStdout(func() {
37 38
 		suite.NoError(suite.cli.GenerateSecret.Run(suite.cli, "dev"))
38 39
 	})
39 40
 	suite.True(strings.HasPrefix(output, "ee"))

+ 3
- 78
cli/init_test.go Ver arquivo

@@ -1,66 +1,25 @@
1 1
 package cli_test
2 2
 
3 3
 import (
4
-	"bytes"
5
-	"context"
6
-	"io"
7
-	"net"
8 4
 	"net/http"
9
-	"os"
10
-	"strings"
11
-	"time"
12 5
 
13 6
 	"github.com/9seconds/mtg/v2/cli"
14
-	"github.com/9seconds/mtg/v2/mtglib/network"
7
+	"github.com/9seconds/mtg/v2/testlib"
15 8
 	"github.com/jarcoal/httpmock"
16 9
 	"github.com/stretchr/testify/mock"
17 10
 	"github.com/stretchr/testify/suite"
18 11
 )
19 12
 
20
-type NetworkMock struct {
21
-	mock.Mock
22
-}
23
-
24
-func (n *NetworkMock) Dial(network, address string) (net.Conn, error) {
25
-	args := n.Called(network, address)
26
-
27
-	return args.Get(0).(net.Conn), args.Error(1)
28
-}
29
-
30
-func (n *NetworkMock) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
31
-	args := n.Called(ctx, network, address)
32
-
33
-	return args.Get(0).(net.Conn), args.Error(1)
34
-}
35
-
36
-func (n *NetworkMock) DNSResolve(network, hostname string) ([]string, error) {
37
-	args := n.Called(network, hostname)
38
-
39
-	return args.Get(0).([]string), args.Error(1)
40
-}
41
-
42
-func (n *NetworkMock) MakeHTTPClient(dialFunc network.DialFunc) *http.Client {
43
-	return n.Called(dialFunc).Get(0).(*http.Client)
44
-}
45
-
46
-func (n *NetworkMock) IdleTimeout() time.Duration {
47
-	return n.Called().Get(0).(time.Duration)
48
-}
49
-
50
-func (n *NetworkMock) HTTPTimeout() time.Duration {
51
-	return n.Called().Get(0).(time.Duration)
52
-}
53
-
54 13
 type CommonTestSuite struct {
55 14
 	suite.Suite
56 15
 
57 16
 	cli         *cli.CLI
58
-	networkMock *NetworkMock
17
+	networkMock *testlib.NetworkMock
59 18
 	httpClient  *http.Client
60 19
 }
61 20
 
62 21
 func (suite *CommonTestSuite) SetupTest() {
63
-	suite.networkMock = &NetworkMock{}
22
+	suite.networkMock = &testlib.NetworkMock{}
64 23
 	suite.httpClient = &http.Client{}
65 24
 	suite.cli = &cli.CLI{}
66 25
 
@@ -76,37 +35,3 @@ func (suite *CommonTestSuite) TearDownTest() {
76 35
 	suite.networkMock.AssertExpectations(suite.T())
77 36
 	httpmock.DeactivateAndReset()
78 37
 }
79
-
80
-func (suite *CommonTestSuite) CaptureStdout(callback func()) string {
81
-	return suite.captureOutput(&os.Stdout, callback)
82
-}
83
-
84
-func (suite *CommonTestSuite) CaptureStderr(callback func()) string {
85
-	return suite.captureOutput(&os.Stderr, callback)
86
-}
87
-
88
-func (suite *CommonTestSuite) captureOutput(filefp **os.File, callback func()) string {
89
-	oldFp := *filefp
90
-
91
-	defer func() {
92
-		*filefp = oldFp
93
-	}()
94
-
95
-	reader, writer, _ := os.Pipe()
96
-	buf := &bytes.Buffer{}
97
-	closeChan := make(chan bool)
98
-
99
-	go func() {
100
-		io.Copy(buf, reader) // nolint: errcheck
101
-		close(closeChan)
102
-	}()
103
-
104
-	*filefp = writer
105
-
106
-	callback()
107
-
108
-	writer.Close()
109
-	<-closeChan
110
-
111
-	return strings.TrimSpace(buf.String())
112
-}

+ 1
- 0
go.mod Ver arquivo

@@ -12,6 +12,7 @@ require (
12 12
 	github.com/libp2p/go-reuseport v0.0.2
13 13
 	github.com/mccutchen/go-httpbin v1.1.1
14 14
 	github.com/pelletier/go-toml v1.8.1
15
+	github.com/rs/zerolog v1.20.0 // indirect
15 16
 	github.com/stretchr/objx v0.3.0 // indirect
16 17
 	github.com/stretchr/testify v1.7.0
17 18
 	github.com/xeipuuv/gojsonschema v1.2.0

+ 11
- 0
go.sum Ver arquivo

@@ -6,6 +6,7 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd
6 6
 github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
7 7
 github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6 h1:4NNbNM2Iq/k57qEu7WfL67UrbPq1uFWxW4qODCohi+0=
8 8
 github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6/go.mod h1:J29hk+f9lJrblVIfiJOtTFk+OblBawmib4uz/VdKzlg=
9
+github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
9 10
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10 11
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
11 12
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -27,6 +28,9 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
27 28
 github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
28 29
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
29 30
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
31
+github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
32
+github.com/rs/zerolog v1.20.0 h1:38k9hgtUBdxFwE34yS8rTHmHBa4eN16E4DJlv177LNs=
33
+github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo=
30 34
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
31 35
 github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
32 36
 github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
@@ -42,15 +46,22 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
42 46
 github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
43 47
 github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
44 48
 github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
49
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
50
+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
45 51
 golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
46 52
 golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
53
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
54
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
47 55
 golang.org/x/sys v0.0.0-20190228124157-a34e9553db1e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
48 56
 golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
49 57
 golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 h1:46ULzRKLh1CwgRq2dC5SlBzEqqNCi8rreOZnNrbqcIY=
50 58
 golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
51 59
 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
60
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
52 61
 golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
53 62
 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
63
+golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
64
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
54 65
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
55 66
 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
56 67
 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

+ 28
- 1
mtglib/init.go Ver arquivo

@@ -1,5 +1,32 @@
1 1
 package mtglib
2 2
 
3
-import "errors"
3
+import (
4
+	"context"
5
+	"errors"
6
+	"net"
7
+	"net/http"
8
+	"time"
9
+)
4 10
 
5 11
 var ErrSecretEmpty = errors.New("secret is empty")
12
+
13
+type Network interface {
14
+	Dial(network, address string) (net.Conn, error)
15
+	DialContext(ctx context.Context, network, address string) (net.Conn, error)
16
+	MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error)) *http.Client
17
+	IdleTimeout() time.Duration
18
+}
19
+
20
+type Logger interface {
21
+	Named(name string) Logger
22
+
23
+	BindInt(name string, value int) Logger
24
+	BindStr(name, value string) Logger
25
+
26
+	Info(msg string)
27
+	InfoError(msg string, err error)
28
+	Warning(msg string)
29
+	WarningError(msg string, err error)
30
+	Debug(msg string)
31
+	DebugError(msg string, err error)
32
+}

+ 0
- 65
mtglib/network/init_internal_test.go Ver arquivo

@@ -1,65 +0,0 @@
1
-package network
2
-
3
-import (
4
-	"context"
5
-	"net"
6
-	"time"
7
-
8
-	"github.com/stretchr/testify/mock"
9
-)
10
-
11
-type ConnMock struct {
12
-	mock.Mock
13
-}
14
-
15
-func (c *ConnMock) Read(b []byte) (int, error) {
16
-	args := c.Called(b)
17
-
18
-	return args.Int(0), args.Error(1)
19
-}
20
-
21
-func (c *ConnMock) Write(b []byte) (int, error) {
22
-	args := c.Called(b)
23
-
24
-	return args.Int(0), args.Error(1)
25
-}
26
-
27
-func (c *ConnMock) Close() error {
28
-	return c.Called().Error(0)
29
-}
30
-
31
-func (c *ConnMock) LocalAddr() net.Addr {
32
-	return c.Called().Get(0).(net.Addr)
33
-}
34
-
35
-func (c *ConnMock) RemoteAddr() net.Addr {
36
-	return c.Called().Get(0).(net.Addr)
37
-}
38
-
39
-func (c *ConnMock) SetDeadline(t time.Time) error {
40
-	return c.Called(t).Error(0)
41
-}
42
-
43
-func (c *ConnMock) SetReadDeadline(t time.Time) error {
44
-	return c.Called(t).Error(0)
45
-}
46
-
47
-func (c *ConnMock) SetWriteDeadline(t time.Time) error {
48
-	return c.Called(t).Error(0)
49
-}
50
-
51
-type DialerMock struct {
52
-	mock.Mock
53
-}
54
-
55
-func (d *DialerMock) Dial(network, address string) (net.Conn, error) {
56
-	args := d.Called(network, address)
57
-
58
-	return args.Get(0).(net.Conn), args.Error(1)
59
-}
60
-
61
-func (d *DialerMock) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
62
-	args := d.Called(ctx, network, address)
63
-
64
-	return args.Get(0).(net.Conn), args.Error(1)
65
-}

mtglib/network/circuit_breaker.go → network/circuit_breaker.go Ver arquivo


mtglib/network/circuit_breaker_internal_test.go → network/circuit_breaker_internal_test.go Ver arquivo

@@ -9,6 +9,7 @@ import (
9 9
 	"testing"
10 10
 	"time"
11 11
 
12
+	"github.com/9seconds/mtg/v2/testlib"
12 13
 	"github.com/stretchr/testify/mock"
13 14
 	"github.com/stretchr/testify/suite"
14 15
 )
@@ -20,7 +21,7 @@ type CircuitBreakerTestSuite struct {
20 21
 	mutex          sync.Mutex
21 22
 	ctx            context.Context
22 23
 	ctxCancel      context.CancelFunc
23
-	connMock       *ConnMock
24
+	connMock       *testlib.NetConnMock
24 25
 	baseDialerMock *DialerMock
25 26
 }
26 27
 
@@ -28,7 +29,7 @@ func (suite *CircuitBreakerTestSuite) SetupTest() {
28 29
 	suite.mutex = sync.Mutex{}
29 30
 	suite.ctx, suite.ctxCancel = context.WithCancel(context.Background())
30 31
 	suite.baseDialerMock = &DialerMock{}
31
-	suite.connMock = &ConnMock{}
32
+	suite.connMock = &testlib.NetConnMock{}
32 33
 	suite.d = newCircuitBreakerDialer(suite.baseDialerMock,
33 34
 		3, 100*time.Millisecond, 50*time.Millisecond)
34 35
 }

mtglib/network/default.go → network/default.go Ver arquivo


mtglib/network/default_test.go → network/default_test.go Ver arquivo

@@ -5,7 +5,7 @@ import (
5 5
 	"net/http"
6 6
 	"testing"
7 7
 
8
-	"github.com/9seconds/mtg/v2/mtglib/network"
8
+	"github.com/9seconds/mtg/v2/network"
9 9
 	"github.com/stretchr/testify/suite"
10 10
 )
11 11
 

mtglib/network/init.go → network/init.go Ver arquivo

@@ -4,7 +4,6 @@ import (
4 4
 	"context"
5 5
 	"errors"
6 6
 	"net"
7
-	"net/http"
8 7
 	"time"
9 8
 )
10 9
 
@@ -27,18 +26,7 @@ var (
27 26
 	ErrCannotDialWithAllProxies = errors.New("cannot dial with all proxies")
28 27
 )
29 28
 
30
-type DialFunc func(ctx context.Context, protocol, address string) (net.Conn, error)
31
-
32 29
 type Dialer interface {
33 30
 	Dial(network, address string) (net.Conn, error)
34 31
 	DialContext(ctx context.Context, network, address string) (net.Conn, error)
35 32
 }
36
-
37
-type Network interface {
38
-	Dialer
39
-
40
-	DNSResolve(network, hostname string) (ips []string, err error)
41
-	MakeHTTPClient(DialFunc) *http.Client
42
-	IdleTimeout() time.Duration
43
-	HTTPTimeout() time.Duration
44
-}

+ 24
- 0
network/init_internal_test.go Ver arquivo

@@ -0,0 +1,24 @@
1
+package network
2
+
3
+import (
4
+	"context"
5
+	"net"
6
+
7
+	"github.com/stretchr/testify/mock"
8
+)
9
+
10
+type DialerMock struct {
11
+	mock.Mock
12
+}
13
+
14
+func (d *DialerMock) Dial(network, address string) (net.Conn, error) {
15
+	args := d.Called(network, address)
16
+
17
+	return args.Get(0).(net.Conn), args.Error(1)
18
+}
19
+
20
+func (d *DialerMock) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
21
+	args := d.Called(ctx, network, address)
22
+
23
+	return args.Get(0).(net.Conn), args.Error(1)
24
+}

mtglib/network/init_test.go → network/init_test.go Ver arquivo

@@ -8,7 +8,7 @@ import (
8 8
 	"net/url"
9 9
 	"strings"
10 10
 
11
-	"github.com/9seconds/mtg/v2/mtglib/network"
11
+	"github.com/9seconds/mtg/v2/network"
12 12
 	socks5 "github.com/armon/go-socks5"
13 13
 	"github.com/mccutchen/go-httpbin/httpbin"
14 14
 	"github.com/stretchr/testify/mock"

mtglib/network/load_balanced_socks5.go → network/load_balanced_socks5.go Ver arquivo


mtglib/network/load_balanced_socks5_test.go → network/load_balanced_socks5_test.go Ver arquivo

@@ -8,7 +8,7 @@ import (
8 8
 	"net/url"
9 9
 	"testing"
10 10
 
11
-	"github.com/9seconds/mtg/v2/mtglib/network"
11
+	"github.com/9seconds/mtg/v2/network"
12 12
 	"github.com/stretchr/testify/mock"
13 13
 	"github.com/stretchr/testify/suite"
14 14
 )

mtglib/network/network.go → network/network.go Ver arquivo

@@ -9,6 +9,7 @@ import (
9 9
 	"sync"
10 10
 	"time"
11 11
 
12
+	"github.com/9seconds/mtg/v2/mtglib"
12 13
 	doh "github.com/babolivier/go-doh-client"
13 14
 )
14 15
 
@@ -38,7 +39,7 @@ func (n *network) Dial(protocol, address string) (net.Conn, error) {
38 39
 func (n *network) DialContext(ctx context.Context, protocol, address string) (net.Conn, error) {
39 40
 	host, port, _ := net.SplitHostPort(address)
40 41
 
41
-	ips, err := n.DNSResolve(protocol, host)
42
+	ips, err := n.dnsResolve(protocol, host)
42 43
 	if err != nil {
43 44
 		return nil, fmt.Errorf("cannot resolve dns names: %w", err)
44 45
 	}
@@ -61,7 +62,20 @@ func (n *network) DialContext(ctx context.Context, protocol, address string) (ne
61 62
 	return nil, fmt.Errorf("cannot dial to %s:%s: %w", protocol, address, err)
62 63
 }
63 64
 
64
-func (n *network) DNSResolve(protocol, address string) ([]string, error) {
65
+func (n *network) MakeHTTPClient(dialFunc func(ctx context.Context,
66
+	network, address string) (net.Conn, error)) *http.Client {
67
+	if dialFunc == nil {
68
+		dialFunc = n.DialContext
69
+	}
70
+
71
+	return makeHTTPClient(n.userAgent, n.httpTimeout, dialFunc)
72
+}
73
+
74
+func (n *network) IdleTimeout() time.Duration {
75
+	return n.idleTimeout
76
+}
77
+
78
+func (n *network) dnsResolve(protocol, address string) ([]string, error) {
65 79
 	if net.ParseIP(address) != nil {
66 80
 		return []string{address}, nil
67 81
 	}
@@ -115,25 +129,9 @@ func (n *network) DNSResolve(protocol, address string) ([]string, error) {
115 129
 	return ips, nil
116 130
 }
117 131
 
118
-func (n *network) MakeHTTPClient(dialFunc DialFunc) *http.Client {
119
-	if dialFunc == nil {
120
-		dialFunc = n.DialContext
121
-	}
122
-
123
-	return makeHTTPClient(n.userAgent, n.httpTimeout, dialFunc)
124
-}
125
-
126
-func (n *network) IdleTimeout() time.Duration {
127
-	return n.idleTimeout
128
-}
129
-
130
-func (n *network) HTTPTimeout() time.Duration {
131
-	return n.httpTimeout
132
-}
133
-
134 132
 func NewNetwork(dialer Dialer,
135 133
 	userAgent, dohHostname string,
136
-	httpTimeout, idleTimeout time.Duration) (Network, error) {
134
+	httpTimeout, idleTimeout time.Duration) (mtglib.Network, error) {
137 135
 	switch {
138 136
 	case idleTimeout < 0:
139 137
 		return nil, fmt.Errorf("timeout should be positive number %s", idleTimeout)
@@ -158,7 +156,9 @@ func NewNetwork(dialer Dialer,
158 156
 	}, nil
159 157
 }
160 158
 
161
-func makeHTTPClient(userAgent string, timeout time.Duration, dialFunc DialFunc) *http.Client {
159
+func makeHTTPClient(userAgent string,
160
+	timeout time.Duration,
161
+	dialFunc func(ctx context.Context, network, address string) (net.Conn, error)) *http.Client {
162 162
 	return &http.Client{
163 163
 		Timeout: timeout,
164 164
 		Transport: networkHTTPTransport{

mtglib/network/proxy_dialer.go → network/proxy_dialer.go Ver arquivo


mtglib/network/proxy_dialer_internal_test.go → network/proxy_dialer_internal_test.go Ver arquivo


mtglib/network/socks5.go → network/socks5.go Ver arquivo


mtglib/network/socks5_test.go → network/socks5_test.go Ver arquivo

@@ -4,7 +4,7 @@ import (
4 4
 	"net/http"
5 5
 	"testing"
6 6
 
7
-	"github.com/9seconds/mtg/v2/mtglib/network"
7
+	"github.com/9seconds/mtg/v2/network"
8 8
 	"github.com/stretchr/testify/suite"
9 9
 )
10 10
 

+ 42
- 0
testlib/capture_output.go Ver arquivo

@@ -0,0 +1,42 @@
1
+package testlib
2
+
3
+import (
4
+	"bytes"
5
+	"io"
6
+	"os"
7
+	"strings"
8
+)
9
+
10
+func CaptureStdout(callback func()) string {
11
+	return captureOutput(&os.Stdout, callback)
12
+}
13
+
14
+func CaptureStderr(callback func()) string {
15
+	return captureOutput(&os.Stderr, callback)
16
+}
17
+
18
+func captureOutput(filefp **os.File, callback func()) string {
19
+	oldFp := *filefp
20
+
21
+	defer func() {
22
+		*filefp = oldFp
23
+	}()
24
+
25
+	reader, writer, _ := os.Pipe()
26
+	buf := &bytes.Buffer{}
27
+	closeChan := make(chan bool)
28
+
29
+	go func() {
30
+		io.Copy(buf, reader) // nolint: errcheck
31
+		close(closeChan)
32
+	}()
33
+
34
+	*filefp = writer
35
+
36
+	callback()
37
+
38
+	writer.Close()
39
+	<-closeChan
40
+
41
+	return strings.TrimSpace(buf.String())
42
+}

+ 48
- 0
testlib/net_conn_mock.go Ver arquivo

@@ -0,0 +1,48 @@
1
+package testlib
2
+
3
+import (
4
+	"net"
5
+	"time"
6
+
7
+	"github.com/stretchr/testify/mock"
8
+)
9
+
10
+type NetConnMock struct {
11
+	mock.Mock
12
+}
13
+
14
+func (n *NetConnMock) Read(b []byte) (int, error) {
15
+	args := n.Called(b)
16
+
17
+	return args.Int(0), args.Error(1)
18
+}
19
+
20
+func (n *NetConnMock) Write(b []byte) (int, error) {
21
+	args := n.Called(b)
22
+
23
+	return args.Int(0), args.Error(1)
24
+}
25
+
26
+func (n *NetConnMock) Close() error {
27
+	return n.Called().Error(0)
28
+}
29
+
30
+func (n *NetConnMock) LocalAddr() net.Addr {
31
+	return n.Called().Get(0).(net.Addr)
32
+}
33
+
34
+func (n *NetConnMock) RemoteAddr() net.Addr {
35
+	return n.Called().Get(0).(net.Addr)
36
+}
37
+
38
+func (n *NetConnMock) SetDeadline(t time.Time) error {
39
+	return n.Called(t).Error(0)
40
+}
41
+
42
+func (n *NetConnMock) SetReadDeadline(t time.Time) error {
43
+	return n.Called(t).Error(0)
44
+}
45
+
46
+func (n *NetConnMock) SetWriteDeadline(t time.Time) error {
47
+	return n.Called(t).Error(0)
48
+}

+ 35
- 0
testlib/network_mock.go Ver arquivo

@@ -0,0 +1,35 @@
1
+package testlib
2
+
3
+import (
4
+	"context"
5
+	"net"
6
+	"net/http"
7
+	"time"
8
+
9
+	"github.com/stretchr/testify/mock"
10
+)
11
+
12
+type NetworkMock struct {
13
+	mock.Mock
14
+}
15
+
16
+func (n *NetworkMock) Dial(network, address string) (net.Conn, error) {
17
+	args := n.Called(network, address)
18
+
19
+	return args.Get(0).(net.Conn), args.Error(1)
20
+}
21
+
22
+func (n *NetworkMock) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
23
+	args := n.Called(ctx, network, address)
24
+
25
+	return args.Get(0).(net.Conn), args.Error(1)
26
+}
27
+
28
+func (n *NetworkMock) MakeHTTPClient(dialFunc func(ctx context.Context,
29
+	network, address string) (net.Conn, error)) *http.Client {
30
+	return n.Called(dialFunc).Get(0).(*http.Client)
31
+}
32
+
33
+func (n *NetworkMock) IdleTimeout() time.Duration {
34
+	return n.Called().Get(0).(time.Duration)
35
+}

Carregando…
Cancelar
Salvar