-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merche_test.go
168 lines (155 loc) · 3.56 KB
/
merche_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
package merche
import (
"context"
"io"
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"testing"
)
type writer struct {
data []byte
}
func (w *writer) Write(data []byte) (n int, err error) {
w.data = data
return len(data), nil
}
func TestClient_Do(t *testing.T) {
type fakeResponse struct{}
type args struct {
v any
}
tests := []struct {
name string
mercedesAPIMock *httptest.Server
args args
want *Response
wantErr error
}{
{
name: "auth error response",
mercedesAPIMock: createFakeServer(http.StatusUnauthorized, "auth_error.json"),
wantErr: &UnauthorizedError{
ErrorMessage: "Unauthorized",
StatusCode: "401",
Message: "Token invalid: Not active",
},
},
{
name: "exve error response",
mercedesAPIMock: createFakeServer(http.StatusBadRequest, "exve_error.json"),
wantErr: &ExVeError{
ExveErrorID: "Id",
ExveErrorMsg: "Msg",
ExveErrorRef: "Ref",
},
},
{
name: "api error",
mercedesAPIMock: createFakeServer(http.StatusNotFound, ""),
wantErr: &MercedesAPIError{
StatusCode: http.StatusNotFound,
},
},
{
name: "mercedes not content response",
mercedesAPIMock: createFakeServer(http.StatusNoContent, ""),
args: args{
v: &fakeResponse{},
},
},
{
name: "mercedes api response: nil decoding",
mercedesAPIMock: createFakeServer(http.StatusOK, "vehicle_status_get_resources.json"),
args: args{
v: nil,
},
},
{
name: "mercedes api response: copy to writer",
mercedesAPIMock: createFakeServer(http.StatusOK, "vehicle_status_get_resources.json"),
args: args{
v: new(writer),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
url, _ := url.Parse(tt.mercedesAPIMock.URL + "/")
c := NewClient(nil)
c.BaseURL = url
req, _ := c.NewRequest(context.Background(), http.MethodGet, "", http.NoBody)
_, err := c.Do(req, tt.args.v)
if err != nil && err.Error() != tt.wantErr.Error() {
t.Errorf("Client.do() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
func TestClient_NewRequest(t *testing.T) {
invalidBaseURL, _ := url.Parse("https://api.mercedes-benz.com")
type fields struct {
BaseURL *url.URL
}
type args struct {
ctx context.Context
method string
path string
body io.Reader
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "invalid base url",
fields: fields{
BaseURL: invalidBaseURL,
},
args: args{
ctx: context.Background(),
method: http.MethodGet,
path: "",
body: nil,
},
wantErr: true,
},
{
name: "invalid method",
fields: fields{
BaseURL: nil,
},
args: args{
ctx: nil,
method: http.MethodGet,
path: "",
body: nil,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(nil)
if tt.fields.BaseURL != nil {
c.BaseURL = tt.fields.BaseURL
}
_, err := c.NewRequest(tt.args.ctx, tt.args.method, tt.args.path, tt.args.body)
if (err != nil) != tt.wantErr {
t.Errorf("Client.NewRequest() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
func createFakeServer(statusCode int, res string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
http.ServeFile(w, r, filepath.Join("testdata", res))
}))
}