forked from nikitaksv/apidq-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
180 lines (157 loc) · 4.52 KB
/
client_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
package apidq
import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/nikitaksv/apidq-client-go/dto/address"
"github.com/stretchr/testify/require"
)
const TestAPIKey = "testApiKey123"
func NewTestClient(h http.HandlerFunc) (*Client, *httptest.Server) {
s := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
authKey := r.Header.Get(authorization)
if authKey == "" {
w.WriteHeader(http.StatusUnauthorized)
if _, err := w.Write([]byte(`{"code": 16, "message": "Ключ API обязателен"}`)); err != nil {
panic(err)
}
return
} else if authKey != TestAPIKey {
if _, err := w.Write([]byte(`{"code":16,"message":"Ошибка авторизации"}`)); err != nil {
panic(err)
}
w.WriteHeader(http.StatusUnauthorized)
return
}
h(w, r)
},
),
)
c, err := NewClient(http.DefaultClient, s.URL)
if err != nil {
panic(err)
}
return c.WithAuth(TestAPIKey), s
}
func TestClientHost(t *testing.T) {
c, err := NewClient(http.DefaultClient, "http://no-connect-url.local:9999")
if err != nil {
panic(err)
}
_, _, err = c.Address.Clean(context.Background(), &address.CleanRequest{})
if err == nil {
t.Fatal(errors.New("need network error"))
}
if !strings.Contains(err.Error(), "dial tcp: lookup no-connect-url.local") {
t.Fatal(err.Error())
}
}
func TestClientIncorrectError(t *testing.T) {
invalidErrRsp := []byte(`{"code":"invalid code type","message":false}`)
c, s := NewTestClient(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
_, err := w.Write(invalidErrRsp)
if err != nil {
panic(err)
}
})
defer s.Close()
_, _, err := c.Address.Clean(context.Background(), &address.CleanRequest{})
if err == nil {
t.Fatal(errors.New("need ErrorResponse"))
}
if err.Error() != "{\"code\":\"invalid code type\",\"message\":false}: json: cannot unmarshal string into Go struct field ErrorResponse.code of type int" {
t.Fatal(err)
}
}
func TestClientEmptyErrorResponse(t *testing.T) {
invalidErrRsp := []byte(`{"asd":"qwe"}`)
c, s := NewTestClient(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
_, err := w.Write(invalidErrRsp)
if err != nil {
panic(err)
}
})
defer s.Close()
_, _, err := c.Address.Clean(context.Background(), &address.CleanRequest{})
if err == nil {
t.Fatal(errors.New("need error"))
}
if err.Error() != "{\"asd\":\"qwe\"}: invalid ErrorResponse struct" {
t.Fatal(err)
}
}
func TestClientIncorrectResponse(t *testing.T) {
invalidRsp := []byte(`{"origin":"123","area":false}`)
c, s := NewTestClient(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
_, err := w.Write(invalidRsp)
if err != nil {
panic(err)
}
})
defer s.Close()
_, _, err := c.Address.Clean(context.Background(), &address.CleanRequest{})
if err == nil {
t.Fatal(errors.New("need error"))
}
if err.Error() != "{\"origin\":\"123\",\"area\":false}: json: cannot unmarshal bool into Go struct field CleanResponse.area of type address.Part" {
t.Fatal(err)
}
}
func TestAuth(t *testing.T) {
client, tS := NewTestClient(func(w http.ResponseWriter, r *http.Request) {})
defer tS.Close()
_, _, err := client.WithReqOptions(func(r *http.Request) error {
r.Header.Del(authorization)
return nil
}).Address.Clean(context.Background(), &address.CleanRequest{})
if err == nil {
t.Fatal(errors.New("need ErrorResponse"))
}
if err.Error() != "[16] Ключ API обязателен" {
t.Fatal(err)
}
_, _, err = client.WithAuthService(TestAPIKey, "address").Address.Clean(context.Background(), &address.CleanRequest{})
if err != nil {
t.Fatal(err)
}
client, tS = NewTestClient(func(w http.ResponseWriter, r *http.Request) {})
defer tS.Close()
_, _, err = client.Address.Clean(context.Background(), &address.CleanRequest{})
if err != nil {
t.Fatal(err)
}
}
func testEndpointCall(t *testing.T, reqBs, rspBs []byte, endpointCall func(client *Client) (interface{}, *http.Response, error)) {
h := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
bs, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
require.JSONEq(t, string(bs), string(reqBs))
if _, err = w.Write(rspBs); err != nil {
panic(err)
}
}
client, tS := NewTestClient(h)
defer tS.Close()
cleanRsp, _, err := endpointCall(client)
if err != nil {
t.Fatal(err)
}
bs, err := json.Marshal(cleanRsp)
if err != nil {
t.Fatal(err)
}
require.JSONEq(t, string(bs), string(rspBs))
}