-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
111 lines (105 loc) · 3.12 KB
/
client_test.go
File metadata and controls
111 lines (105 loc) · 3.12 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
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
package d1http
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func testClient(handler http.HandlerFunc) (*Client, func()) {
srv := httptest.NewServer(handler)
c := New(Config{
AccountID: "acc",
DatabaseID: "db",
APIToken: "tok",
BaseURL: srv.URL,
HTTPClient: srv.Client(),
Retry: RetryConfig{MaxRetries: 1, MinBackoff: time.Millisecond, MaxBackoff: time.Millisecond},
})
return c, srv.Close
}
func TestAuthHeaderAndRawBody(t *testing.T) {
c, closeFn := testClient(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer tok" {
t.Fatalf("missing auth header: %q", r.Header.Get("Authorization"))
}
if r.URL.Path != "/accounts/acc/d1/database/db/raw" {
t.Fatalf("unexpected path: %s", r.URL.Path)
}
var body rawRequest
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
t.Fatal(err)
}
if body.SQL != "SELECT ?" || len(body.Params) != 1 {
t.Fatalf("bad body: %+v", body)
}
_ = json.NewEncoder(w).Encode(envelope[[]RawResult]{
Success: true,
Result: []RawResult{{Success: true, Results: RawRows{Columns: []string{"x"}, Rows: [][]any{{float64(1)}}}}},
})
})
defer closeFn()
res, err := c.Raw(context.Background(), "SELECT ?", 1)
if err != nil {
t.Fatal(err)
}
if len(res) != 1 || len(res[0].First()) != 1 {
t.Fatalf("bad result: %+v", res)
}
}
func TestAPIError(t *testing.T) {
c, closeFn := testClient(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(envelope[json.RawMessage]{
Success: false,
Errors: []ResponseInfo{{Code: 1000, Message: "bad sql"}},
})
})
defer closeFn()
_, err := c.Raw(context.Background(), "bad")
if err == nil {
t.Fatal("expected error")
}
apiErr, ok := err.(*APIError)
if !ok || apiErr.StatusCode != http.StatusBadRequest {
t.Fatalf("expected APIError, got %T %v", err, err)
}
}
func TestRetry429(t *testing.T) {
calls := 0
c, closeFn := testClient(func(w http.ResponseWriter, r *http.Request) {
calls++
if calls == 1 {
w.WriteHeader(http.StatusTooManyRequests)
_ = json.NewEncoder(w).Encode(envelope[json.RawMessage]{Success: false, Errors: []ResponseInfo{{Message: "rate"}}})
return
}
_ = json.NewEncoder(w).Encode(envelope[[]RawResult]{Success: true, Result: []RawResult{{Success: true}}})
})
defer closeFn()
if _, err := c.Raw(context.Background(), "SELECT 1"); err != nil {
t.Fatal(err)
}
if calls != 2 {
t.Fatalf("expected 2 calls, got %d", calls)
}
}
func TestBatchRawJoinsStatements(t *testing.T) {
c, closeFn := testClient(func(w http.ResponseWriter, r *http.Request) {
var body rawRequest
_ = json.NewDecoder(r.Body).Decode(&body)
if body.SQL != "SELECT ?; SELECT ?;" {
t.Fatalf("unexpected sql: %q", body.SQL)
}
if len(body.Params) != 2 {
t.Fatalf("unexpected params: %+v", body.Params)
}
_ = json.NewEncoder(w).Encode(envelope[[]RawResult]{Success: true})
})
defer closeFn()
_, err := c.BatchRaw(context.Background(), []Statement{{SQL: "SELECT ?", Params: []any{1}}, {SQL: "SELECT ?;", Params: []any{2}}})
if err != nil {
t.Fatal(err)
}
}