-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeeds_test.go
131 lines (113 loc) · 3.55 KB
/
feeds_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
package mta
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
"testing"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/errornil/mta/v2/transit_realtime"
)
func str(v string) *string {
return &v
}
func TestFeedsErrClientRequired(t *testing.T) {
_, err := NewFeedsClient(nil, "", "")
require.Error(t, err)
require.Equal(t, ErrClientRequired, err)
}
func TestFeedsErrAPIKeyRequired(t *testing.T) {
_, err := NewFeedsClient(mockClient{}, "", "")
require.Error(t, err)
require.Equal(t, ErrAPIKeyRequired, err)
}
func TestGetFeedMessage(t *testing.T) {
msg := transit_realtime.FeedMessage{
Header: &transit_realtime.FeedHeader{
GtfsRealtimeVersion: str("1.0"),
},
Entity: []*transit_realtime.FeedEntity{
{
Id: str("1"),
TripUpdate: &transit_realtime.TripUpdate{
Trip: &transit_realtime.TripDescriptor{
StartTime: str("20160101T000000"),
},
StopTimeUpdate: []*transit_realtime.TripUpdate_StopTimeUpdate{
{
StopId: str("1"),
Departure: &transit_realtime.TripUpdate_StopTimeEvent{
Time: proto.Int64(1),
},
},
},
},
},
},
}
DoFunc = func(req *http.Request) (*http.Response, error) {
require.Equal(t, "apiKey", req.Header.Get("x-api-key"))
b, _ := proto.Marshal(&msg)
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(b)),
}, nil
}
c, err := NewFeedsClient(mockClient{}, "apiKey", "")
require.NoError(t, err)
feedMessage, err := c.GetFeedMessage(Feed123456S)
require.NoError(t, err)
require.True(t, proto.Equal(&msg, feedMessage))
}
func TestGetFeedMessageErrRequestSend(t *testing.T) {
DoFunc = func(req *http.Request) (*http.Response, error) {
return nil, net.UnknownNetworkError("...")
}
c, _ := NewFeedsClient(mockClient{}, "apiKey", "")
_, err := c.GetFeedMessage(Feed123456S)
require.Error(t, err)
require.Equal(t, "failed to send GET request: unknown network ...", err.Error())
}
func TestGetFeedMessageErrNon200(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, _ := NewFeedsClient(mockClient{}, "apiKey", "")
_, err := c.GetFeedMessage(Feed123456S)
require.Error(t, err)
require.Equal(t, "non 200 response status: 500 Internal Server Error", err.Error())
}
func TestGetFeedMessageErrReadBody(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, _ := NewFeedsClient(mockClient{}, "apiKey", "")
_, err := c.GetFeedMessage(Feed123456S)
require.Error(t, err)
require.Equal(t, "failed to read response body: error reading", err.Error())
}
func TestGetFeedMessageErrBadResponse(t *testing.T) {
DoFunc = func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte("not Protobuf"))),
}, nil
}
c, _ := NewFeedsClient(mockClient{}, "apiKey", "")
_, err := c.GetFeedMessage(Feed123456S)
require.Error(t, err)
require.Equal(t, "failed to unmarshall GTFS Realtime Feed Message: proto: can't skip unknown wire type 6", err.Error())
}