-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_test.go
114 lines (88 loc) · 3.47 KB
/
transport_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package engineio_test
import (
"fmt"
"net/http"
"net/url"
"testing"
engineio "github.com/lewisgibson/go-engine.io"
"github.com/lewisgibson/go-engine.io/internal/mocks"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)
func TestTransportType_String(t *testing.T) {
t.Parallel()
// Assert: the string representation of the transport type should never be empty.
require.NotEmpty(t, engineio.TransportTypePolling.String())
}
func TestTransportState_String(t *testing.T) {
t.Parallel()
// Assert: the string representation of the transport state should never be empty.
require.NotEmpty(t, engineio.TransportStateOpen.String())
}
func TestTransportRoundTripper_RoundTrip_NilTransportClient(t *testing.T) {
t.Parallel()
// Arrange: create a new transport round tripper.
transport := engineio.TransportRoundTripper{}
// Act: round trip a request.
_, err := transport.RoundTrip(&http.Request{})
require.ErrorIsf(t, err, engineio.ErrTransportRoundTripperClientRequired, "error should be ErrTransportRoundTripperClientRequired")
}
func TestTransportRoundTripper_RoundTrip_CallsTransportClient(t *testing.T) {
t.Parallel()
// Arrange: create a new mock controller.
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// Arrange: create a new mock transport client.
mockTransportClient := mocks.NewMockTransportClient(ctrl)
mockTransportClient.EXPECT().
Do(gomock.Any()).
Return(&http.Response{
StatusCode: http.StatusOK,
}, nil)
// Arrange: create a new transport round tripper.
transport := engineio.TransportRoundTripper{
Client: mockTransportClient,
}
// Act: round trip a request.
r, err := transport.RoundTrip(&http.Request{})
require.NoErrorf(t, err, "error should be nil")
// Assert: the response should not be nil.
require.NotNilf(t, r, "response should not be nil")
require.Equalf(t, http.StatusOK, r.StatusCode, "response status code should be http.StatusOK")
}
func TestTransports(t *testing.T) {
t.Parallel()
u, err := url.Parse("http://localhost/engine.io/?EIO=4&transport=polling")
require.NoError(t, err)
for transportType, transportConstructor := range engineio.Transports {
t.Run(fmt.Sprintf("%s without url", transportType), func(t *testing.T) {
t.Parallel()
// Act: create a new transport without a URL.
transport, err := transportConstructor(nil, engineio.TransportOptions{})
// Assert: an error should be returned and the transport should be nil.
require.Errorf(t, err, "url is required")
require.Nilf(t, transport, "transport should be nil")
})
t.Run(fmt.Sprintf("%s without options", transportType), func(t *testing.T) {
t.Parallel()
// Act: create a new transport without options.
transport, err := transportConstructor(u, engineio.TransportOptions{})
// Assert: no error should be returned and the transport should not be nil.
require.NoErrorf(t, err, "transport should not return an error")
require.NotNilf(t, transport, "transport should not be nil")
})
t.Run(fmt.Sprintf("%s with options", transportType), func(t *testing.T) {
t.Parallel()
// Act: create a new transport with options.
transport, err := transportConstructor(u, engineio.TransportOptions{
Client: http.DefaultClient,
Header: http.Header{
"Authorization": []string{"Bearer token"},
},
})
// Assert: no error should be returned and the transport should not be nil.
require.NoErrorf(t, err, "transport should not return an error")
require.NotNilf(t, transport, "transport should not be nil")
})
}
}