forked from abclogin/dadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dadata_http_client_test.go
178 lines (161 loc) · 6.16 KB
/
dadata_http_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
package dadata // import "gopkg.in/webdeskltd/dadata.v2"
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
type fakeHandlerOK struct{ t *testing.T }
type fakeHandlerISE struct{ t *testing.T }
// ServeHTTP Fake http server http.Handler response OK
func (fh fakeHandlerOK) ServeHTTP(wr http.ResponseWriter, rq *http.Request) {
wr.Header().Add("Content-Type", "application/json")
wr.WriteHeader(http.StatusOK)
wr.Write([]byte(`{}`))
fh.t.Logf("Request for fake http server %q, response OK and empty json", rq.RequestURI)
}
// ServeHTTP Fake http server http.Handler response internal server error (RFC 7231, 6.6.1)
func (fh fakeHandlerISE) ServeHTTP(wr http.ResponseWriter, rq *http.Request) {
wr.WriteHeader(http.StatusInternalServerError)
wr.Write([]byte(http.StatusText(http.StatusInternalServerError)))
fh.t.Logf("Request for fake http server %q, response 500", rq.RequestURI)
}
func TestHttpClientRequest(t *testing.T) {
var err error
var srvOK, srvISE *httptest.Server
// Restore baseURL
defer func() { baseURL = constBaseURL }()
// Server response OK
srvOK = httptest.NewServer(&fakeHandlerOK{t: t})
baseURL = srvOK.URL + "/"
_, err = NewDaData("", "").ProfileBalance()
if err != nil {
t.Fatalf("DaData request error: %s", err)
}
// Server fail
srvISE = httptest.NewServer(&fakeHandlerISE{t: t})
baseURL = srvISE.URL + "/"
_, err = NewDaData("", "").ProfileBalance()
if err == nil {
t.Fatalf("Incorrect work a http client. Web server response 500 (internal server error), but request completed without errors")
}
}
func TestCleanNilResponseAndError(t *testing.T) {
var err error
var srvISE *httptest.Server
var dadata *DaData
var address []Address
var phones []Phone
var names []Name
var emails []Email
var birthdates []Birthdate
var vehicles []Vehicle
var passports []Passport
// Restore baseURL
defer func() { baseURL = constBaseURL }()
srvISE = httptest.NewServer(&fakeHandlerISE{t: t})
baseURL = srvISE.URL + "/"
dadata = NewDaData("", "")
// Test functions
if address, err = dadata.CleanAddresses(""); address != nil && err != nil {
t.Fatalf("Function CleanAddresses() returns not a nil response with an error: %s", err)
}
if phones, err = dadata.CleanPhones(""); phones != nil && err != nil {
t.Fatalf("Function CleanPhones() returns not a nil response with an error: %s", err)
}
if names, err = dadata.CleanNames(""); names != nil && err != nil {
t.Fatalf("Function CleanNames() returns not a nil response with an error: %s", err)
}
if emails, err = dadata.CleanEmails(""); emails != nil && err != nil {
t.Fatalf("Function CleanEmails() returns not a nil response with an error: %s", err)
}
if birthdates, err = dadata.CleanBirthdates(""); birthdates != nil && err != nil {
t.Fatalf("Function CleanBirthdates() returns not a nil response with an error: %s", err)
}
if vehicles, err = dadata.CleanVehicles(""); vehicles != nil && err != nil {
t.Fatalf("Function CleanVehicles() returns not a nil response with an error: %s", err)
}
if passports, err = dadata.CleanPassports(""); passports != nil && err != nil {
t.Fatalf("Function CleanPassports() returns not a nil response with an error: %s", err)
}
}
func TestSuggestNilResponseAndError(t *testing.T) {
var err error
var srvISE *httptest.Server
var dadata *DaData
var address []ResponseAddress
var names []ResponseName
var banks []ResponseBank
var partyes []ResponseParty
var emails []ResponseEmail
// Restore baseURL
defer func() { baseURL = constBaseURL }()
srvISE = httptest.NewServer(&fakeHandlerISE{t: t})
baseURL = srvISE.URL + "/"
dadata = NewDaData("", "")
// Test functions
if address, err = dadata.SuggestAddresses(SuggestRequestParams{}); address != nil && err != nil {
t.Fatalf("Function SuggestAddresses() returns not a nil response with an error: %s", err)
}
if names, err = dadata.SuggestNames(SuggestRequestParams{}); names != nil && err != nil {
t.Fatalf("Function SuggestNames() returns not a nil response with an error: %s", err)
}
if banks, err = dadata.SuggestBanks(SuggestRequestParams{}); banks != nil && err != nil {
t.Fatalf("Function SuggestBanks() returns not a nil response with an error: %s", err)
}
if partyes, err = dadata.SuggestParties(SuggestRequestParams{}); partyes != nil && err != nil {
t.Fatalf("Function SuggestParties() returns not a nil response with an error: %s", err)
}
if emails, err = dadata.SuggestEmails(SuggestRequestParams{}); emails != nil && err != nil {
t.Fatalf("Function SuggestEmails() returns not a nil response with an error: %s", err)
}
}
func TestDailyStatNilResponseAndError(t *testing.T) {
var err error
var srvISE *httptest.Server
var dadata *DaData
var stat *StatResponse
// Restore baseURL
defer func() { baseURL = constBaseURL }()
srvISE = httptest.NewServer(&fakeHandlerISE{t: t})
baseURL = srvISE.URL + "/"
dadata = NewDaData("", "")
// Test functions
if stat, err = dadata.DailyStat(time.Now()); stat != nil && err != nil {
t.Fatalf("Function DailyStat() returns not a nil response with an error: %s", err)
}
}
func TestAddressByIDNilResponseAndError(t *testing.T) {
var err error
var srvISE *httptest.Server
var dadata *DaData
var address *ResponseAddress
var addresses []ResponseAddress
// Restore baseURL
defer func() { baseURL = constBaseURL }()
srvISE = httptest.NewServer(&fakeHandlerISE{t: t})
baseURL = srvISE.URL + "/"
dadata = NewDaData("", "")
// Test functions
if address, err = dadata.AddressByID(""); address != nil && err != nil {
t.Fatalf("Function AddressByID() returns not a nil response with an error: %s", err)
}
if addresses, err = dadata.AddressesByID(""); addresses != nil && err != nil {
t.Fatalf("Function AddressesByID() returns not a nil response with an error: %s", err)
}
}
func TestGeoIPNilResponseAndError(t *testing.T) {
var err error
var srvISE *httptest.Server
var dadata *DaData
var ip *GeoIPResponse
// Restore baseURL
defer func() { baseSuggestURL = constBaseSuggestURL }()
srvISE = httptest.NewServer(&fakeHandlerISE{t: t})
baseSuggestURL = srvISE.URL + "/"
dadata = NewDaData("", "")
// Test functions
if ip, err = dadata.GeoIP("127.0.0.1"); ip != nil && err != nil {
t.Fatalf("Function GeoIP() returns not a nil response with an error: %s", err)
}
}