-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticker_test.go
98 lines (86 loc) · 2.24 KB
/
ticker_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
package coincheck
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestClientGetTicker(t *testing.T) {
t.Run("In the case of a successful GET /api/ticker request", func(t *testing.T) {
// Create a new test server
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wantMethod := http.MethodGet
if diff := cmp.Diff(wantMethod, r.Method); diff != "" {
printDiff(t, diff)
}
wantEndpoint := "/api/ticker"
if diff := cmp.Diff(wantEndpoint, r.URL.Path); diff != "" {
printDiff(t, diff)
}
wantPair := PairETCJPY
if got := r.URL.Query().Get("pair"); got != wantPair.String() {
t.Errorf("pair: got %v, want %v", got, wantPair)
}
result := GetTickerResponse{
Last: 1000000,
Bid: 999000,
Ask: 1001000,
High: 1002000,
Low: 998000,
Volume: 100,
Timestamp: 1609459200,
}
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Fatal(err)
}
}))
defer testServer.Close()
// Create a new client
client, err := NewClient(WithBaseURL(testServer.URL))
if err != nil {
t.Fatal(err)
}
// Start testing
input := GetTickerInput{
Pair: PairETCJPY,
}
got, err := client.GetTicker(context.Background(), input)
if err != nil {
t.Fatal(err)
}
// Check the result
want := &GetTickerResponse{
Last: 1000000,
Bid: 999000,
Ask: 1001000,
High: 1002000,
Low: 998000,
Volume: 100,
Timestamp: 1609459200,
}
if diff := cmp.Diff(want, got); diff != "" {
printDiff(t, diff)
}
})
t.Run("In the case of a failed GET /api/ticker request", func(t *testing.T) {
// Create a new test server
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer testServer.Close()
// Create a new client
client, err := NewClient(WithBaseURL(testServer.URL))
if err != nil {
t.Fatal(err)
}
// Start testing
input := GetTickerInput{
Pair: PairETCJPY,
}
if _, err = client.GetTicker(context.Background(), input); err == nil {
t.Error("want error, but got nil")
}
})
}