-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_test.go
132 lines (120 loc) · 2.99 KB
/
http_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
package kraken
import (
"net/url"
"testing"
)
func Test_newFormURLEncodedBody(t *testing.T) {
type args struct {
b url.Values
}
tests := []struct {
name string
args args
wantString string
wantNonce string
wantContentType string
wantValues string
wantErr bool
}{
{
name: "new form body",
args: args{
b: url.Values{
"asset": []string{"XBT"},
"cursor": []string{"cursor"},
"limit": []string{"10"},
},
},
wantNonce: "nonce",
wantContentType: "application/x-www-form-urlencoded; charset=utf-8",
wantValues: "asset=XBT&cursor=cursor&limit=10&nonce=nonce&otp=otp",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := newFormURLEncodedBody(tt.args.b)
got.Values.Set("nonce", "nonce")
got.withOtp("otp")
if got.nonce() != tt.wantNonce {
t.Errorf("newJSONBody() nonce = %v, want %v", got.nonce(), tt.wantNonce)
return
}
if got.contentType() != tt.wantContentType {
t.Errorf("newJSONBody() contentType = %v, want %v", got.contentType(), tt.wantContentType)
return
}
if got.string() != tt.wantValues {
t.Errorf("newJSONBody() string = %v, want %v", got.string(), tt.wantValues)
}
})
}
}
func Test_newJSONBody(t *testing.T) {
type fakeJSON struct {
Asset string `json:"asset"`
Cursor string `json:"cursor"`
Limit int `json:"limit"`
}
type args struct {
b any
}
tests := []struct {
name string
args args
wantString string
wantNonce string
wantContentType string
wantJSON string
wantErr bool
}{
{
name: "new json body as pointer",
args: args{
b: &fakeJSON{
Asset: "XBT",
Cursor: "cursor",
Limit: 10,
},
},
wantNonce: "nonce",
wantContentType: "application/json",
wantJSON: `{"asset":"XBT","cursor":"cursor","limit":10,"nonce":"nonce","otp":"otp"}`,
},
{
name: "new json body as reference",
args: args{
b: fakeJSON{
Asset: "XBT",
Cursor: "cursor",
Limit: 10,
},
},
wantNonce: "nonce",
wantContentType: "application/json",
wantJSON: `{"asset":"XBT","cursor":"cursor","limit":10,"nonce":"nonce","otp":"otp"}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := newJSONBody(tt.args.b)
if (err != nil) != tt.wantErr {
t.Errorf("newJSONBody() error = %v, wantErr %v", err, tt.wantErr)
return
}
// Add fake nonce to the got json body.
got.jsonMessage["nonce"] = "nonce"
got.withOtp("otp")
if got.nonce() != tt.wantNonce {
t.Errorf("newJSONBody() nonce = %v, want %v", got.nonce(), tt.wantNonce)
return
}
if got.contentType() != tt.wantContentType {
t.Errorf("newJSONBody() contentType = %v, want %v", got.contentType(), tt.wantContentType)
return
}
if got.string() != tt.wantJSON {
t.Errorf("newJSONBody() string = %v, want %v", got.string(), tt.wantJSON)
}
})
}
}