-
Notifications
You must be signed in to change notification settings - Fork 10
/
client_test.go
94 lines (91 loc) · 4.03 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
package paapi5
import (
"testing"
"time"
)
func TestClient(t *testing.T) {
testCases := []struct {
partnerTag string
accessKey string
secretKey string
marketplace string
partnerType string
date TimeStamp
contentEncoding string
hostName string
xAmzDate string
xAmzTarget string
payload []byte
sigedText string
sig string
authorization string
}{
{
partnerTag: "mytag-20",
accessKey: "AKIAIOSFODNN7EXAMPLE",
secretKey: "1234567890",
marketplace: DefaultMarketplace.String(),
partnerType: defaultPartnerType,
date: NewTimeStamp(time.Date(2019, time.September, 30, 8, 31, 54, 0, time.UTC)),
contentEncoding: "amz-1.0",
hostName: "webservices.amazon.com",
xAmzDate: "20190930T083154Z",
xAmzTarget: "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.GetItems",
payload: []byte(`{"ItemIds": ["B07YCM5K55"],"Resources": ["Images.Primary.Small","Images.Primary.Medium","Images.Primary.Large","ItemInfo.ByLineInfo","ItemInfo.ContentInfo","ItemInfo.Classifications","ItemInfo.ExternalIds","ItemInfo.ProductInfo","ItemInfo.Title"],"PartnerTag": "mytag-20","PartnerType": "Associates","Marketplace": "www.amazon.com","Operation": "GetItems"}`),
sigedText: "AWS4-HMAC-SHA256\n20190930T083154Z\n20190930/us-east-1/ProductAdvertisingAPI/aws4_request\n00edab8e9f221dd80f01241b85f4526f204ebf49818d678f041e21404a44b8cb",
sig: "717e266b28f02523fc39894f565532bd53fe80b37a9ed1b631b77c50483f8c08",
authorization: "AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20190930/us-east-1/ProductAdvertisingAPI/aws4_request,SignedHeaders=content-encoding;host;x-amz-date;x-amz-target,Signature=717e266b28f02523fc39894f565532bd53fe80b37a9ed1b631b77c50483f8c08",
},
}
for _, tc := range testCases {
c := New().CreateClient(tc.partnerTag, tc.accessKey, tc.secretKey)
if c.Marketplace() != tc.marketplace {
t.Errorf("Client.Marketplace() is \"%v\", want \"%v\"", c.Marketplace(), tc.marketplace)
}
if c.PartnerTag() != tc.partnerTag {
t.Errorf("Client.PartnerTag() is \"%v\", want \"%v\"", c.PartnerTag(), tc.partnerTag)
}
if c.PartnerType() != tc.partnerType {
t.Errorf("Client.PartnerType() is \"%v\", want \"%v\"", c.PartnerType(), tc.partnerType)
}
hds := newHeaders(c.(*client).server, GetItems, tc.date)
if hds.get("Content-Encoding") != tc.contentEncoding {
t.Errorf("headers.get(\"Content-Encoding\") is \"%v\", want \"%v\"", hds.get("Content-Encoding"), tc.contentEncoding)
}
if hds.get("Host") != tc.hostName {
t.Errorf("headers.get(\"Host\") is \"%v\", want \"%v\"", hds.get("Host"), tc.hostName)
}
if hds.get("X-Amz-Date") != tc.xAmzDate {
t.Errorf("headers.get(\"X-Amz-Date\") is \"%v\", want \"%v\"", hds.get("X-Amz-Date"), tc.xAmzDate)
}
if hds.get("X-Amz-Target") != tc.xAmzTarget {
t.Errorf("headers.get(\"X-Amz-Target\") is \"%v\", want \"%v\"", hds.get("X-Amz-Target"), tc.xAmzTarget)
}
str := c.(*client).signedString(hds, tc.payload)
if str != tc.sigedText {
t.Errorf("Client.signedString() is \"%v\", want \"%v\"", str, tc.sigedText)
}
sig := c.(*client).signiture(str, hds)
if sig != tc.sig {
t.Errorf("Client.signiture() is \"%v\", want \"%v\"", sig, tc.sig)
}
auth := c.(*client).authorization(sig, hds)
if auth != tc.authorization {
t.Errorf("Client.authorization() is \"%v\", want \"%v\"", auth, tc.authorization)
}
}
}
/* Copyright 2019,2020 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/