This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
status_test.go
131 lines (107 loc) · 3.02 KB
/
status_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
package w3s
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/ipfs/go-cid"
)
var statusHelloCarHandler = func(w http.ResponseWriter, r *http.Request) {
carbytes, err := hex.DecodeString(helloCarHex)
if err != nil {
fmt.Printf("DecodeString: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
status := map[string]interface{}{
"cid": helloRoot,
"dagSize": len(carbytes),
"created": "2022-02-20T15:04:05.999Z",
"pins": []interface{}{},
"deals": []interface{}{},
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(status)
}
func TestStatusHappyPath(t *testing.T) {
routes := routeMap{
"/status/" + helloRoot: {
http.MethodGet: statusHelloCarHandler,
},
}
hc, cleanup := startTestServer(t, routes)
defer cleanup()
client, err := NewClient(WithHTTPClient(hc), WithToken("validtoken"))
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
c, _ := cid.Parse(helloRoot)
st, err := client.Status(context.Background(), c)
if err != nil {
t.Fatalf("failed to send request: %v", err)
}
if st.Cid.String() != helloRoot {
t.Fatalf("got cid %s, wanted %s", st.Cid.String(), helloRoot)
}
if st.DagSize != 208 {
t.Fatalf("got dagsize %d, wanted %d", st.DagSize, 208)
}
}
var statusIncompleteDealHandler = func(w http.ResponseWriter, r *http.Request) {
carbytes, err := hex.DecodeString(helloCarHex)
if err != nil {
fmt.Printf("DecodeString: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
status := map[string]interface{}{
"cid": helloRoot,
"dagSize": len(carbytes),
"created": "2022-02-20T15:04:05.999Z",
"pins": []interface{}{},
"deals": []map[string]interface{}{
{
"dealId": 0,
"storageProvider": "",
"status": "Queued",
"pieceCid": "baga6ea4seaql2leesalhfwmb5xbvhrotu4x76mxn25wcpile6qpbn777omdmqfa",
"dataCid": "bafybeieesr6dknelbduxqmmtt7q2nernm4mmka4k66t7f4d6sgsq3ixc4u",
"dataModelSelector": "Links/144/Hash/Links/2/Hash/Links/0/Hash",
"activation": "",
"expiration": "",
"created": "",
"updated": "",
},
},
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(status)
}
func TestStatusIncompleteDeal(t *testing.T) {
routes := routeMap{
"/status/" + helloRoot: {
http.MethodGet: statusIncompleteDealHandler,
},
}
hc, cleanup := startTestServer(t, routes)
defer cleanup()
client, err := NewClient(WithHTTPClient(hc), WithToken("validtoken"))
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
c, _ := cid.Parse(helloRoot)
st, err := client.Status(context.Background(), c)
if err != nil {
t.Fatalf("failed to send request: %v", err)
}
if st.Cid.String() != helloRoot {
t.Fatalf("got cid %s, wanted %s", st.Cid.String(), helloRoot)
}
if st.DagSize != 208 {
t.Fatalf("got dagsize %d, wanted %d", st.DagSize, 208)
}
}