-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathclient_test.go
More file actions
73 lines (65 loc) · 2.14 KB
/
Copy pathclient_test.go
File metadata and controls
73 lines (65 loc) · 2.14 KB
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
package agent_test
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/aviate-labs/agent-go"
"github.com/aviate-labs/agent-go/principal"
"github.com/fxamacker/cbor/v2"
)
func recordingClient(t *testing.T, opts ...agent.ClientOption) (agent.Client, *string) {
t.Helper()
var gotPath string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
// Minimal replied-call response; ReadState returns the body verbatim.
body, _ := cbor.Marshal(map[string]any{"status": "replied", "certificate": []byte{}})
w.WriteHeader(http.StatusOK)
_, _ = w.Write(body)
}))
t.Cleanup(srv.Close)
host, _ := url.Parse(srv.URL)
c := agent.NewClient(append([]agent.ClientOption{agent.WithHostURL(host)}, opts...)...)
return c, &gotPath
}
func TestClientCallDefaultsToV4(t *testing.T) {
c, gotPath := recordingClient(t)
cid := principal.MustDecode("aaaaa-aa")
if _, err := c.Call(context.Background(), cid, nil); err != nil {
t.Fatal(err)
}
want := "/api/v4/canister/" + cid.Encode() + "/call"
if *gotPath != want {
t.Fatalf("got %q, want %q", *gotPath, want)
}
}
func TestClientReadStateDefaultsToV3(t *testing.T) {
c, gotPath := recordingClient(t)
cid := principal.MustDecode("aaaaa-aa")
if _, err := c.ReadState(context.Background(), cid, nil); err != nil {
t.Fatal(err)
}
want := "/api/v3/canister/" + cid.Encode() + "/read_state"
if *gotPath != want {
t.Fatalf("got %q, want %q", *gotPath, want)
}
}
func TestClientLegacyAPI(t *testing.T) {
cid := principal.MustDecode("aaaaa-aa")
cCall, callPath := recordingClient(t, agent.WithLegacyAPI())
if _, err := cCall.Call(context.Background(), cid, nil); err != nil {
t.Fatal(err)
}
if want := "/api/v3/canister/" + cid.Encode() + "/call"; *callPath != want {
t.Fatalf("call: got %q, want %q", *callPath, want)
}
cRead, readPath := recordingClient(t, agent.WithLegacyAPI())
if _, err := cRead.ReadState(context.Background(), cid, nil); err != nil {
t.Fatal(err)
}
if want := "/api/v2/canister/" + cid.Encode() + "/read_state"; *readPath != want {
t.Fatalf("read_state: got %q, want %q", *readPath, want)
}
}