This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
185 lines (172 loc) · 5.5 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
181
182
183
184
185
package p24
import (
"context"
"encoding/xml"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func TestNewClient(t *testing.T) {
logOutput := ""
log := LogFunc(func(format string, args ...interface{}) {
logOutput = fmt.Sprintf(format, args...)
})
doerOutput := ""
doer := DoFunc(func(req *http.Request) (*http.Response, error) {
doerOutput = req.Method
return nil, nil
})
merchant := Merchant{
ID: "id",
Pass: "pass",
}
cli := NewClient(ClientOpts{
HTTP: doer,
Merchant: merchant,
})
require.Equal(t, merchant, cli.merchant)
require.NotNil(t, cli.log)
req, _ := http.NewRequest(http.MethodPost, "http://localhost", http.NoBody)
_, _ = cli.http.Do(req)
require.Equal(t, http.MethodPost, doerOutput)
cli = NewClient(ClientOpts{Log: log})
cli.log.Logf("%s", "test")
require.Equal(t, "test", logOutput)
}
func TestClient_DoContext(t *testing.T) {
cases := []struct {
expected Response
merchant Merchant
body []byte
code int
errMsg string
}{
{ // ok
expected: Response{
XMLName: xml.Name{Local: "response"},
MerchantSign: MerchantSign{"id", "ad67cf1c11e0f87bedac2c9bb260e3abf54e9862"},
Data: ResponseData{
Oper: defaultOper,
Info: struct {
Test string `xml:"test"`
}{Test: "test"},
},
},
body: []byte(xml.Header + `<response><data><info><test>test</test></info><oper>cmt</oper></data><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
merchant: Merchant{"id", "pass"},
code: 200,
},
{
expected: Response{
XMLName: xml.Name{Local: "response"},
MerchantSign: MerchantSign{"id", "ad67cf1c11e0f87bedac2c9bb260e3abf54e9862"},
Data: ResponseData{
Oper: defaultOper,
Info: struct {
Test string `xml:"test1"`
}{Test: "test"},
},
},
body: []byte(xml.Header + `<response><data><info><test>test</test></info><oper>cmt</oper></data><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
merchant: Merchant{"id", "pass"},
code: 200,
errMsg: "can`t unmarshal xml response",
},
{
expected: Response{
XMLName: xml.Name{Local: "response"},
MerchantSign: MerchantSign{"id", "ad67cf1c11e0f87bedac2c9bb260e3abf54e9862"},
Data: ResponseData{
Oper: defaultOper,
Info: struct {
Test string `xml:"test"`
}{Test: "test"},
},
},
body: []byte(xml.Header + `<response><data><info><test>test</test></info><oper>cmt</oper><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
merchant: Merchant{"id", "pass"},
code: 200,
errMsg: "unexpected xml response content",
},
{
expected: Response{
XMLName: xml.Name{Local: "response"},
MerchantSign: MerchantSign{"id", "ad67cf1c11e0f87bedac2c9bb260e3abf54e9862"},
Data: ResponseData{
Oper: defaultOper,
Info: struct {
Test string `xml:"test"`
}{Test: "test"},
},
},
body: []byte(xml.Header + `<response><data><info><test>test</test></info><oper>cmt</oper></data><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
merchant: Merchant{"id", "other pass"},
errMsg: "invalid signature",
code: 200,
},
{
expected: Response{
XMLName: xml.Name{Local: "response"},
MerchantSign: MerchantSign{"id", "ad67cf1c11e0f87bedac2c9bb260e3abf54e9862"},
Data: ResponseData{
Oper: defaultOper,
Info: struct {
Test string `xml:"test"`
}{Test: "test"},
},
},
body: []byte(xml.Header + `<response><data><info>some error</info><oper>cmt</oper></data><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
merchant: Merchant{"id", "pass"},
errMsg: "xml response with err",
code: 200,
},
{
expected: Response{
XMLName: xml.Name{Local: "response"},
MerchantSign: MerchantSign{"id", "ad67cf1c11e0f87bedac2c9bb260e3abf54e9862"},
Data: ResponseData{
Oper: defaultOper,
Info: struct {
Test string `xml:"test"`
}{Test: "test"},
},
},
body: []byte(xml.Header + `<response><data><info><test>test</test></info><oper>cmt</oper></data><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
merchant: Merchant{"id", "pass"},
errMsg: "unexpected http status code",
code: 400,
},
}
url, method, req := "http://localhost", "POST", Request{}
exceptedBody, _ := xml.Marshal(req)
exceptedBody = []byte(xml.Header + string(exceptedBody))
for i, c := range cases {
c := c
t.Run(strconv.Itoa(i), func(t *testing.T) {
var do DoFunc = func(req *http.Request) (*http.Response, error) {
require.Equal(t, url, req.URL.String())
require.Equal(t, method, req.Method)
actualBody, err := io.ReadAll(req.Body)
require.NoError(t, err)
require.Equal(t, string(exceptedBody), string(actualBody))
tr := httptest.NewRecorder()
_, _ = tr.Write(c.body)
tr.Code = c.code
return tr.Result(), nil
}
cli := Client{do, nil, c.merchant}
actual := c.expected
if err := cli.DoContext(context.Background(), url, method, req, &actual); err != nil {
require.NotEmpty(t, c.errMsg)
require.ErrorContains(t, err, c.errMsg)
return
}
require.Empty(t, c.errMsg)
require.Equal(t, c.expected, actual)
})
}
}