@voxgig/sdkgen 4.49.1 → 4.51.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/voxgig-sdkgen +1 -1
- package/package.json +1 -1
- package/project/.sdk/model/feature/cache.aontu +6 -4
- package/project/.sdk/model/target/lua.aontu +6 -0
- package/project/.sdk/src/cmp/c/Config_c.ts +3 -134
- package/project/.sdk/src/cmp/c/TestClean_c.ts +14 -0
- package/project/.sdk/src/cmp/clojure/TestClean_clojure.ts +10 -5
- package/project/.sdk/src/cmp/cpp/TestClean_cpp.ts +13 -0
- package/project/.sdk/src/cmp/csharp/TestClean_csharp.ts +9 -0
- package/project/.sdk/src/cmp/elixir/TestClean_elixir.ts +11 -3
- package/project/.sdk/src/cmp/go/TestClean_go.ts +39 -3
- package/project/.sdk/src/cmp/java/TestClean_java.ts +8 -0
- package/project/.sdk/src/cmp/js/TestClean_js.ts +13 -5
- package/project/.sdk/src/cmp/kotlin/TestClean_kotlin.ts +22 -0
- package/project/.sdk/src/cmp/lua/TestClean_lua.ts +12 -3
- package/project/.sdk/src/cmp/ocaml/TestClean_ocaml.ts +18 -4
- package/project/.sdk/src/cmp/perl/TestClean_perl.ts +12 -3
- package/project/.sdk/src/cmp/php/TestClean_php.ts +10 -3
- package/project/.sdk/src/cmp/py/TestClean_py.ts +10 -3
- package/project/.sdk/src/cmp/rb/TestClean_rb.ts +10 -3
- package/project/.sdk/src/cmp/rust/TestClean_rust.ts +10 -0
- package/project/.sdk/src/cmp/scala/TestClean_scala.ts +34 -0
- package/project/.sdk/src/cmp/swift/TestClean_swift.ts +20 -10
- package/project/.sdk/src/cmp/ts/TestClean_ts.ts +13 -5
- package/project/.sdk/src/cmp/zig/TestClean_zig.ts +12 -0
- package/project/.sdk/tm/c/core/context.c +1 -0
- package/project/.sdk/tm/c/core/sdk.h +7 -4
- package/project/.sdk/tm/c/feature/cache.c +243 -13
- package/project/.sdk/tm/c/feature/debug.c +1 -0
- package/project/.sdk/tm/c/feature/secrets.c +27 -38
- package/project/.sdk/tm/c/feature/telemetry.c +1 -0
- package/project/.sdk/tm/c/tests/feature_test.c +142 -0
- package/project/.sdk/tm/c/utility/clean.c +26 -33
- package/project/.sdk/tm/c/utility/make_options.c +9 -8
- package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +14 -11
- package/project/.sdk/tm/clojure/src/sdk/client.clj +3 -1
- package/project/.sdk/tm/clojure/src/sdk/core.clj +19 -11
- package/project/.sdk/tm/clojure/src/sdk/features.clj +83 -10
- package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +47 -0
- package/project/.sdk/tm/clojure/test/sdk/test/pipeline.clj +1 -0
- package/project/.sdk/tm/cpp/Makefile +5 -0
- package/project/.sdk/tm/cpp/core/types.hpp +11 -4
- package/project/.sdk/tm/cpp/feature/audit.hpp +6 -7
- package/project/.sdk/tm/cpp/feature/cache.hpp +188 -9
- package/project/.sdk/tm/cpp/feature/debug.hpp +1 -0
- package/project/.sdk/tm/cpp/feature/secrets.hpp +12 -0
- package/project/.sdk/tm/cpp/feature/telemetry.hpp +1 -0
- package/project/.sdk/tm/cpp/test/feature_test.cpp +128 -0
- package/project/.sdk/tm/cpp/utility/pipeline.hpp +9 -7
- package/project/.sdk/tm/csharp/core/Context.cs +4 -0
- package/project/.sdk/tm/csharp/feature/CacheFeature.cs +126 -12
- package/project/.sdk/tm/csharp/feature/DebugFeature.cs +1 -0
- package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +9 -27
- package/project/.sdk/tm/csharp/feature/TelemetryFeature.cs +1 -0
- package/project/.sdk/tm/csharp/test/FeatureTest.cs +101 -0
- package/project/.sdk/tm/csharp/utility/Clean.cs +5 -10
- package/project/.sdk/tm/csharp/utility/Fetcher.cs +7 -1
- package/project/.sdk/tm/csharp/utility/MakeOptions.cs +5 -12
- package/project/.sdk/tm/elixir/lib/projectname/context.ex +2 -0
- package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +146 -30
- package/project/.sdk/tm/elixir/lib/projectname/feature/debug.ex +1 -0
- package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +10 -56
- package/project/.sdk/tm/elixir/lib/projectname/feature/telemetry.ex +1 -0
- package/project/.sdk/tm/elixir/lib/projectname/utility.ex +18 -12
- package/project/.sdk/tm/elixir/test/feature_test.exs +84 -0
- package/project/.sdk/tm/go/core/context.go +6 -0
- package/project/.sdk/tm/go/feature/cache_feature.go +131 -4
- package/project/.sdk/tm/go/feature/debug_feature.go +3 -2
- package/project/.sdk/tm/go/feature/secrets_feature.go +17 -71
- package/project/.sdk/tm/go/feature/telemetry_feature.go +5 -4
- package/project/.sdk/tm/go/test/feature_corpus_test.go +150 -2
- package/project/.sdk/tm/go/test/feature_test.go +101 -0
- package/project/.sdk/tm/go/utility/clean.go +5 -7
- package/project/.sdk/tm/go/utility/fetcher.go +11 -0
- package/project/.sdk/tm/go/utility/make_options.go +5 -7
- package/project/.sdk/tm/java/core/Context.java +9 -0
- package/project/.sdk/tm/java/feature/CacheFeature.java +123 -17
- package/project/.sdk/tm/java/feature/DebugFeature.java +1 -0
- package/project/.sdk/tm/java/feature/SecretsFeature.java +9 -92
- package/project/.sdk/tm/java/feature/TelemetryFeature.java +1 -0
- package/project/.sdk/tm/java/test/FeatureCorpusTest.java +141 -2
- package/project/.sdk/tm/java/test/FeatureTest.java +88 -0
- package/project/.sdk/tm/java/utility/Clean.java +6 -10
- package/project/.sdk/tm/java/utility/Fetcher.java +5 -1
- package/project/.sdk/tm/java/utility/MakeOptions.java +3 -1
- package/project/.sdk/tm/js/src/Context.js +4 -0
- package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +118 -11
- package/project/.sdk/tm/js/src/feature/cost/CostFeature.js +3 -2
- package/project/.sdk/tm/js/src/feature/debug/DebugFeature.js +1 -0
- package/project/.sdk/tm/js/src/feature/metrics/MetricsFeature.js +1 -1
- package/project/.sdk/tm/js/src/feature/paging/PagingFeature.js +4 -4
- package/project/.sdk/tm/js/src/feature/rbac/RbacFeature.js +4 -4
- package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +16 -6
- package/project/.sdk/tm/js/src/feature/telemetry/TelemetryFeature.js +1 -0
- package/project/.sdk/tm/js/src/feature/validate/ValidateFeature.js +2 -1
- package/project/.sdk/tm/js/src/utility/CleanUtility.js +1 -1
- package/project/.sdk/tm/js/src/utility/MakeOptionsUtility.js +6 -6
- package/project/.sdk/tm/js/test/feature/Corpus.test.js +100 -2
- package/project/.sdk/tm/js/test/feature/harness.js +3 -3
- package/project/.sdk/tm/js/test/feature.test.js +17 -0
- package/project/.sdk/tm/js/test/pipeline.test.js +1 -1
- package/project/.sdk/tm/kotlin/core/Context.kt +3 -0
- package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +102 -7
- package/project/.sdk/tm/kotlin/feature/DebugFeature.kt +1 -0
- package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +10 -20
- package/project/.sdk/tm/kotlin/feature/TelemetryFeature.kt +1 -0
- package/project/.sdk/tm/kotlin/test/FeatureTest.kt +79 -0
- package/project/.sdk/tm/kotlin/utility/CoreUtil.kt +5 -5
- package/project/.sdk/tm/kotlin/utility/Fetcher.kt +4 -0
- package/project/.sdk/tm/kotlin/utility/MakeOptions.kt +3 -1
- package/project/.sdk/tm/lua/core/context.lua +6 -0
- package/project/.sdk/tm/lua/feature/cache_feature.lua +212 -15
- package/project/.sdk/tm/lua/feature/debug_feature.lua +1 -0
- package/project/.sdk/tm/lua/feature/secrets_feature.lua +15 -80
- package/project/.sdk/tm/lua/feature/telemetry_feature.lua +1 -0
- package/project/.sdk/tm/lua/test/feature_test.lua +108 -0
- package/project/.sdk/tm/lua/utility/clean.lua +6 -9
- package/project/.sdk/tm/lua/utility/fetcher.lua +9 -0
- package/project/.sdk/tm/lua/utility/make_options.lua +5 -5
- package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +21 -6
- package/project/.sdk/tm/ocaml/sdk_features.ml +108 -8
- package/project/.sdk/tm/ocaml/sdk_runtime.ml +13 -10
- package/project/.sdk/tm/ocaml/sdk_types.ml +2 -0
- package/project/.sdk/tm/ocaml/test/t_feature.ml +51 -1
- package/project/.sdk/tm/perl/core/context.pm +3 -0
- package/project/.sdk/tm/perl/feature/audit_feature.pm +4 -7
- package/project/.sdk/tm/perl/feature/cache_feature.pm +111 -15
- package/project/.sdk/tm/perl/feature/debug_feature.pm +1 -0
- package/project/.sdk/tm/perl/feature/secrets_feature.pm +13 -18
- package/project/.sdk/tm/perl/feature/telemetry_feature.pm +1 -0
- package/project/.sdk/tm/perl/t/feature.t +86 -0
- package/project/.sdk/tm/perl/t/feature_corpus.t +80 -4
- package/project/.sdk/tm/perl/utility/clean.pm +6 -8
- package/project/.sdk/tm/perl/utility/fetcher.pm +4 -0
- package/project/.sdk/tm/perl/utility/make_options.pm +5 -5
- package/project/.sdk/tm/php/core/Context.php +4 -0
- package/project/.sdk/tm/php/feature/CacheFeature.php +98 -13
- package/project/.sdk/tm/php/feature/DebugFeature.php +1 -0
- package/project/.sdk/tm/php/feature/SecretsFeature.php +18 -15
- package/project/.sdk/tm/php/feature/TelemetryFeature.php +1 -0
- package/project/.sdk/tm/php/test/FeatureCorpusTest.php +113 -3
- package/project/.sdk/tm/php/test/FeatureTest.php +90 -0
- package/project/.sdk/tm/php/utility/Clean.php +3 -10
- package/project/.sdk/tm/php/utility/Fetcher.php +8 -3
- package/project/.sdk/tm/php/utility/MakeOptions.php +5 -7
- package/project/.sdk/tm/py/pkg/core/context.py +4 -0
- package/project/.sdk/tm/py/pkg/feature/cache_feature.py +79 -13
- package/project/.sdk/tm/py/pkg/feature/debug_feature.py +1 -0
- package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +15 -66
- package/project/.sdk/tm/py/pkg/feature/telemetry_feature.py +1 -0
- package/project/.sdk/tm/py/pkg/utility/clean.py +4 -10
- package/project/.sdk/tm/py/pkg/utility/make_options.py +8 -9
- package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +23 -9
- package/project/.sdk/tm/py/test/test_feature.py +73 -0
- package/project/.sdk/tm/py/test/test_feature_corpus.py +88 -3
- package/project/.sdk/tm/rb/core/context.rb +4 -1
- package/project/.sdk/tm/rb/feature/audit_feature.rb +6 -5
- package/project/.sdk/tm/rb/feature/cache_feature.rb +75 -11
- package/project/.sdk/tm/rb/feature/debug_feature.rb +1 -0
- package/project/.sdk/tm/rb/feature/secrets_feature.rb +9 -64
- package/project/.sdk/tm/rb/feature/telemetry_feature.rb +1 -0
- package/project/.sdk/tm/rb/test/feature_corpus_test.rb +85 -3
- package/project/.sdk/tm/rb/test/feature_test.rb +93 -0
- package/project/.sdk/tm/rb/utility/clean.rb +6 -7
- package/project/.sdk/tm/rb/utility/fetcher.rb +6 -0
- package/project/.sdk/tm/rb/utility/make_options.rb +5 -5
- package/project/.sdk/tm/rust/core/context.rs +8 -0
- package/project/.sdk/tm/rust/feature/cache.rs +120 -3
- package/project/.sdk/tm/rust/feature/debug.rs +1 -0
- package/project/.sdk/tm/rust/feature/secrets.rs +36 -98
- package/project/.sdk/tm/rust/feature/telemetry.rs +1 -0
- package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +3 -3
- package/project/.sdk/tm/rust/tests/feature_test.rs +141 -0
- package/project/.sdk/tm/rust/utility/clean.rs +35 -43
- package/project/.sdk/tm/rust/utility/fetcher.rs +6 -0
- package/project/.sdk/tm/rust/utility/make_options.rs +8 -11
- package/project/.sdk/tm/scala/core/Context.scala +4 -0
- package/project/.sdk/tm/scala/feature/CacheFeature.scala +117 -11
- package/project/.sdk/tm/scala/feature/DebugFeature.scala +1 -0
- package/project/.sdk/tm/scala/feature/SecretsFeature.scala +11 -24
- package/project/.sdk/tm/scala/feature/TelemetryFeature.scala +1 -0
- package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +52 -1
- package/project/.sdk/tm/scala/utility/Fetcher.scala +5 -0
- package/project/.sdk/tm/scala/utility/Make.scala +3 -1
- package/project/.sdk/tm/scala/utility/Misc.scala +5 -8
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Context.swift +3 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +122 -11
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/DebugFeature.swift +1 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +9 -13
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/TelemetryFeature.swift +1 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Fetcher.swift +4 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/MakeOptions.swift +5 -4
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/ResultUtil.swift +4 -5
- package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +79 -0
- package/project/.sdk/tm/ts/src/Context.ts +4 -0
- package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +115 -4
- package/project/.sdk/tm/ts/src/feature/cost/CostFeature.ts +3 -2
- package/project/.sdk/tm/ts/src/feature/debug/DebugFeature.ts +1 -0
- package/project/.sdk/tm/ts/src/feature/metrics/MetricsFeature.ts +1 -1
- package/project/.sdk/tm/ts/src/feature/paging/PagingFeature.ts +4 -4
- package/project/.sdk/tm/ts/src/feature/rbac/RbacFeature.ts +4 -4
- package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +14 -2
- package/project/.sdk/tm/ts/src/feature/telemetry/TelemetryFeature.ts +1 -0
- package/project/.sdk/tm/ts/src/feature/validate/ValidateFeature.ts +2 -1
- package/project/.sdk/tm/ts/src/utility/CleanUtility.ts +1 -1
- package/project/.sdk/tm/ts/src/utility/MakeOptionsUtility.ts +6 -6
- package/project/.sdk/tm/ts/test/feature/Corpus.test.ts +101 -2
- package/project/.sdk/tm/ts/test/feature/harness.ts +3 -3
- package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
- package/project/.sdk/tm/ts/test/pipeline.test.ts +1 -1
- package/project/.sdk/tm/zig/core/context.zig +5 -0
- package/project/.sdk/tm/zig/core/utility.zig +11 -9
- package/project/.sdk/tm/zig/feature/cache.zig +144 -13
- package/project/.sdk/tm/zig/feature/debug.zig +1 -0
- package/project/.sdk/tm/zig/feature/secrets.zig +6 -1
- package/project/.sdk/tm/zig/feature/telemetry.zig +1 -0
- package/project/.sdk/tm/zig/test/feature_test.zig +100 -0
- package/project/sdkgen-package.json +1 -1
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
package feature
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
|
+
"crypto/hmac"
|
|
5
|
+
"crypto/rand"
|
|
6
|
+
"crypto/sha256"
|
|
7
|
+
"encoding/hex"
|
|
8
|
+
"fmt"
|
|
9
|
+
"sort"
|
|
10
|
+
"strconv"
|
|
4
11
|
"strings"
|
|
5
12
|
|
|
6
13
|
"GOMODULE/core"
|
|
@@ -13,6 +20,7 @@ type CacheFeature struct {
|
|
|
13
20
|
options map[string]any
|
|
14
21
|
store map[string]*cacheEntry
|
|
15
22
|
order []string
|
|
23
|
+
secret []byte
|
|
16
24
|
|
|
17
25
|
// Activity tracking (mirrors the ts client._cache record).
|
|
18
26
|
Hit int
|
|
@@ -22,6 +30,7 @@ type CacheFeature struct {
|
|
|
22
30
|
|
|
23
31
|
type cacheEntry struct {
|
|
24
32
|
expiry int64
|
|
33
|
+
scope string
|
|
25
34
|
snapshot *cacheSnapshot
|
|
26
35
|
}
|
|
27
36
|
|
|
@@ -53,6 +62,10 @@ func (f *CacheFeature) Init(ctx *core.Context, options map[string]any) {
|
|
|
53
62
|
|
|
54
63
|
f.store = map[string]*cacheEntry{}
|
|
55
64
|
f.order = []string{}
|
|
65
|
+
f.secret = make([]byte, 32)
|
|
66
|
+
if _, err := rand.Read(f.secret); err != nil {
|
|
67
|
+
panic(err)
|
|
68
|
+
}
|
|
56
69
|
|
|
57
70
|
inner := ctx.Utility.Fetcher
|
|
58
71
|
|
|
@@ -81,10 +94,14 @@ func (f *CacheFeature) through(ctx *core.Context, url string, fetchdef map[strin
|
|
|
81
94
|
}
|
|
82
95
|
}
|
|
83
96
|
if !cacheable {
|
|
84
|
-
|
|
97
|
+
res, err := inner(ctx, url, fetchdef)
|
|
98
|
+
if method != "GET" && method != "HEAD" {
|
|
99
|
+
f.invalidate(url)
|
|
100
|
+
}
|
|
101
|
+
return res, err
|
|
85
102
|
}
|
|
86
103
|
|
|
87
|
-
key := method + " " + url
|
|
104
|
+
key := method + " " + url + " " + f.digest(fetchdef["headers"])
|
|
88
105
|
now := foptNow(f.options)()
|
|
89
106
|
|
|
90
107
|
if hit, ok := f.store[key]; ok && hit.expiry > now {
|
|
@@ -96,9 +113,14 @@ func (f *CacheFeature) through(ctx *core.Context, url string, fetchdef map[strin
|
|
|
96
113
|
|
|
97
114
|
if err == nil && f.storable(res) {
|
|
98
115
|
snapshot := f.snapshot(res)
|
|
99
|
-
ttl := foptInt(f.options, "ttl", 5000)
|
|
116
|
+
ttl := cacheLifetime(snapshot.headers, int64(foptInt(f.options, "ttl", 5000)))
|
|
117
|
+
if ttl <= 0 {
|
|
118
|
+
f.Bypass++
|
|
119
|
+
return f.replay(snapshot), nil
|
|
120
|
+
}
|
|
121
|
+
f.drop(key)
|
|
100
122
|
f.evict()
|
|
101
|
-
f.store[key] = &cacheEntry{expiry: now +
|
|
123
|
+
f.store[key] = &cacheEntry{expiry: now + ttl, scope: cacheScope(url), snapshot: snapshot}
|
|
102
124
|
f.order = append(f.order, key)
|
|
103
125
|
f.Miss++
|
|
104
126
|
return f.replay(snapshot), nil
|
|
@@ -108,6 +130,111 @@ func (f *CacheFeature) through(ctx *core.Context, url string, fetchdef map[strin
|
|
|
108
130
|
return res, err
|
|
109
131
|
}
|
|
110
132
|
|
|
133
|
+
func (f *CacheFeature) digest(headers any) string {
|
|
134
|
+
pairs := [][2]string{}
|
|
135
|
+
if hm, ok := headers.(map[string]any); ok {
|
|
136
|
+
for k, v := range hm {
|
|
137
|
+
if v != nil {
|
|
138
|
+
pairs = append(pairs, [2]string{strings.ToLower(k), fmt.Sprint(v)})
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
} else if hm, ok := headers.(map[string]string); ok {
|
|
142
|
+
for k, v := range hm {
|
|
143
|
+
pairs = append(pairs, [2]string{strings.ToLower(k), v})
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
sort.Slice(pairs, func(i, j int) bool {
|
|
147
|
+
if pairs[i][0] != pairs[j][0] {
|
|
148
|
+
return pairs[i][0] < pairs[j][0]
|
|
149
|
+
}
|
|
150
|
+
return pairs[i][1] < pairs[j][1]
|
|
151
|
+
})
|
|
152
|
+
mac := hmac.New(sha256.New, f.secret)
|
|
153
|
+
for _, p := range pairs {
|
|
154
|
+
mac.Write([]byte(p[0] + ":" + p[1] + "\n"))
|
|
155
|
+
}
|
|
156
|
+
return hex.EncodeToString(mac.Sum(nil))
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
func (f *CacheFeature) invalidate(url string) {
|
|
160
|
+
path := cacheScope(url)
|
|
161
|
+
collection := cacheParent(path)
|
|
162
|
+
for key, entry := range f.store {
|
|
163
|
+
if cacheUnder(entry.scope, path) || entry.scope == collection {
|
|
164
|
+
f.drop(key)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
func (f *CacheFeature) drop(key string) {
|
|
170
|
+
if _, ok := f.store[key]; !ok {
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
delete(f.store, key)
|
|
174
|
+
for i, k := range f.order {
|
|
175
|
+
if k == key {
|
|
176
|
+
f.order = append(f.order[:i], f.order[i+1:]...)
|
|
177
|
+
break
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// The configured ttl capped by the response's Cache-Control; zero when the
|
|
183
|
+
// response may not be stored.
|
|
184
|
+
func cacheLifetime(headers map[string]any, ttl int64) int64 {
|
|
185
|
+
control := ""
|
|
186
|
+
for k, v := range headers {
|
|
187
|
+
if strings.ToLower(k) == "cache-control" {
|
|
188
|
+
control = strings.ToLower(fmt.Sprint(v))
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
life := ttl
|
|
192
|
+
for _, part := range strings.Split(control, ",") {
|
|
193
|
+
directive := strings.TrimSpace(part)
|
|
194
|
+
switch directive {
|
|
195
|
+
case "no-store", "private", "no-cache":
|
|
196
|
+
return 0
|
|
197
|
+
}
|
|
198
|
+
if strings.HasPrefix(directive, "max-age") {
|
|
199
|
+
value := strings.TrimSpace(strings.TrimPrefix(directive, "max-age"))
|
|
200
|
+
if strings.HasPrefix(value, "=") {
|
|
201
|
+
value = strings.Trim(strings.TrimSpace(value[1:]), "\"")
|
|
202
|
+
if secs, err := strconv.ParseInt(value, 10, 64); err == nil && secs >= 0 && secs*1000 < life {
|
|
203
|
+
life = secs * 1000
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return life
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
func cacheScope(url string) string {
|
|
212
|
+
if i := strings.IndexAny(url, "?#"); i >= 0 {
|
|
213
|
+
url = url[:i]
|
|
214
|
+
}
|
|
215
|
+
return strings.TrimRight(url, "/")
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
func cacheParent(scope string) string {
|
|
219
|
+
root := 0
|
|
220
|
+
if scheme := strings.Index(scope, "://"); scheme >= 0 {
|
|
221
|
+
root = strings.Index(scope[scheme+3:], "/")
|
|
222
|
+
if root < 0 {
|
|
223
|
+
return scope
|
|
224
|
+
}
|
|
225
|
+
root += scheme + 3
|
|
226
|
+
}
|
|
227
|
+
cut := strings.LastIndex(scope, "/")
|
|
228
|
+
if cut < root {
|
|
229
|
+
cut = root
|
|
230
|
+
}
|
|
231
|
+
return scope[:cut]
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
func cacheUnder(scope string, prefix string) bool {
|
|
235
|
+
return scope == prefix || strings.HasPrefix(scope, prefix+"/")
|
|
236
|
+
}
|
|
237
|
+
|
|
111
238
|
// A body the transport could not read as JSON passes through, for the operation to report.
|
|
112
239
|
// A body that declares another type than JSON passes by, unread.
|
|
113
240
|
func (f *CacheFeature) storable(res any) bool {
|
|
@@ -46,8 +46,9 @@ func (f *DebugFeature) PreRequest(ctx *core.Context) {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
entry := map[string]any{
|
|
49
|
-
"op":
|
|
50
|
-
"
|
|
49
|
+
"op": entity + "." + opname,
|
|
50
|
+
"correlationId": ctx.Id,
|
|
51
|
+
"start": foptNow(f.options)(),
|
|
51
52
|
}
|
|
52
53
|
if ctx.Spec != nil {
|
|
53
54
|
entry["method"] = ctx.Spec.Method
|
|
@@ -4,22 +4,21 @@ import (
|
|
|
4
4
|
"encoding/base64"
|
|
5
5
|
"encoding/json"
|
|
6
6
|
"fmt"
|
|
7
|
-
"io"
|
|
8
|
-
"net/http"
|
|
9
7
|
neturl "net/url"
|
|
10
8
|
"strconv"
|
|
11
9
|
"strings"
|
|
12
10
|
"sync"
|
|
13
|
-
"time"
|
|
14
11
|
|
|
15
12
|
"GOMODULE/core"
|
|
16
13
|
plugin "GOMODULE/feature/secrets/plugin"
|
|
17
14
|
sekreto "GOMODULE/feature/secrets/sekreto"
|
|
15
|
+
"GOMODULE/utility"
|
|
18
16
|
)
|
|
19
17
|
|
|
20
18
|
type SecretsFeature struct {
|
|
21
19
|
BaseFeature
|
|
22
20
|
client *core.ProjectNameSDK
|
|
21
|
+
rootctx *core.Context
|
|
23
22
|
options map[string]any
|
|
24
23
|
|
|
25
24
|
liveopts map[string]any
|
|
@@ -41,6 +40,9 @@ type SecretsFeature struct {
|
|
|
41
40
|
cred string
|
|
42
41
|
}
|
|
43
42
|
|
|
43
|
+
// Milliseconds the token purchase may take.
|
|
44
|
+
const secretsExchangeTimeout = 30000
|
|
45
|
+
|
|
44
46
|
type secretsExchange struct {
|
|
45
47
|
path string
|
|
46
48
|
method string
|
|
@@ -82,6 +84,7 @@ func (f *SecretsFeature) Sekreto() *sekreto.Sekreto {
|
|
|
82
84
|
|
|
83
85
|
func (f *SecretsFeature) Init(ctx *core.Context, options map[string]any) {
|
|
84
86
|
f.client = ctx.Client
|
|
87
|
+
f.rootctx = ctx
|
|
85
88
|
f.options = options
|
|
86
89
|
f.liveopts = ctx.Options
|
|
87
90
|
f.Active = foptBool(options, "active", false)
|
|
@@ -489,11 +492,15 @@ func (f *SecretsFeature) buyonce() (string, error) {
|
|
|
489
492
|
base = strings.TrimRight(base, "/")
|
|
490
493
|
url := base + "/" + strings.TrimLeft(x.path, "/")
|
|
491
494
|
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
495
|
+
// The SDK's own transport, beneath every feature's wrapper: it holds the
|
|
496
|
+
// test feature's block and the GET-only guard.
|
|
497
|
+
fetch := func(at string, fd map[string]any) (map[string]any, error) {
|
|
498
|
+
res, err := utility.Fetcher(f.rootctx, at, fd)
|
|
499
|
+
if nil != err {
|
|
500
|
+
return nil, err
|
|
501
|
+
}
|
|
502
|
+
out, _ := res.(map[string]any)
|
|
503
|
+
return out, nil
|
|
497
504
|
}
|
|
498
505
|
|
|
499
506
|
// The body is MARSHALLED, never concatenated: a refresh token (or a
|
|
@@ -510,6 +517,8 @@ func (f *SecretsFeature) buyonce() (string, error) {
|
|
|
510
517
|
"body": string(bodybytes),
|
|
511
518
|
// Never followed by the client: a redirect would carry the refresh token on.
|
|
512
519
|
"redirect": "manual",
|
|
520
|
+
// Its own bound: the timeout feature's wrapper is above this call.
|
|
521
|
+
"timeout": secretsExchangeTimeout,
|
|
513
522
|
}
|
|
514
523
|
|
|
515
524
|
res, err := fetch(url, fetchdef)
|
|
@@ -603,66 +612,3 @@ func secretsOrigin(u *neturl.URL) string {
|
|
|
603
612
|
}
|
|
604
613
|
return scheme + "://" + strings.ToLower(u.Hostname()) + ":" + port
|
|
605
614
|
}
|
|
606
|
-
|
|
607
|
-
// rawExchangeFetch is the token-exchange transport of last resort: plain
|
|
608
|
-
// net/http, same result shape the system.fetch seam promises ("status" +
|
|
609
|
-
// "json"). It exists so an exchange works with ordinary SDK options -
|
|
610
|
-
// requiring a custom transport for the COMMON case would reject every
|
|
611
|
-
// live token purchase before a request was made.
|
|
612
|
-
func rawExchangeFetch(fullurl string, fetchdef map[string]any) (map[string]any, error) {
|
|
613
|
-
method, _ := fetchdef["method"].(string)
|
|
614
|
-
if "" == method {
|
|
615
|
-
method = "POST"
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
var body io.Reader
|
|
619
|
-
if raw, is := fetchdef["body"].(string); is && "" != raw {
|
|
620
|
-
body = strings.NewReader(raw)
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
req, err := http.NewRequest(method, fullurl, body)
|
|
624
|
-
if nil != err {
|
|
625
|
-
return nil, err
|
|
626
|
-
}
|
|
627
|
-
if headers, is := fetchdef["headers"].(map[string]any); is {
|
|
628
|
-
for k, v := range headers {
|
|
629
|
-
if vs, is := v.(string); is {
|
|
630
|
-
req.Header.Set(k, vs)
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
client := &http.Client{
|
|
636
|
-
Timeout: 30 * time.Second,
|
|
637
|
-
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
638
|
-
return http.ErrUseLastResponse
|
|
639
|
-
},
|
|
640
|
-
}
|
|
641
|
-
res, err := client.Do(req)
|
|
642
|
-
if nil != err {
|
|
643
|
-
return nil, err
|
|
644
|
-
}
|
|
645
|
-
defer res.Body.Close()
|
|
646
|
-
|
|
647
|
-
raw, err := io.ReadAll(res.Body)
|
|
648
|
-
if nil != err {
|
|
649
|
-
return nil, err
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
headers := map[string]any{}
|
|
653
|
-
for k := range res.Header {
|
|
654
|
-
headers[strings.ToLower(k)] = res.Header.Get(k)
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
return map[string]any{
|
|
658
|
-
"status": res.StatusCode,
|
|
659
|
-
"headers": headers,
|
|
660
|
-
"json": func() any {
|
|
661
|
-
var out any
|
|
662
|
-
if nil != json.Unmarshal(raw, &out) {
|
|
663
|
-
return nil
|
|
664
|
-
}
|
|
665
|
-
return out
|
|
666
|
-
},
|
|
667
|
-
}, nil
|
|
668
|
-
}
|
|
@@ -49,10 +49,11 @@ func (f *TelemetryFeature) PrePoint(ctx *core.Context) {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
span := map[string]any{
|
|
52
|
-
"traceId":
|
|
53
|
-
"spanId":
|
|
54
|
-
"
|
|
55
|
-
"
|
|
52
|
+
"traceId": f.id("trace"),
|
|
53
|
+
"spanId": f.id("span"),
|
|
54
|
+
"correlationId": ctx.Id,
|
|
55
|
+
"name": entity + "." + opname,
|
|
56
|
+
"start": foptNow(f.options)(),
|
|
56
57
|
}
|
|
57
58
|
ctx.Out[telemetrySpanKey] = span
|
|
58
59
|
f.ActiveSpans++
|
|
@@ -9,6 +9,7 @@ import (
|
|
|
9
9
|
"sort"
|
|
10
10
|
"strings"
|
|
11
11
|
"testing"
|
|
12
|
+
"time"
|
|
12
13
|
|
|
13
14
|
sdk "GOMODULE"
|
|
14
15
|
)
|
|
@@ -185,6 +186,10 @@ func fcCandidates(client *sdk.ProjectNameSDK) []fcOp {
|
|
|
185
186
|
|
|
186
187
|
// fcInvoke performs one operation on a client, by entity name and method.
|
|
187
188
|
func fcInvoke(client *sdk.ProjectNameSDK, op fcOp, ctrl map[string]any) error {
|
|
189
|
+
return fcInvokeArgs(client, op, map[string]any{}, ctrl)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
func fcInvokeArgs(client *sdk.ProjectNameSDK, op fcOp, args map[string]any, ctrl map[string]any) error {
|
|
188
193
|
cv := reflect.ValueOf(client)
|
|
189
194
|
ct := cv.Type()
|
|
190
195
|
mapType := reflect.TypeOf(map[string]any{})
|
|
@@ -208,7 +213,7 @@ func fcInvoke(client *sdk.ProjectNameSDK, op fcOp, ctrl map[string]any) error {
|
|
|
208
213
|
return fmt.Errorf("no method %s on entity %s", op.method, op.accessor)
|
|
209
214
|
}
|
|
210
215
|
rets := om.Call([]reflect.Value{
|
|
211
|
-
reflect.ValueOf(
|
|
216
|
+
reflect.ValueOf(args),
|
|
212
217
|
reflect.ValueOf(ctrl),
|
|
213
218
|
})
|
|
214
219
|
if err, ok := rets[1].Interface().(error); ok && err != nil {
|
|
@@ -239,6 +244,96 @@ func fcUsableOps(want int) []fcOp {
|
|
|
239
244
|
return picked
|
|
240
245
|
}
|
|
241
246
|
|
|
247
|
+
// fcUsablePair finds a read and a write of one entity (#READ, #WRITE), each
|
|
248
|
+
// driven as fcUsableOps drives an op.
|
|
249
|
+
func fcUsablePair() (fcOp, fcOp, bool) {
|
|
250
|
+
reads := map[string]fcOp{}
|
|
251
|
+
writes := map[string]fcOp{}
|
|
252
|
+
for _, cand := range fcCandidates(fcClient(map[string]any{})) {
|
|
253
|
+
role := ""
|
|
254
|
+
switch strings.ToLower(cand.method) {
|
|
255
|
+
case "list", "load":
|
|
256
|
+
role = "read"
|
|
257
|
+
case "create", "update", "patch", "remove":
|
|
258
|
+
role = "write"
|
|
259
|
+
}
|
|
260
|
+
seen := reads
|
|
261
|
+
if role == "write" {
|
|
262
|
+
seen = writes
|
|
263
|
+
}
|
|
264
|
+
if _, done := seen[cand.accessor]; role == "" || done {
|
|
265
|
+
continue
|
|
266
|
+
}
|
|
267
|
+
if err := fcInvoke(fcClient(map[string]any{}), cand, map[string]any{}); err != nil {
|
|
268
|
+
continue
|
|
269
|
+
}
|
|
270
|
+
seen[cand.accessor] = cand
|
|
271
|
+
read, okr := reads[cand.accessor]
|
|
272
|
+
write, okw := writes[cand.accessor]
|
|
273
|
+
if okr && okw {
|
|
274
|
+
return read, write, true
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return fcOp{}, fcOp{}, false
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// fcUsableItem finds a write of one entity (#ITEM) that completes when driven
|
|
281
|
+
// with fcItemArgs.
|
|
282
|
+
func fcUsableItem(entity string) (fcOp, bool) {
|
|
283
|
+
for _, cand := range fcCandidates(fcClient(map[string]any{})) {
|
|
284
|
+
switch strings.ToLower(cand.method) {
|
|
285
|
+
case "update", "patch", "remove":
|
|
286
|
+
default:
|
|
287
|
+
continue
|
|
288
|
+
}
|
|
289
|
+
if cand.accessor != entity {
|
|
290
|
+
continue
|
|
291
|
+
}
|
|
292
|
+
if err := fcInvokeArgs(fcClient(map[string]any{}), cand, fcItemArgs(), map[string]any{}); err == nil {
|
|
293
|
+
return cand, true
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return fcOp{}, false
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
func fcItemArgs() map[string]any {
|
|
300
|
+
return map[string]any{"id": "corpus-item"}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// fcStep runs one step: it may wait before its call, may add request headers
|
|
304
|
+
// for its call, and may pass `args` as the operation's first argument.
|
|
305
|
+
func fcStep(client *sdk.ProjectNameSDK, op fcOp, step map[string]any, ctrl map[string]any) error {
|
|
306
|
+
if wait, ok := fcNum(step["wait"]); ok {
|
|
307
|
+
time.Sleep(time.Duration(wait) * time.Millisecond)
|
|
308
|
+
}
|
|
309
|
+
options := client.GetRootCtx().Options
|
|
310
|
+
prior, had := options["headers"]
|
|
311
|
+
if extra, ok := step["headers"].(map[string]any); ok {
|
|
312
|
+
merged := map[string]any{}
|
|
313
|
+
if pm, ok := prior.(map[string]any); ok {
|
|
314
|
+
for k, v := range pm {
|
|
315
|
+
merged[k] = v
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
for k, v := range extra {
|
|
319
|
+
merged[k] = v
|
|
320
|
+
}
|
|
321
|
+
options["headers"] = merged
|
|
322
|
+
defer func() {
|
|
323
|
+
if had {
|
|
324
|
+
options["headers"] = prior
|
|
325
|
+
} else {
|
|
326
|
+
delete(options, "headers")
|
|
327
|
+
}
|
|
328
|
+
}()
|
|
329
|
+
}
|
|
330
|
+
args, _ := step["args"].(map[string]any)
|
|
331
|
+
if args == nil {
|
|
332
|
+
args = map[string]any{}
|
|
333
|
+
}
|
|
334
|
+
return fcInvokeArgs(client, op, args, ctrl)
|
|
335
|
+
}
|
|
336
|
+
|
|
242
337
|
// fcResolve replaces #OPn throughout a case, keys included.
|
|
243
338
|
func fcResolve(node any, tokens map[string]string) any {
|
|
244
339
|
switch v := node.(type) {
|
|
@@ -419,6 +514,16 @@ func fcMissingFeature(kase map[string]any, have map[string]bool) string {
|
|
|
419
514
|
return ""
|
|
420
515
|
}
|
|
421
516
|
|
|
517
|
+
// fcLacking names the first token a case uses that this SDK cannot drive.
|
|
518
|
+
func fcLacking(encoded string, have map[string]bool) string {
|
|
519
|
+
for _, tok := range []string{"#ITEM", "#OTHER"} {
|
|
520
|
+
if !have[tok] && strings.Contains(encoded, tok) {
|
|
521
|
+
return tok
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return ""
|
|
525
|
+
}
|
|
526
|
+
|
|
422
527
|
func TestFeatureCorpus(t *testing.T) {
|
|
423
528
|
spec := loadTestSpec(t)
|
|
424
529
|
|
|
@@ -429,6 +534,18 @@ func TestFeatureCorpus(t *testing.T) {
|
|
|
429
534
|
|
|
430
535
|
ops := fcUsableOps(2)
|
|
431
536
|
have := map[string]bool{}
|
|
537
|
+
pairRead, pairWrite, paired := fcUsablePair()
|
|
538
|
+
var pairItem, pairOther fcOp
|
|
539
|
+
hasItem, hasOther := false, false
|
|
540
|
+
if paired {
|
|
541
|
+
pairItem, hasItem = fcUsableItem(pairRead.accessor)
|
|
542
|
+
for _, o := range ops {
|
|
543
|
+
if o.accessor != pairRead.accessor {
|
|
544
|
+
pairOther, hasOther = o, true
|
|
545
|
+
break
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
432
549
|
|
|
433
550
|
// With every operation needing an argument there is nothing to drive:
|
|
434
551
|
// skip and say so, as the clean test does, rather than fail.
|
|
@@ -483,6 +600,20 @@ func TestFeatureCorpus(t *testing.T) {
|
|
|
483
600
|
continue
|
|
484
601
|
}
|
|
485
602
|
|
|
603
|
+
encoded, _ := json.Marshal(kase)
|
|
604
|
+
wantsPair := strings.Contains(string(encoded), "#READ") || strings.Contains(string(encoded), "#WRITE") ||
|
|
605
|
+
strings.Contains(string(encoded), "#ITEM") || strings.Contains(string(encoded), "#OTHER")
|
|
606
|
+
if wantsPair && !paired {
|
|
607
|
+
t.Logf("skip %q: needs a read and a write of one entity, this SDK offers none",
|
|
608
|
+
kase["name"])
|
|
609
|
+
continue
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
if lacking := fcLacking(string(encoded), map[string]bool{"#ITEM": hasItem, "#OTHER": hasOther}); paired && lacking != "" {
|
|
613
|
+
t.Logf("skip %q: needs %s, which this SDK cannot drive", kase["name"], lacking)
|
|
614
|
+
continue
|
|
615
|
+
}
|
|
616
|
+
|
|
486
617
|
if missing := fcMissingFeature(kase, have); missing != "" {
|
|
487
618
|
t.Logf("skip %q: needs the %s feature, which this SDK does not generate",
|
|
488
619
|
kase["name"], missing)
|
|
@@ -493,12 +624,29 @@ func TestFeatureCorpus(t *testing.T) {
|
|
|
493
624
|
for i := 0; i < need; i++ {
|
|
494
625
|
tokens[fmt.Sprintf("#OP%d", i+1)] = ops[i].key
|
|
495
626
|
}
|
|
627
|
+
if wantsPair {
|
|
628
|
+
tokens["#READ"] = pairRead.key
|
|
629
|
+
tokens["#WRITE"] = pairWrite.key
|
|
630
|
+
if hasItem {
|
|
631
|
+
tokens["#ITEM"] = pairItem.key
|
|
632
|
+
}
|
|
633
|
+
if hasOther {
|
|
634
|
+
tokens["#OTHER"] = pairOther.key
|
|
635
|
+
}
|
|
636
|
+
}
|
|
496
637
|
resolved, _ := fcResolve(kase, tokens).(map[string]any)
|
|
497
638
|
|
|
498
639
|
byKey := map[string]fcOp{}
|
|
499
640
|
for _, o := range ops {
|
|
500
641
|
byKey[o.key] = o
|
|
501
642
|
}
|
|
643
|
+
if paired {
|
|
644
|
+
byKey[pairRead.key] = pairRead
|
|
645
|
+
byKey[pairWrite.key] = pairWrite
|
|
646
|
+
if hasItem {
|
|
647
|
+
byKey[pairItem.key] = pairItem
|
|
648
|
+
}
|
|
649
|
+
}
|
|
502
650
|
|
|
503
651
|
client := fcClient(resolved)
|
|
504
652
|
label, _ := resolved["name"].(string)
|
|
@@ -520,7 +668,7 @@ func TestFeatureCorpus(t *testing.T) {
|
|
|
520
668
|
ctrl = c
|
|
521
669
|
}
|
|
522
670
|
|
|
523
|
-
err :=
|
|
671
|
+
err := fcStep(client, op, step, ctrl)
|
|
524
672
|
wanterr, haserr := step["err"]
|
|
525
673
|
|
|
526
674
|
if !haserr {
|
|
@@ -645,6 +645,67 @@ func TestFeatureCache(t *testing.T) {
|
|
|
645
645
|
}
|
|
646
646
|
})
|
|
647
647
|
|
|
648
|
+
t.Run("keys-on-request-headers", func(t *testing.T) {
|
|
649
|
+
rec := &fhRecorder{}
|
|
650
|
+
h := fhMake(rec.fetch, fhF(feat.NewCacheFeature(), map[string]any{"ttl": 10000}))
|
|
651
|
+
h.op(fhOpSpec{op: "load", path: "/w/1", headers: map[string]any{"x-tenant": "tenant-a"}})
|
|
652
|
+
h.op(fhOpSpec{op: "load", path: "/w/1", headers: map[string]any{"X-Tenant": "tenant-b"}})
|
|
653
|
+
if len(rec.calls) != 2 {
|
|
654
|
+
t.Errorf("a second tenant was served the first tenant's entry: %d calls", len(rec.calls))
|
|
655
|
+
}
|
|
656
|
+
h.op(fhOpSpec{op: "load", path: "/w/1", headers: map[string]any{"x-tenant": "tenant-b"}})
|
|
657
|
+
if len(rec.calls) != 2 {
|
|
658
|
+
t.Errorf("the same headers did not share an entry: %d calls", len(rec.calls))
|
|
659
|
+
}
|
|
660
|
+
})
|
|
661
|
+
|
|
662
|
+
t.Run("a-write-drops-its-collection", func(t *testing.T) {
|
|
663
|
+
rec := &fhRecorder{}
|
|
664
|
+
h := fhMake(rec.fetch, fhF(feat.NewCacheFeature(), map[string]any{"ttl": 10000}))
|
|
665
|
+
paths := []string{"/w/1", "/w", "/w/1/part", "/other/1", "/w/2"}
|
|
666
|
+
for _, p := range paths {
|
|
667
|
+
h.op(fhOpSpec{op: "load", path: p})
|
|
668
|
+
}
|
|
669
|
+
h.op(fhOpSpec{op: "update", method: "PUT", path: "/w/1"})
|
|
670
|
+
for _, p := range paths {
|
|
671
|
+
h.op(fhOpSpec{op: "load", path: p})
|
|
672
|
+
}
|
|
673
|
+
if len(rec.calls) != 9 {
|
|
674
|
+
t.Errorf("expected the collection refetched and /other cached: %d calls", len(rec.calls))
|
|
675
|
+
}
|
|
676
|
+
})
|
|
677
|
+
|
|
678
|
+
t.Run("cache-control-prevents-storing", func(t *testing.T) {
|
|
679
|
+
for _, control := range []string{"no-store", "private", "No-Cache", "max-age=60, private"} {
|
|
680
|
+
rec := &fhRecorder{reply: func(_ int, _ map[string]any) (any, error) {
|
|
681
|
+
return fhResponse(200, map[string]any{"ok": true}, map[string]any{"Cache-Control": control}), nil
|
|
682
|
+
}}
|
|
683
|
+
h := fhMake(rec.fetch, fhF(feat.NewCacheFeature(), map[string]any{"ttl": 10000}))
|
|
684
|
+
h.op(fhOpSpec{op: "load", path: "/w"})
|
|
685
|
+
h.op(fhOpSpec{op: "load", path: "/w"})
|
|
686
|
+
if len(rec.calls) != 2 {
|
|
687
|
+
t.Errorf("%s was stored: %d calls", control, len(rec.calls))
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
})
|
|
691
|
+
|
|
692
|
+
t.Run("max-age-caps-the-ttl", func(t *testing.T) {
|
|
693
|
+
clock := &fhClock{}
|
|
694
|
+
rec := &fhRecorder{reply: func(_ int, _ map[string]any) (any, error) {
|
|
695
|
+
return fhResponse(200, map[string]any{"ok": true}, map[string]any{"cache-control": "max-age=1"}), nil
|
|
696
|
+
}}
|
|
697
|
+
h := fhMake(rec.fetch, fhF(feat.NewCacheFeature(),
|
|
698
|
+
map[string]any{"ttl": 10000, "now": clock.now}))
|
|
699
|
+
h.op(fhOpSpec{op: "load", path: "/w"})
|
|
700
|
+
clock.advance(500)
|
|
701
|
+
h.op(fhOpSpec{op: "load", path: "/w"})
|
|
702
|
+
clock.advance(600)
|
|
703
|
+
h.op(fhOpSpec{op: "load", path: "/w"})
|
|
704
|
+
if len(rec.calls) != 2 {
|
|
705
|
+
t.Errorf("expected max-age=1 to expire the entry before the ttl: %d calls", len(rec.calls))
|
|
706
|
+
}
|
|
707
|
+
})
|
|
708
|
+
|
|
648
709
|
t.Run("inactive-does-not-wrap", func(t *testing.T) {
|
|
649
710
|
rec := &fhRecorder{}
|
|
650
711
|
h := fhMake(rec.fetch, fhF(feat.NewCacheFeature(), map[string]any{"active": false}))
|
|
@@ -1047,6 +1108,46 @@ func TestFeatureAudit(t *testing.T) {
|
|
|
1047
1108
|
})
|
|
1048
1109
|
}
|
|
1049
1110
|
|
|
1111
|
+
// --- one call, one id -----------------------------------------------------------
|
|
1112
|
+
|
|
1113
|
+
func TestFeatureOneCallOneId(t *testing.T) {
|
|
1114
|
+
fhSkipWithout(t, "telemetry", "debug", "audit")
|
|
1115
|
+
|
|
1116
|
+
t.Run("telemetry-debug-and-audit-carry-one-calls-context-id", func(t *testing.T) {
|
|
1117
|
+
ft := feat.NewTelemetryFeature()
|
|
1118
|
+
fd := feat.NewDebugFeature()
|
|
1119
|
+
fa := feat.NewAuditFeature()
|
|
1120
|
+
h := fhMake(nil, fhF(ft, nil), fhF(fd, nil), fhF(fa, nil))
|
|
1121
|
+
h.op(fhOpSpec{op: "load"})
|
|
1122
|
+
h.op(fhOpSpec{op: "list"})
|
|
1123
|
+
ids := func(recs []map[string]any) []any {
|
|
1124
|
+
out := []any{}
|
|
1125
|
+
for _, rec := range recs {
|
|
1126
|
+
out = append(out, rec["correlationId"])
|
|
1127
|
+
}
|
|
1128
|
+
return out
|
|
1129
|
+
}
|
|
1130
|
+
calls := ids(fa.Records)
|
|
1131
|
+
if len(calls) != 2 {
|
|
1132
|
+
t.Fatalf("expected 2 audit records, got %d", len(calls))
|
|
1133
|
+
}
|
|
1134
|
+
for _, id := range calls {
|
|
1135
|
+
if s, ok := id.(string); !ok || s == "" {
|
|
1136
|
+
t.Fatalf("expected non-empty string ids, got %v", calls)
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
if calls[0] == calls[1] {
|
|
1140
|
+
t.Errorf("expected one id per call, got %v", calls)
|
|
1141
|
+
}
|
|
1142
|
+
if spans := ids(ft.Spans); !reflect.DeepEqual(spans, calls) {
|
|
1143
|
+
t.Errorf("expected span ids %v, got %v", calls, spans)
|
|
1144
|
+
}
|
|
1145
|
+
if entries := ids(fd.Entries); !reflect.DeepEqual(entries, calls) {
|
|
1146
|
+
t.Errorf("expected debug entry ids %v, got %v", calls, entries)
|
|
1147
|
+
}
|
|
1148
|
+
})
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1050
1151
|
// --- clienttrack ----------------------------------------------------------------
|
|
1051
1152
|
|
|
1052
1153
|
func TestFeatureClienttrack(t *testing.T) {
|