forked from terrorizer1980/go-coinbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
74 lines (64 loc) · 1.84 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
package coinbase
import (
"testing"
"net/http"
"net/http/httptest"
"crypto/sha256"
"crypto/hmac"
"fmt"
)
func TestClientFetch(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"Data":{"Epoch":1485347945}}`)
}))
defer ts.Close()
a := APIClient{
Key: "123",
Secret: "123456",
Endpoint: ts.URL,
}
var result APIClientEpoch
err := a.Fetch("GET", "/v2/time", nil, &result)
if err != nil {
t.Error("Expected nil, got ", err.Error())
}
if result.Data.Epoch != 1485347945 {
t.Error("Expected valid unix timestamp, got ", result.Data.Epoch)
}
}
func TestClientAuthenticate(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"Data":{"Epoch":1485347945}}`)
}))
defer ts.Close()
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Error("Expected nil, got ", err.Error())
}
a := APIClient{
Key: "123",
Secret: "123456",
Endpoint: ts.URL,
}
err = a.Authenticate("/", req, nil)
if err != nil {
t.Error("Expected nil, got ", err.Error())
}
key := req.Header.Get("CB-ACCESS-KEY")
if key != "123" {
t.Error("Expected key to be 123, got ", key)
}
signatureHeader := []byte(req.Header.Get("CB-ACCESS-SIGN"))
h := hmac.New(sha256.New, []byte("123456"))
h.Write([]byte("1485347945GET/"))
signature := fmt.Sprintf("%x", h.Sum(nil))
if !hmac.Equal(signatureHeader, []byte(signature)) {
t.Error("Expected equal signatures, got different ones")
}
timestamp := req.Header.Get("CB-ACCESS-TIMESTAMP")
if timestamp != "1485347945" {
t.Error("Expected valid unix timestamp, got ", timestamp)
}
}