Ver código fonte

Add test for default dialer

tags/v2.0.0-rc1
9seconds 5 anos atrás
pai
commit
0ed7e10379
3 arquivos alterados com 102 adições e 0 exclusões
  1. 2
    0
      go.mod
  2. 7
    0
      go.sum
  3. 93
    0
      mtglib/network/default_test.go

+ 2
- 0
go.mod Ver arquivo

@@ -5,7 +5,9 @@ go 1.16
5 5
 require (
6 6
 	github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6
7 7
 	github.com/libp2p/go-reuseport v0.0.2
8
+	github.com/mccutchen/go-httpbin v1.1.1 // indirect
8 9
 	github.com/pelletier/go-toml v1.8.1
9 10
 	github.com/shadowsocks/go-shadowsocks2 v0.1.4
11
+	github.com/stretchr/testify v1.7.0 // indirect
10 12
 	golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
11 13
 )

+ 7
- 0
go.sum Ver arquivo

@@ -5,6 +5,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
5 5
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6 6
 github.com/libp2p/go-reuseport v0.0.2 h1:XSG94b1FJfGA01BUrT82imejHQyTxO4jEWqheyCXYvU=
7 7
 github.com/libp2p/go-reuseport v0.0.2/go.mod h1:SPD+5RwGC7rcnzngoYC86GjPzjSywuQyMVAheVBD9nQ=
8
+github.com/mccutchen/go-httpbin v1.1.1 h1:aEws49HEJEyXHLDnshQVswfUlCVoS8g6h9YaDyaW7RE=
9
+github.com/mccutchen/go-httpbin v1.1.1/go.mod h1:fhpOYavp5g2K74XDl/ao2y4KvhqVtKlkg1e+0UaQv7I=
8 10
 github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM=
9 11
 github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
10 12
 github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -15,9 +17,12 @@ github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3 h1:f/FNXud6gA3MNr
15 17
 github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3/go.mod h1:HgjTstvQsPGkxUsCd2KWxErBblirPizecHcpD3ffK+s=
16 18
 github.com/shadowsocks/go-shadowsocks2 v0.1.4 h1:4VzajPL7RwwmImysBSvI+lm/UaegDGQq3hr42dYo3gs=
17 19
 github.com/shadowsocks/go-shadowsocks2 v0.1.4/go.mod h1:AGGpIoek4HRno4xzyFiAtLHkOpcoznZEkAccaI/rplM=
20
+github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
18 21
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
19 22
 github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
20 23
 github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
24
+github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
25
+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
21 26
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
22 27
 golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 h1:/ZScEX8SfEmUGRHs0gxpqteO5nfNW6axyZbBdw9A12g=
23 28
 golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
@@ -37,3 +42,5 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
37 42
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
38 43
 gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
39 44
 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
45
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
46
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

+ 93
- 0
mtglib/network/default_test.go Ver arquivo

@@ -0,0 +1,93 @@
1
+package network_test
2
+
3
+import (
4
+	"context"
5
+	"net/http"
6
+	"net/http/httptest"
7
+	"strings"
8
+	"testing"
9
+
10
+	"github.com/9seconds/mtg/v2/mtglib/network"
11
+	"github.com/mccutchen/go-httpbin/httpbin"
12
+	"github.com/stretchr/testify/suite"
13
+)
14
+
15
+type DefaultDialerTestSuite struct {
16
+	suite.Suite
17
+
18
+	d          network.Dialer
19
+	srvAddress string
20
+	srv        *httptest.Server
21
+}
22
+
23
+func (suite *DefaultDialerTestSuite) SetupSuite() {
24
+	suite.srv = httptest.NewServer(httpbin.NewHTTPBin().Handler())
25
+	suite.srvAddress = strings.TrimPrefix(suite.srv.URL, "http://")
26
+}
27
+
28
+func (suite *DefaultDialerTestSuite) SetupTest() {
29
+	d, err := network.NewDefaultDialer(0, 0)
30
+
31
+	suite.NoError(err)
32
+
33
+	suite.d = d
34
+}
35
+
36
+func (suite *DefaultDialerTestSuite) TearDownSuite() {
37
+	suite.srv.Close()
38
+}
39
+
40
+func (suite *DefaultDialerTestSuite) TestNegativeTimeout() {
41
+	_, err := network.NewDefaultDialer(-1, 0)
42
+
43
+	suite.Error(err)
44
+}
45
+
46
+func (suite *DefaultDialerTestSuite) TestNegativeBufferSize() {
47
+	_, err := network.NewDefaultDialer(0, -1)
48
+
49
+	suite.Error(err)
50
+}
51
+
52
+func (suite *DefaultDialerTestSuite) TestUnsupportedProtocol() {
53
+	_, err := suite.d.DialContext(context.Background(), "udp", suite.srvAddress)
54
+
55
+	suite.Error(err)
56
+}
57
+
58
+func (suite *DefaultDialerTestSuite) TestCannotDial() {
59
+	_, err := suite.d.DialContext(context.Background(),
60
+		"tcp",
61
+		suite.srvAddress+suite.srvAddress)
62
+
63
+	suite.Error(err)
64
+}
65
+
66
+func (suite *DefaultDialerTestSuite) TestConnectOk() {
67
+	conn, err := suite.d.DialContext(context.Background(),
68
+		"tcp",
69
+		suite.srvAddress)
70
+
71
+	suite.NoError(err)
72
+	suite.NotNil(conn)
73
+
74
+	conn.Close()
75
+}
76
+
77
+func (suite *DefaultDialerTestSuite) TestRequest() {
78
+	httpClient := http.Client{
79
+		Transport: &http.Transport{
80
+			DialContext: suite.d.DialContext,
81
+		},
82
+	}
83
+
84
+	resp, err := httpClient.Get(suite.srv.URL + "/get")
85
+
86
+	suite.NoError(err)
87
+
88
+	resp.Body.Close()
89
+}
90
+
91
+func TestDefaultDialer(t *testing.T) {
92
+	suite.Run(t, &DefaultDialerTestSuite{})
93
+}

Carregando…
Cancelar
Salvar