-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_options_test.go
More file actions
173 lines (154 loc) · 5.92 KB
/
Copy pathclient_options_test.go
File metadata and controls
173 lines (154 loc) · 5.92 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package instant_test
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/InstaNode-dev/sdk-go/instant"
)
// markerTransport is an http.RoundTripper that stamps a marker header on every
// outbound request and records that it was invoked. Used to prove the SDK's
// New() preserves the caller's Transport instead of silently discarding it.
type markerTransport struct {
marker string
base http.RoundTripper
hits int32
}
func (m *markerTransport) RoundTrip(req *http.Request) (*http.Response, error) {
atomic.AddInt32(&m.hits, 1)
req = req.Clone(req.Context())
req.Header.Set("X-Caller-Marker", m.marker)
base := m.base
if base == nil {
base = http.DefaultTransport
}
return base.RoundTrip(req)
}
// TestWithHTTPClient_PreservesTransport pins B17-P0: the caller's Transport
// MUST be chained underneath the SDK's auth transport so OTel injection,
// custom TLS, proxy injection, and other RoundTripper wrappers keep working.
//
// Before the fix, WithHTTPClient kept only the caller's Timeout and discarded
// the rest of the *http.Client — including Transport. The server below would
// have observed no X-Caller-Marker header and the test would fail.
func TestWithHTTPClient_PreservesTransport(t *testing.T) {
var sawMarker, sawUserAgent, sawAuth string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sawMarker = r.Header.Get("X-Caller-Marker")
sawUserAgent = r.Header.Get("User-Agent")
sawAuth = r.Header.Get("Authorization")
writeJSON(w, http.StatusOK, map[string]any{
"ok": true,
"items": []any{},
"total": 0,
})
}))
t.Cleanup(srv.Close)
caller := &markerTransport{marker: "trace-id-abc"}
hc := &http.Client{
Timeout: 17 * time.Second,
Transport: caller,
}
client := instant.New(
instant.WithBaseURL(srv.URL),
instant.WithAPIKey("sk-test"),
instant.WithHTTPClient(hc),
)
if _, err := client.ListResources(context.Background()); err != nil {
t.Fatalf("ListResources: %v", err)
}
if got := atomic.LoadInt32(&caller.hits); got != 1 {
t.Fatalf("caller transport invocations = %d, want 1 (caller Transport was discarded)", got)
}
if sawMarker != "trace-id-abc" {
t.Fatalf("server saw X-Caller-Marker = %q, want %q (caller Transport was discarded)", sawMarker, "trace-id-abc")
}
if !strings.HasPrefix(sawUserAgent, "instant-go-sdk/") {
t.Errorf("User-Agent = %q, want prefix instant-go-sdk/ (auth transport not layered)", sawUserAgent)
}
if sawAuth != "Bearer sk-test" {
t.Errorf("Authorization = %q, want Bearer sk-test (auth transport not layered)", sawAuth)
}
}
// TestWithHTTPClient_PreservesTimeout pins that the caller's Timeout still
// survives the transport-chaining rewrite. Proven indirectly by the slow
// server hitting the caller's tiny timeout — the request must error with a
// timeout, NOT succeed.
func TestWithHTTPClient_PreservesTimeout(t *testing.T) {
// Server stalls for longer than the caller's Timeout.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(200 * time.Millisecond)
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "items": []any{}, "total": 0})
}))
t.Cleanup(srv.Close)
hc := &http.Client{Timeout: 30 * time.Millisecond}
client := instant.New(instant.WithBaseURL(srv.URL), instant.WithHTTPClient(hc))
_, err := client.ListResources(context.Background())
if err == nil {
t.Fatalf("ListResources should have timed out — caller Timeout was discarded")
}
}
// TestWithHTTPClient_NilIsNoop pins that passing a nil client doesn't blow up
// later when the SDK tries to wrap it. The default 30s timeout should remain,
// proven indirectly by a normal request succeeding.
func TestWithHTTPClient_NilIsNoop(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "items": []any{}, "total": 0})
}))
t.Cleanup(srv.Close)
client := instant.New(instant.WithBaseURL(srv.URL), instant.WithHTTPClient(nil))
if _, err := client.ListResources(context.Background()); err != nil {
t.Errorf("nil hc should be a no-op, ListResources errored: %v", err)
}
}
// TestWithHTTPClient_NestedTransports pins that nested Transport wrappers
// (an OTel-style RoundTripper that itself wraps another RoundTripper) chain
// correctly. Each layer should see the request before the wire.
func TestWithHTTPClient_NestedTransports(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, map[string]any{
"ok": true,
"items": []any{},
"total": 0,
})
}))
t.Cleanup(srv.Close)
inner := &markerTransport{marker: "inner"}
outer := &markerTransport{marker: "outer", base: inner}
hc := &http.Client{Transport: outer}
client := instant.New(instant.WithBaseURL(srv.URL), instant.WithHTTPClient(hc))
if _, err := client.ListResources(context.Background()); err != nil {
t.Fatalf("ListResources: %v", err)
}
if got := atomic.LoadInt32(&outer.hits); got != 1 {
t.Fatalf("outer transport hits = %d, want 1", got)
}
if got := atomic.LoadInt32(&inner.hits); got != 1 {
t.Fatalf("inner transport hits = %d, want 1 (chain broken)", got)
}
}
// TestSDKVersionInUserAgent pins the User-Agent format and that the version
// comes from the SDKVersion constant — the source of truth.
func TestSDKVersionInUserAgent(t *testing.T) {
var ua string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ua = r.Header.Get("User-Agent")
writeJSON(w, http.StatusOK, map[string]any{
"ok": true,
"items": []any{},
"total": 0,
})
}))
t.Cleanup(srv.Close)
client := instant.New(instant.WithBaseURL(srv.URL))
if _, err := client.ListResources(context.Background()); err != nil {
t.Fatalf("ListResources: %v", err)
}
want := "instant-go-sdk/" + instant.SDKVersion
if ua != want {
t.Errorf("User-Agent = %q, want %q", ua, want)
}
}