-
Notifications
You must be signed in to change notification settings - Fork 0
/
bus_test.go
227 lines (207 loc) · 6.23 KB
/
bus_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package mta
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestBusErrClientRequired(t *testing.T) {
_, err := NewBusTimeClient(nil, "", "")
require.Error(t, err)
require.Equal(t, ErrClientRequired, err)
}
func TestBusErrAPIKeyRequired(t *testing.T) {
_, err := NewBusTimeClient(mockClient{}, "", "")
require.Error(t, err)
require.Equal(t, ErrAPIKeyRequired, err)
}
func TestGetStopMonitoring(t *testing.T) {
DoFunc = func(req *http.Request) (*http.Response, error) {
json := `{
"Siri": {
"ServiceDelivery": {
"ResponseTimestamp": "2006-01-02T15:04:05.000-07:00",
"StopMonitoringDelivery": [
{
"ResponseTimestamp": "2006-01-02T15:04:05.000-07:00",
"MonitoredStopVisit": [
{
"MonitoredVehicleJourney": {
"LineRef": "MTA NYCT_M20",
"FramedVehicleJourneyRef": {
"DataFrameRef": "M20",
"DatedVehicleJourneyRef": "M20"
},
"OperatorRef": "MTA",
"OriginRef": "404847",
"PublishedLineName": [
"M20"
],
"DestinationName": [
"LINCOLN CENTER 66 ST via 8 AV"
],
"MonitoredCall": {
"ExpectedArrivalTime": "2021-08-14T23:40:57.787-04:00"
}
}
}
]
}
],
"SituationExchangeDelivery": []
}
}
}`
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(json))),
}, nil
}
c, err := NewBusTimeClient(mockClient{}, "apiKey", "")
require.NoError(t, err)
resp, err := c.GetStopMonitoring("404847")
require.NoError(t, err)
expectedResp := &StopMonitoringResponse{
Siri: Siri{
ServiceDelivery: ServiceDelivery{
ResponseTimestamp: "2006-01-02T15:04:05.000-07:00",
StopMonitoringDelivery: []StopMonitoringDelivery{
{
ResponseTimestamp: "2006-01-02T15:04:05.000-07:00",
MonitoredStopVisit: []MonitoredStopVisit{
{
MonitoredVehicleJourney: MonitoredVehicleJourney{
LineRef: "MTA NYCT_M20",
FramedVehicleJourneyRef: FramedVehicleJourneyRef{
DataFrameRef: "M20",
DatedVehicleJourneyRef: "M20",
},
OperatorRef: "MTA",
OriginRef: "404847",
PublishedLineName: []string{"M20"},
DestinationName: []string{"LINCOLN CENTER 66 ST via 8 AV"},
MonitoredCall: &MonitoredCall{
ExpectedArrivalTime: "2021-08-14T23:40:57.787-04:00",
},
},
},
},
},
},
},
},
}
require.Equal(t, expectedResp, resp)
}
func TestGetStopMonitoringErrAPIKeyNotAuthorized(t *testing.T) {
DoFunc = func(req *http.Request) (*http.Response, error) {
json := `{
"Siri": {
"ServiceDelivery": {
"ResponseTimestamp": "2006-01-02T15:04:05.000-07:00",
"VehicleMonitoringDelivery": [
{
"ResponseTimestamp": "2006-01-02T15:04:05.000-07:00",
"ErrorCondition": {
"OtherError": {
"ErrorText": "API key is not authorized."
},
"Description": "API key is not authorized."
}
}
]
}
}
}`
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(json))),
}, nil
}
c, _ := NewBusTimeClient(mockClient{}, "apiKey", "")
_, err := c.GetStopMonitoring("404847")
require.Error(t, err)
require.Equal(t, ErrAPIKeyNotAuthorized, err)
}
func TestGetStopMonitoringErrAPIKeyRequired2(t *testing.T) {
DoFunc = func(req *http.Request) (*http.Response, error) {
json := `{
"Siri": {
"ServiceDelivery": {
"ResponseTimestamp": "2006-01-02T15:04:05.000-07:00",
"VehicleMonitoringDelivery": [
{
"ResponseTimestamp": "2006-01-02T15:04:05.000-07:00",
"ErrorCondition": {
"OtherError": {
"ErrorText": "API key required."
},
"Description": "API key required."
}
}
]
}
}
}`
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(json))),
}, nil
}
c, _ := NewBusTimeClient(mockClient{}, "apiKey", "")
_, err := c.GetStopMonitoring("404847")
require.Error(t, err)
require.Equal(t, "API key required.", err.Error())
}
func TestGetStopMonitoringErrRequestSend(t *testing.T) {
DoFunc = func(req *http.Request) (*http.Response, error) {
return nil, net.UnknownNetworkError("...")
}
c, _ := NewBusTimeClient(mockClient{}, "apiKey", "")
_, err := c.GetStopMonitoring("404847")
require.Error(t, err)
require.Equal(t, "failed to send GetStopMonitoring request: unknown network ...", err.Error())
}
func TestGetStopMonitoringErrNon200(t *testing.T) {
DoFunc = func(req *http.Request) (*http.Response, error) {
return &http.Response{
Status: "500 Internal Server Error",
StatusCode: http.StatusInternalServerError,
Body: ioutil.NopCloser(bytes.NewReader([]byte("..."))),
}, nil
}
c, _ := NewBusTimeClient(mockClient{}, "apiKey", "")
_, err := c.GetStopMonitoring("404847")
require.Error(t, err)
require.Equal(t, "non 200 response status: 500 Internal Server Error", err.Error())
}
func TestGetStopMonitoringErrReadBody(t *testing.T) {
mockReadCloser := mockReadCloser{}
mockReadCloser.On("Read", mock.AnythingOfType("[]uint8")).Return(0, fmt.Errorf("error reading"))
mockReadCloser.On("Close").Return(nil)
DoFunc = func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: &mockReadCloser,
}, nil
}
c, _ := NewBusTimeClient(mockClient{}, "apiKey", "")
_, err := c.GetStopMonitoring("404847")
require.Error(t, err)
require.Equal(t, "failed to read GetStopMonitoring response: error reading", err.Error())
}
func TestGetStopMonitoringErrBadResponse(t *testing.T) {
DoFunc = func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte("not JSON"))),
}, nil
}
c, _ := NewBusTimeClient(mockClient{}, "apiKey", "")
_, err := c.GetStopMonitoring("404847")
require.Error(t, err, "failed to parse GetStopMonitoring response: invalid character 'o' in literal null (expecting 'u'), body: not JSON")
}