forked from utekaravinash/gopaapi5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.go
235 lines (202 loc) · 6.55 KB
/
request_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package gopaapi5
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"strings"
"testing"
"time"
"github.com/utekaravinash/gopaapi5/api"
)
var (
testRequestTime = time.Date(2020, time.February, 22, 11, 15, 9, 0, time.UTC)
testAuthorizationHeader = "AWS4-HMAC-SHA256 Credential=accessKey/20200222/us-east-1/ProductAdvertisingAPI/aws4_request " +
"SignedHeaders=content-encoding;content-type;host;x-amz-date;x-amz-target " +
"Signature=a9c55a51fd4110f33032f20399d6d3df03df43f4a9b45188292132672dbe7aba"
)
func TestRequestBuildSign(t *testing.T) {
client, err := NewClient("accessKey", "secretKey", "associateTag", api.UnitedStates)
if err != nil {
t.Fatalf("Error getting new client: %s", err)
}
req := &request{
operation: api.GetItems,
payload: map[string]interface{}{},
path: "paapi5/getitems",
client: client,
dateTime: testRequestTime,
}
err = req.build()
if err != nil {
t.Fatalf("Error building request: %s", err)
}
err = req.sign()
if err != nil {
t.Fatalf("Error signing request: %s", err)
}
reqBody, err := ioutil.ReadAll(req.httpReq.Body)
if err != nil {
t.Errorf("Error reading request body: %s", err)
}
tests := []struct {
name string
expected interface{}
actual interface{}
}{
{"PayloadPartnerType", "Associates", req.payload["PartnerType"]},
{"PayloadPartnerTag", "associateTag", req.payload["PartnerTag"]},
{"PayloadMarketplace", "www.amazon.com", req.payload["Marketplace"]},
{"HeaderHost", "webservices.amazon.com", req.httpReq.Header.Get("host")},
{"HeaderContentType", "application/json; charset=utf-8", req.httpReq.Header.Get("content-type")},
{"HeaderContentEncoding", "amz-1.0", req.httpReq.Header.Get("content-encoding")},
{"HeaderXAmzDate", "20200222T111509Z", req.httpReq.Header.Get("x-amz-date")},
{"HeaderXAmzTarget", "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.GetItems", req.httpReq.Header.Get("x-amz-target")},
{"AuthorizationHeader", testAuthorizationHeader, req.httpReq.Header.Get(authorizationHeader)},
{"RequestBody", `{"Marketplace":"www.amazon.com","PartnerTag":"associateTag","PartnerType":"Associates"}`, string(reqBody)},
{"RequestURL", "https://webservices.amazon.com/paapi5/getitems", req.httpReq.URL.String()},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.expected != test.actual {
t.Errorf("Expected: %v, Actual: %v", test.expected, test.actual)
}
})
}
}
func TestStripExcessSpaces(t *testing.T) {
valuesWithSpaceAtBothEnds := []string{"abc", "a b c", " abc", "abc ", " abc "}
stripExcessSpaces(valuesWithSpaceAtBothEnds)
valuesWithoutSpaceAtBothEnds := []string{"abc", "a b c", "abc", "abc", "abc"}
stripExcessSpaces(valuesWithoutSpaceAtBothEnds)
nilSlice := []string{}
stripExcessSpaces(nilSlice)
tests := []struct {
name string
expected interface{}
actual interface{}
}{
{"ValuesWithSpaceAtBothEnds", "abc,a b c,abc,abc,abc", strings.Join(valuesWithSpaceAtBothEnds, ",")},
{"ValuesWithoutSpaceAtBothEnds", "abc,a b c,abc,abc,abc", strings.Join(valuesWithoutSpaceAtBothEnds, ",")},
{"NilSlice", "", strings.Join(nilSlice, ",")},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.expected != test.actual {
t.Errorf("Expected: %v, Actual: %v", test.expected, test.actual)
}
})
}
}
func TestHmacSHA256(t *testing.T) {
tests := []struct {
name string
expected interface{}
actual interface{}
}{
{"Key1,Data1", "8736733aaa19a38fe88073449795f85272d7a709a12d0ff226a1c65180a7a52d", fmt.Sprintf("%x", hmacSHA256([]byte("Key1"), []byte("Data1")))},
{"Key2,Data2", "ef1be62d151e55c16944e80587c17bd29143c00812392fb6113c1b20cadc67bf", fmt.Sprintf("%x", hmacSHA256([]byte("Key2"), []byte("Data2")))},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.expected != test.actual {
t.Errorf("Expected: %v, Actual: %v", test.expected, test.actual)
}
})
}
}
func TestHashSHA256(t *testing.T) {
tests := []struct {
name string
expected interface{}
actual interface{}
}{
{"Data1", "e9fb5550dbc06ade237d302497f9b8b246984676f10562b9813d45da26ffa6d7", fmt.Sprintf("%x", hashSHA256([]byte("Data1")))},
{"Data2", "24975629874c2650cab0e97f3a4e97a6e50f91278d5be470277a3bc2f317c459", fmt.Sprintf("%x", hashSHA256([]byte("Data2")))},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.expected != test.actual {
t.Errorf("Expected: %v, Actual: %v", test.expected, test.actual)
}
})
}
}
func TestFormatShortLongDate(t *testing.T) {
tests := []struct {
name string
expected interface{}
actual interface{}
}{
{"ShortDate", "20200222", formatShortDate(testRequestTime)},
{"LongDate", "20200222T111509Z", formatDate(testRequestTime)},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.expected != test.actual {
t.Errorf("Expected: %v, Actual: %v", test.expected, test.actual)
}
})
}
}
func TestGetURIPath(t *testing.T) {
url1, err := url.Parse("https://en.wikipedia.org/wiki/URL")
if err != nil {
t.Fatalf("Error parsing URL: %s", "https://en.wikipedia.org/wiki/URL")
}
url2, err := url.Parse("http://example.com/abc?xyz=def")
if err != nil {
t.Fatalf("Error parsing URL: %s", "https://en.wikipedia.org/wiki/URL")
}
url3, err := url.Parse("http://example.com")
if err != nil {
t.Fatalf("Error parsing URL: %s", "http://example.com")
}
tests := []struct {
name string
expected interface{}
actual interface{}
}{
{"URL1", "/wiki/URL", getURIPath(url1)},
{"URL2", "/abc", getURIPath(url2)},
{"URL3", "/", getURIPath(url3)},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.expected != test.actual {
t.Errorf("Expected: %v, Actual: %v", test.expected, test.actual)
}
})
}
}
func TestSend(t *testing.T) {
client, _ := NewClient("accessKey", "secretKey", "associateTag", api.UnitedStates)
client.testing = true
req := &request{
operation: api.GetItems,
payload: map[string]interface{}{"Test": &struct{}{}},
client: client,
path: fmt.Sprintf("paapi5/%s", strings.ToLower(string(api.GetItems))),
dateTime: time.Now().UTC(),
ctx: context.Background(),
}
err := req.build()
if err != nil {
t.Fail()
}
tests := []struct {
name string
expected interface{}
actual interface{}
}{
// {"Check Err", nil, err},
// {"Nil HTTP Client", ErrNilHttpClient, err},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.expected != test.actual {
t.Errorf("Expected: %v, Actual: %v", test.expected, test.actual)
}
})
}
}