Add test for default dialer

This commit is contained in:
9seconds
2021-03-05 17:11:54 +03:00
parent 21ee1e2c6c
commit 0ed7e10379
3 changed files with 102 additions and 0 deletions
+2
View File
@@ -5,7 +5,9 @@ go 1.16
require (
github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6
github.com/libp2p/go-reuseport v0.0.2
github.com/mccutchen/go-httpbin v1.1.1 // indirect
github.com/pelletier/go-toml v1.8.1
github.com/shadowsocks/go-shadowsocks2 v0.1.4
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
)
+7
View File
@@ -5,6 +5,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/libp2p/go-reuseport v0.0.2 h1:XSG94b1FJfGA01BUrT82imejHQyTxO4jEWqheyCXYvU=
github.com/libp2p/go-reuseport v0.0.2/go.mod h1:SPD+5RwGC7rcnzngoYC86GjPzjSywuQyMVAheVBD9nQ=
github.com/mccutchen/go-httpbin v1.1.1 h1:aEws49HEJEyXHLDnshQVswfUlCVoS8g6h9YaDyaW7RE=
github.com/mccutchen/go-httpbin v1.1.1/go.mod h1:fhpOYavp5g2K74XDl/ao2y4KvhqVtKlkg1e+0UaQv7I=
github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM=
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
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
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3/go.mod h1:HgjTstvQsPGkxUsCd2KWxErBblirPizecHcpD3ffK+s=
github.com/shadowsocks/go-shadowsocks2 v0.1.4 h1:4VzajPL7RwwmImysBSvI+lm/UaegDGQq3hr42dYo3gs=
github.com/shadowsocks/go-shadowsocks2 v0.1.4/go.mod h1:AGGpIoek4HRno4xzyFiAtLHkOpcoznZEkAccaI/rplM=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 h1:/ZScEX8SfEmUGRHs0gxpqteO5nfNW6axyZbBdw9A12g=
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
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+93
View File
@@ -0,0 +1,93 @@
package network_test
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/9seconds/mtg/v2/mtglib/network"
"github.com/mccutchen/go-httpbin/httpbin"
"github.com/stretchr/testify/suite"
)
type DefaultDialerTestSuite struct {
suite.Suite
d network.Dialer
srvAddress string
srv *httptest.Server
}
func (suite *DefaultDialerTestSuite) SetupSuite() {
suite.srv = httptest.NewServer(httpbin.NewHTTPBin().Handler())
suite.srvAddress = strings.TrimPrefix(suite.srv.URL, "http://")
}
func (suite *DefaultDialerTestSuite) SetupTest() {
d, err := network.NewDefaultDialer(0, 0)
suite.NoError(err)
suite.d = d
}
func (suite *DefaultDialerTestSuite) TearDownSuite() {
suite.srv.Close()
}
func (suite *DefaultDialerTestSuite) TestNegativeTimeout() {
_, err := network.NewDefaultDialer(-1, 0)
suite.Error(err)
}
func (suite *DefaultDialerTestSuite) TestNegativeBufferSize() {
_, err := network.NewDefaultDialer(0, -1)
suite.Error(err)
}
func (suite *DefaultDialerTestSuite) TestUnsupportedProtocol() {
_, err := suite.d.DialContext(context.Background(), "udp", suite.srvAddress)
suite.Error(err)
}
func (suite *DefaultDialerTestSuite) TestCannotDial() {
_, err := suite.d.DialContext(context.Background(),
"tcp",
suite.srvAddress+suite.srvAddress)
suite.Error(err)
}
func (suite *DefaultDialerTestSuite) TestConnectOk() {
conn, err := suite.d.DialContext(context.Background(),
"tcp",
suite.srvAddress)
suite.NoError(err)
suite.NotNil(conn)
conn.Close()
}
func (suite *DefaultDialerTestSuite) TestRequest() {
httpClient := http.Client{
Transport: &http.Transport{
DialContext: suite.d.DialContext,
},
}
resp, err := httpClient.Get(suite.srv.URL + "/get")
suite.NoError(err)
resp.Body.Close()
}
func TestDefaultDialer(t *testing.T) {
suite.Run(t, &DefaultDialerTestSuite{})
}