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