@voxgig/sdkgen 4.39.0 → 4.40.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/dist/action/doctor.js +5 -1
- package/dist/action/doctor.js.map +1 -1
- package/dist/helpers/copymerge.js +14 -9
- package/dist/helpers/copymerge.js.map +1 -1
- package/dist/helpers/testPolicy.d.ts +8 -2
- package/dist/helpers/testPolicy.js +18 -0
- package/dist/helpers/testPolicy.js.map +1 -1
- package/dist/sdkgen.d.ts +4 -4
- package/dist/sdkgen.js +4 -3
- package/dist/sdkgen.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/project/.sdk/model/target/rust.aontu +1 -0
- package/project/.sdk/src/cmp/c/fragment/Main.fragment.c +15 -0
- package/project/.sdk/src/cmp/cpp/TestDirect_cpp.ts +2 -0
- package/project/.sdk/src/cmp/csharp/TestDirect_csharp.ts +20 -3
- package/project/.sdk/src/cmp/csharp/TestEntity_csharp.ts +43 -15
- package/project/.sdk/src/cmp/csharp/fragment/Main.fragment.cs +17 -0
- package/project/.sdk/src/cmp/elixir/fragment/Main.fragment.ex +10 -0
- package/project/.sdk/src/cmp/go/TestDirect_go.ts +23 -4
- package/project/.sdk/src/cmp/go/TestEntity_go.ts +37 -12
- package/project/.sdk/src/cmp/go/fragment/Main.fragment.go +14 -0
- package/project/.sdk/src/cmp/java/TestDirect_java.ts +18 -3
- package/project/.sdk/src/cmp/java/TestEntity_java.ts +39 -14
- package/project/.sdk/src/cmp/js/TestDirect_js.ts +7 -1
- package/project/.sdk/src/cmp/js/fragment/Main.fragment.js +7 -0
- package/project/.sdk/src/cmp/kotlin/TestDirect_kotlin.ts +18 -3
- package/project/.sdk/src/cmp/kotlin/TestEntity_kotlin.ts +37 -14
- package/project/.sdk/src/cmp/lua/TestDirect_lua.ts +13 -3
- package/project/.sdk/src/cmp/lua/TestEntity_lua.ts +36 -10
- package/project/.sdk/src/cmp/lua/fragment/Main.fragment.lua +14 -0
- package/project/.sdk/src/cmp/ocaml/TestDirect_ocaml.ts +3 -1
- package/project/.sdk/src/cmp/perl/TestDirect_perl.ts +9 -3
- package/project/.sdk/src/cmp/perl/TestEntity_perl.ts +22 -8
- package/project/.sdk/src/cmp/perl/fragment/Main.fragment.pm +6 -0
- package/project/.sdk/src/cmp/php/TestDirect_php.ts +13 -3
- package/project/.sdk/src/cmp/php/TestEntity_php.ts +31 -9
- package/project/.sdk/src/cmp/php/fragment/Main.fragment.php +14 -0
- package/project/.sdk/src/cmp/py/TestDirect_py.ts +11 -3
- package/project/.sdk/src/cmp/py/TestEntity_py.ts +30 -9
- package/project/.sdk/src/cmp/py/fragment/Main.fragment.py +6 -0
- package/project/.sdk/src/cmp/rb/TestDirect_rb.ts +9 -3
- package/project/.sdk/src/cmp/rb/TestEntity_rb.ts +25 -10
- package/project/.sdk/src/cmp/rb/fragment/Main.fragment.rb +7 -0
- package/project/.sdk/src/cmp/rust/TestDirect_rust.ts +13 -3
- package/project/.sdk/src/cmp/rust/TestEntity_rust.ts +48 -27
- package/project/.sdk/src/cmp/rust/fragment/Main.fragment.rs +15 -0
- package/project/.sdk/src/cmp/swift/fragment/Main.fragment.swift +7 -0
- package/project/.sdk/src/cmp/ts/TestDirect_ts.ts +7 -1
- package/project/.sdk/src/cmp/ts/TestEntity_ts.ts +8 -2
- package/project/.sdk/src/cmp/ts/fragment/Main.fragment.ts +7 -0
- package/project/.sdk/src/cmp/zig/TestDirect_zig.ts +1 -0
- package/project/.sdk/src/cmp/zig/fragment/Main.fragment.zig +16 -0
- package/project/.sdk/tm/c/utility/http.c +256 -0
- package/project/.sdk/tm/clojure/src/sdk/client.clj +7 -0
- package/project/.sdk/tm/clojure/src/sdk/core.clj +24 -1
- package/project/.sdk/tm/cpp/core/types.hpp +14 -0
- package/project/.sdk/tm/csharp/test/Runner.cs +49 -3
- package/project/.sdk/tm/csharp/utility/Fetcher.cs +5 -1
- package/project/.sdk/tm/elixir/lib/projectname/utility.ex +54 -19
- package/project/.sdk/tm/go/test/testsupport_test.go +44 -3
- package/project/.sdk/tm/go/utility/fetcher.go +40 -0
- package/project/.sdk/tm/java/core/SdkClient.java +7 -0
- package/project/.sdk/tm/java/test/RunnerSupport.java +45 -2
- package/project/.sdk/tm/java/utility/Fetcher.java +67 -1
- package/project/.sdk/tm/js/src/utility/MakeResultUtility.js +3 -1
- package/project/.sdk/tm/js/test/live-entity.js +7 -3
- package/project/.sdk/tm/kotlin/core/SdkClient.kt +7 -0
- package/project/.sdk/tm/kotlin/test/RunnerSupport.kt +40 -3
- package/project/.sdk/tm/kotlin/utility/Fetcher.kt +52 -1
- package/project/.sdk/tm/lua/test/runner.lua +49 -2
- package/project/.sdk/tm/lua/utility/fetcher.lua +228 -0
- package/project/.sdk/tm/ocaml/sdk_features.ml +8 -0
- package/project/.sdk/tm/perl/t/runner.pm +18 -3
- package/project/.sdk/tm/perl/utility/fetcher.pm +42 -3
- package/project/.sdk/tm/php/test/Runner.php +43 -5
- package/project/.sdk/tm/php/utility/Fetcher.php +36 -0
- package/project/.sdk/tm/php/utility/MakeResult.php +3 -1
- package/project/.sdk/tm/py/test/runner.py +32 -3
- package/project/.sdk/tm/rb/test/runner.rb +24 -1
- package/project/.sdk/tm/rust/tests/common/mod.rs +53 -5
- package/project/.sdk/tm/rust/utility/fetcher.rs +43 -3
- package/project/.sdk/tm/scala/core/SdkClient.scala +7 -0
- package/project/.sdk/tm/scala/utility/Fetcher.scala +48 -1
- package/project/.sdk/tm/ts/src/utility/MakeResultUtility.ts +3 -1
- package/project/.sdk/tm/ts/test/live-entity.ts +8 -3
- package/project/sdkgen-package.json +1 -1
- package/src/action/doctor.ts +5 -1
- package/src/helpers/copymerge.ts +16 -11
- package/src/helpers/testPolicy.ts +23 -2
- package/src/sdkgen.ts +4 -2
|
@@ -436,9 +436,10 @@ public final class RunnerSupport {
|
|
|
436
436
|
}
|
|
437
437
|
|
|
438
438
|
// The record a create-less flow reads live: the first its list returns,
|
|
439
|
-
// put where the flow reads the fixture's existing records.
|
|
439
|
+
// put where the flow reads the fixture's existing records. A record with
|
|
440
|
+
// no value in the identity field takes the fallback identity.
|
|
440
441
|
public static void liveExisting(Map<String, Object> data, boolean strict, String name,
|
|
441
|
-
Supplier<Object> list) {
|
|
442
|
+
String field, Object fallback, Supplier<Object> list) {
|
|
442
443
|
Object found = null;
|
|
443
444
|
try {
|
|
444
445
|
found = list.get();
|
|
@@ -455,6 +456,19 @@ public final class RunnerSupport {
|
|
|
455
456
|
}
|
|
456
457
|
Object first = items.get(0);
|
|
457
458
|
Object record = first instanceof Entity ? ((Entity) first).data() : first;
|
|
459
|
+
Map<String, Object> fields = Helpers.toMapAny(record);
|
|
460
|
+
if (fields == null || fields.get(field) == null) {
|
|
461
|
+
if (fallback == null) {
|
|
462
|
+
liveMiss(strict, "Live entity test blocked: the listed " + name + " record has no " + field
|
|
463
|
+
+ ", and neither *_ENTID nor the load route's example gives one");
|
|
464
|
+
}
|
|
465
|
+
Map<String, Object> filled = new LinkedHashMap<>();
|
|
466
|
+
if (fields != null) {
|
|
467
|
+
filled.putAll(fields);
|
|
468
|
+
}
|
|
469
|
+
filled.put(field, fallback);
|
|
470
|
+
record = filled;
|
|
471
|
+
}
|
|
458
472
|
Object existing = data.get("existing");
|
|
459
473
|
if (!(existing instanceof Map)) {
|
|
460
474
|
existing = new LinkedHashMap<String, Object>();
|
|
@@ -465,6 +479,35 @@ public final class RunnerSupport {
|
|
|
465
479
|
((Map<String, Object>) existing).put(name, holder);
|
|
466
480
|
}
|
|
467
481
|
|
|
482
|
+
// Identities compared by their string forms: a route parameter's
|
|
483
|
+
// value is a string where the record may hold a number.
|
|
484
|
+
public static boolean liveSameId(Object a, Object b) {
|
|
485
|
+
if (a == null || b == null) {
|
|
486
|
+
return a == null && b == null;
|
|
487
|
+
}
|
|
488
|
+
return liveIdString(a).equals(liveIdString(b));
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
private static String liveIdString(Object v) {
|
|
492
|
+
if (v instanceof Double || v instanceof Float) {
|
|
493
|
+
double d = ((Number) v).doubleValue();
|
|
494
|
+
if (!Double.isInfinite(d) && d == Math.rint(d) && Math.abs(d) < 9.0e15) {
|
|
495
|
+
return Long.toString((long) d);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
return String.valueOf(v);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// Sleeps test.live.delayMs before a live request.
|
|
502
|
+
public static void livePace() {
|
|
503
|
+
try {
|
|
504
|
+
Thread.sleep(liveDelayMs());
|
|
505
|
+
}
|
|
506
|
+
catch (InterruptedException e) {
|
|
507
|
+
Thread.currentThread().interrupt();
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
468
511
|
// In a lenient live run a failing check skips, observing the live API.
|
|
469
512
|
public static void liveObserve(Throwable err, boolean live, boolean strict) {
|
|
470
513
|
if (strict || !live || err instanceof org.opentest4j.TestAbortedException) {
|
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
package JAVAPACKAGE.utility;
|
|
2
2
|
|
|
3
|
+
import java.io.ByteArrayInputStream;
|
|
4
|
+
import java.io.IOException;
|
|
3
5
|
import java.io.InputStream;
|
|
6
|
+
import java.io.UncheckedIOException;
|
|
4
7
|
import java.net.InetSocketAddress;
|
|
5
8
|
import java.net.ProxySelector;
|
|
6
9
|
import java.net.URI;
|
|
7
10
|
import java.net.http.HttpClient;
|
|
8
11
|
import java.net.http.HttpRequest;
|
|
9
12
|
import java.net.http.HttpResponse;
|
|
13
|
+
import java.nio.charset.Charset;
|
|
14
|
+
import java.nio.charset.StandardCharsets;
|
|
10
15
|
import java.util.LinkedHashMap;
|
|
11
16
|
import java.util.List;
|
|
12
17
|
import java.util.Map;
|
|
13
18
|
import java.util.concurrent.ConcurrentHashMap;
|
|
14
19
|
import java.util.function.BiFunction;
|
|
15
20
|
import java.util.function.Supplier;
|
|
21
|
+
import java.util.zip.GZIPInputStream;
|
|
22
|
+
import java.util.zip.Inflater;
|
|
23
|
+
import java.util.zip.InflaterInputStream;
|
|
16
24
|
|
|
17
25
|
import JAVAPACKAGE.core.Context;
|
|
18
26
|
import JAVAPACKAGE.core.SdkError;
|
|
@@ -133,7 +141,7 @@ final class Fetcher {
|
|
|
133
141
|
|
|
134
142
|
HttpResponse<String> resp;
|
|
135
143
|
try {
|
|
136
|
-
resp = clientFor(fetchdef).send(reqb.build(),
|
|
144
|
+
resp = clientFor(fetchdef).send(reqb.build(), decodedText());
|
|
137
145
|
}
|
|
138
146
|
catch (Exception e) {
|
|
139
147
|
throw new RuntimeException("fetch: " + e.getMessage(), e);
|
|
@@ -173,6 +181,64 @@ final class Fetcher {
|
|
|
173
181
|
return out;
|
|
174
182
|
}
|
|
175
183
|
|
|
184
|
+
// The body as text, as BodyHandlers.ofString reads it, once a gzip or
|
|
185
|
+
// deflate Content-Encoding is undone: HttpClient decodes none, and a server
|
|
186
|
+
// may compress its answer whatever the request asked for.
|
|
187
|
+
static HttpResponse.BodyHandler<String> decodedText() {
|
|
188
|
+
return info -> {
|
|
189
|
+
String encoding = info.headers().firstValue("content-encoding").orElse("")
|
|
190
|
+
.trim().toLowerCase();
|
|
191
|
+
Charset charset = charsetOf(info.headers().firstValue("content-type").orElse(""));
|
|
192
|
+
return HttpResponse.BodySubscribers.mapping(
|
|
193
|
+
HttpResponse.BodySubscribers.ofByteArray(),
|
|
194
|
+
bytes -> {
|
|
195
|
+
try {
|
|
196
|
+
return new String(decodeBody(bytes, encoding), charset);
|
|
197
|
+
}
|
|
198
|
+
catch (IOException e) {
|
|
199
|
+
throw new UncheckedIOException(
|
|
200
|
+
"response body is not valid " + encoding + " data: " + e.getMessage(), e);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Deflate is zlib-wrapped, or raw as some servers send it.
|
|
207
|
+
static byte[] decodeBody(byte[] bytes, String encoding) throws IOException {
|
|
208
|
+
if (bytes == null || bytes.length == 0) {
|
|
209
|
+
return bytes == null ? new byte[0] : bytes;
|
|
210
|
+
}
|
|
211
|
+
if ("gzip".equals(encoding) || "x-gzip".equals(encoding)) {
|
|
212
|
+
try (InputStream in = new GZIPInputStream(new ByteArrayInputStream(bytes))) {
|
|
213
|
+
return in.readAllBytes();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if ("deflate".equals(encoding)) {
|
|
217
|
+
boolean zlib = bytes.length > 1 && (bytes[0] & 0x0f) == 8
|
|
218
|
+
&& (((bytes[0] & 0xff) << 8) | (bytes[1] & 0xff)) % 31 == 0;
|
|
219
|
+
try (InputStream in = new InflaterInputStream(
|
|
220
|
+
new ByteArrayInputStream(bytes), new Inflater(!zlib))) {
|
|
221
|
+
return in.readAllBytes();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return bytes;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
static Charset charsetOf(String contentType) {
|
|
228
|
+
for (String part : contentType.split(";")) {
|
|
229
|
+
String[] kv = part.trim().split("=", 2);
|
|
230
|
+
if (kv.length == 2 && "charset".equalsIgnoreCase(kv[0].trim())) {
|
|
231
|
+
try {
|
|
232
|
+
return Charset.forName(kv[1].trim().replace("\"", ""));
|
|
233
|
+
}
|
|
234
|
+
catch (RuntimeException e) {
|
|
235
|
+
return StandardCharsets.UTF_8;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return StandardCharsets.UTF_8;
|
|
240
|
+
}
|
|
241
|
+
|
|
176
242
|
// HTTP/2 responses carry no reason phrase; use the standard text for
|
|
177
243
|
// common codes so statusText stays informative.
|
|
178
244
|
static String statusText(int status) {
|
|
@@ -32,7 +32,9 @@ function makeResult(ctx) {
|
|
|
32
32
|
const resdata = result.resdata
|
|
33
33
|
result.resdata = []
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
// Only a list holds records: a string has a length too, and an object
|
|
36
|
+
// (a page envelope the transform did not unwrap) holds none.
|
|
37
|
+
if (Array.isArray(resdata) && 0 < resdata.length) {
|
|
36
38
|
for (let entry of resdata) {
|
|
37
39
|
const ent = entity.make()
|
|
38
40
|
ent.data(entry)
|
|
@@ -10,6 +10,8 @@ const live_runner_1 = require("./live-runner");
|
|
|
10
10
|
const utility_1 = require("./utility");
|
|
11
11
|
const routeOf = (point) => String(point.m).toUpperCase() + ' ' + point.o;
|
|
12
12
|
const scalarOf = (value) => ['string', 'number', 'boolean'].includes(typeof value) ? value : undefined;
|
|
13
|
+
// A route parameter's value is a string where the record may hold a number.
|
|
14
|
+
const sameId = (a, b) => null != a && null != b && String(a) === String(b);
|
|
13
15
|
// The offline flow keeps its deterministic fixture assertions. Live flows
|
|
14
16
|
// resolve real prerequisites per operation and collect failures until done.
|
|
15
17
|
// `excluded` names, by step id, the steps this run leaves out, and why.
|
|
@@ -26,6 +28,8 @@ async function runLiveEntity(setup, entity, flow, accessor, facts = {}, settle =
|
|
|
26
28
|
...Object.values(entity.fields || {}).map((field) => field?.n).filter(Boolean)]);
|
|
27
29
|
const marks = new Map();
|
|
28
30
|
const idField = entity.id?.field || 'id';
|
|
31
|
+
const declared = new Set(Object.values(entity.fields || {}).map((field) => field?.n));
|
|
32
|
+
const repeatsId = null != entity.id && (declared.has(idField) || declared.has('id'));
|
|
29
33
|
const copy = (value) => JSON.parse(JSON.stringify(value ?? {}));
|
|
30
34
|
const hasCreate = steps.some(step => step.o === 'create');
|
|
31
35
|
const report = await (0, live_runner_1.runLiveSteps)(steps.map((step, index) => {
|
|
@@ -163,7 +167,7 @@ async function runLiveEntity(setup, entity, flow, accessor, facts = {}, settle =
|
|
|
163
167
|
const id = previous?.[idField] ?? previous?.id;
|
|
164
168
|
if (undefined === id)
|
|
165
169
|
continue;
|
|
166
|
-
const found = data.some((item) => (item?.[idField] ?? item?.id
|
|
170
|
+
const found = data.some((item) => sameId(item?.[idField] ?? item?.id, id));
|
|
167
171
|
if (validation.apply === 'ItemExists')
|
|
168
172
|
(0, strict_1.default)(found, 'Created resource missing from list');
|
|
169
173
|
if (validation.apply === 'ItemNotExists')
|
|
@@ -181,8 +185,8 @@ async function runLiveEntity(setup, entity, flow, accessor, facts = {}, settle =
|
|
|
181
185
|
context.publish(data);
|
|
182
186
|
if (op === 'create' && entity.id)
|
|
183
187
|
(0, strict_1.default)(null != (data[idField] ?? data.id), 'Missing created identity');
|
|
184
|
-
if (['load', 'update'].includes(op) && input.id != null &&
|
|
185
|
-
strict_1.default.equal(data[idField] ?? data.id, input.id, 'Response identity mismatch');
|
|
188
|
+
if (['load', 'update'].includes(op) && input.id != null && repeatsId) {
|
|
189
|
+
strict_1.default.equal(String(data[idField] ?? data.id), String(input.id), 'Response identity mismatch');
|
|
186
190
|
}
|
|
187
191
|
const mark = marks.get(ref);
|
|
188
192
|
if (mark && ['update', 'load'].includes(op)) {
|
|
@@ -169,6 +169,13 @@ abstract class SdkClient(sdkopts: MutableMap<String, Any?>?) {
|
|
|
169
169
|
ctx.spec!!.headers.putAll(uheaders)
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
+
// A raw request has no point to declare its response type, so it asks
|
|
173
|
+
// for the JSON it reads: an API that negotiates on Accept otherwise
|
|
174
|
+
// answers in its own default format. A caller's own accept wins.
|
|
175
|
+
if (ctx.spec!!.headers.keys.none { "accept".equals(it, ignoreCase = true) }) {
|
|
176
|
+
ctx.spec!!.headers["accept"] = "application/json"
|
|
177
|
+
}
|
|
178
|
+
|
|
172
179
|
utility.prepareAuth(ctx)
|
|
173
180
|
|
|
174
181
|
return utility.makeFetchDef(ctx)
|
|
@@ -343,8 +343,10 @@ object RunnerSupport {
|
|
|
343
343
|
}
|
|
344
344
|
|
|
345
345
|
// The record a create-less flow reads live: the first its list returns,
|
|
346
|
-
// put where the flow reads the fixture's existing records.
|
|
347
|
-
|
|
346
|
+
// put where the flow reads the fixture's existing records. A record with
|
|
347
|
+
// no value in the identity field takes the fallback identity.
|
|
348
|
+
fun liveExisting(data: MutableMap<String, Any?>, strict: Boolean, name: String,
|
|
349
|
+
field: String, fallback: Any?, list: () -> Any?) {
|
|
348
350
|
var found: Any? = null
|
|
349
351
|
try {
|
|
350
352
|
found = list()
|
|
@@ -359,7 +361,18 @@ object RunnerSupport {
|
|
|
359
361
|
liveEmpty("The account has no " + name + " record to load")
|
|
360
362
|
}
|
|
361
363
|
val first = items[0]
|
|
362
|
-
|
|
364
|
+
var record = if (first is Entity) first.data() else first
|
|
365
|
+
val fields = record as? Map<*, *>
|
|
366
|
+
if (fields == null || fields[field] == null) {
|
|
367
|
+
if (fallback == null) {
|
|
368
|
+
liveMiss(strict, "Live entity test blocked: the listed " + name + " record has no " + field +
|
|
369
|
+
", and neither *_ENTID nor the load route's example gives one")
|
|
370
|
+
}
|
|
371
|
+
val filled = linkedMapOf<String, Any?>()
|
|
372
|
+
fields?.forEach { (k, v) -> filled[k.toString()] = v }
|
|
373
|
+
filled[field] = fallback
|
|
374
|
+
record = filled
|
|
375
|
+
}
|
|
363
376
|
var existing = data["existing"]
|
|
364
377
|
if (existing !is MutableMap<*, *>) {
|
|
365
378
|
existing = linkedMapOf<String, Any?>()
|
|
@@ -368,6 +381,30 @@ object RunnerSupport {
|
|
|
368
381
|
(existing as MutableMap<String, Any?>)[name] = linkedMapOf<String, Any?>("live01" to record)
|
|
369
382
|
}
|
|
370
383
|
|
|
384
|
+
// Identities compared by their string forms: a route parameter's
|
|
385
|
+
// value is a string where the record may hold a number.
|
|
386
|
+
fun liveSameId(a: Any?, b: Any?): Boolean {
|
|
387
|
+
if (a == null || b == null) {
|
|
388
|
+
return a == null && b == null
|
|
389
|
+
}
|
|
390
|
+
return liveIdString(a) == liveIdString(b)
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
private fun liveIdString(v: Any): String {
|
|
394
|
+
if (v is Double || v is Float) {
|
|
395
|
+
val d = (v as Number).toDouble()
|
|
396
|
+
if (!d.isInfinite() && d == Math.rint(d) && Math.abs(d) < 9.0e15) {
|
|
397
|
+
return d.toLong().toString()
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return v.toString()
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// Sleeps test.live.delayMs before a live request.
|
|
404
|
+
fun livePace() {
|
|
405
|
+
Thread.sleep(liveDelayMs().toLong())
|
|
406
|
+
}
|
|
407
|
+
|
|
371
408
|
// In a lenient live run a failing check skips, observing the live API.
|
|
372
409
|
fun liveObserve(err: Throwable, live: Boolean, strict: Boolean) {
|
|
373
410
|
if (strict || !live || err is org.opentest4j.TestAbortedException) {
|
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
package KOTLINPACKAGE.utility
|
|
2
2
|
|
|
3
|
+
import java.io.ByteArrayInputStream
|
|
4
|
+
import java.io.IOException
|
|
3
5
|
import java.io.InputStream
|
|
6
|
+
import java.io.UncheckedIOException
|
|
4
7
|
import java.net.InetSocketAddress
|
|
5
8
|
import java.net.ProxySelector
|
|
6
9
|
import java.net.URI
|
|
7
10
|
import java.net.http.HttpClient
|
|
8
11
|
import java.net.http.HttpRequest
|
|
9
12
|
import java.net.http.HttpResponse
|
|
13
|
+
import java.nio.charset.Charset
|
|
10
14
|
import java.util.concurrent.ConcurrentHashMap
|
|
11
15
|
import java.util.function.BiFunction
|
|
12
16
|
import java.util.function.Supplier
|
|
17
|
+
import java.util.zip.GZIPInputStream
|
|
18
|
+
import java.util.zip.Inflater
|
|
19
|
+
import java.util.zip.InflaterInputStream
|
|
13
20
|
|
|
14
21
|
import KOTLINPACKAGE.core.Context
|
|
15
22
|
import KOTLINPACKAGE.core.SdkError
|
|
@@ -114,7 +121,7 @@ fun defaultHttpFetch(fullurl: String, fetchdef: MutableMap<String, Any?>): Mutab
|
|
|
114
121
|
|
|
115
122
|
val resp: HttpResponse<String>
|
|
116
123
|
try {
|
|
117
|
-
resp = clientFor(fetchdef).send(reqb.build(),
|
|
124
|
+
resp = clientFor(fetchdef).send(reqb.build(), decodedText())
|
|
118
125
|
} catch (e: Exception) {
|
|
119
126
|
throw RuntimeException("fetch: " + e.message, e)
|
|
120
127
|
}
|
|
@@ -150,6 +157,50 @@ fun defaultHttpFetch(fullurl: String, fetchdef: MutableMap<String, Any?>): Mutab
|
|
|
150
157
|
return out
|
|
151
158
|
}
|
|
152
159
|
|
|
160
|
+
// The body as text, as BodyHandlers.ofString reads it, once a gzip or deflate
|
|
161
|
+
// Content-Encoding is undone: HttpClient decodes none, and a server may
|
|
162
|
+
// compress its answer whatever the request asked for.
|
|
163
|
+
internal fun decodedText(): HttpResponse.BodyHandler<String> =
|
|
164
|
+
HttpResponse.BodyHandler { info ->
|
|
165
|
+
val encoding = info.headers().firstValue("content-encoding").orElse("").trim().lowercase()
|
|
166
|
+
val charset = charsetOf(info.headers().firstValue("content-type").orElse(""))
|
|
167
|
+
HttpResponse.BodySubscribers.mapping(HttpResponse.BodySubscribers.ofByteArray()) { bytes ->
|
|
168
|
+
try {
|
|
169
|
+
String(decodeBody(bytes, encoding), charset)
|
|
170
|
+
} catch (e: IOException) {
|
|
171
|
+
throw UncheckedIOException("response body is not valid $encoding data: ${e.message}", e)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Deflate is zlib-wrapped, or raw as some servers send it.
|
|
177
|
+
internal fun decodeBody(bytes: ByteArray?, encoding: String): ByteArray {
|
|
178
|
+
if (bytes == null || bytes.isEmpty()) return bytes ?: ByteArray(0)
|
|
179
|
+
if ("gzip" == encoding || "x-gzip" == encoding) {
|
|
180
|
+
return GZIPInputStream(ByteArrayInputStream(bytes)).use { it.readAllBytes() }
|
|
181
|
+
}
|
|
182
|
+
if ("deflate" == encoding) {
|
|
183
|
+
val zlib = bytes.size > 1 && (bytes[0].toInt() and 0x0f) == 8 &&
|
|
184
|
+
(((bytes[0].toInt() and 0xff) shl 8) or (bytes[1].toInt() and 0xff)) % 31 == 0
|
|
185
|
+
return InflaterInputStream(ByteArrayInputStream(bytes), Inflater(!zlib)).use { it.readAllBytes() }
|
|
186
|
+
}
|
|
187
|
+
return bytes
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
internal fun charsetOf(contentType: String): Charset {
|
|
191
|
+
for (part in contentType.split(";")) {
|
|
192
|
+
val kv = part.trim().split("=", limit = 2)
|
|
193
|
+
if (kv.size == 2 && "charset".equals(kv[0].trim(), ignoreCase = true)) {
|
|
194
|
+
return try {
|
|
195
|
+
Charset.forName(kv[1].trim().replace("\"", ""))
|
|
196
|
+
} catch (e: RuntimeException) {
|
|
197
|
+
Charsets.UTF_8
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return Charsets.UTF_8
|
|
202
|
+
}
|
|
203
|
+
|
|
153
204
|
// HTTP/2 responses carry no reason phrase; use the standard text for
|
|
154
205
|
// common codes so statusText stays informative.
|
|
155
206
|
fun statusText(status: Int): String {
|
|
@@ -270,8 +270,9 @@ end
|
|
|
270
270
|
|
|
271
271
|
|
|
272
272
|
-- The record a create-less flow reads live: the first its list returns,
|
|
273
|
-
-- put where the flow reads the fixture's existing records.
|
|
274
|
-
|
|
273
|
+
-- put where the flow reads the fixture's existing records. A record with no
|
|
274
|
+
-- value in the identity field takes the fallback identity.
|
|
275
|
+
function runner.live_existing(pending, setup, strict, name, list, field, fallback)
|
|
275
276
|
local ok, found, err = pcall(list)
|
|
276
277
|
if not ok then
|
|
277
278
|
runner.live_miss(pending, strict, "Live list discovery failed: " .. tostring(found))
|
|
@@ -289,11 +290,57 @@ function runner.live_existing(pending, setup, strict, name, list)
|
|
|
289
290
|
if type(record) == "table" and type(record.data_get) == "function" then
|
|
290
291
|
record = record:data_get()
|
|
291
292
|
end
|
|
293
|
+
if type(record) ~= "table" or record[field] == nil then
|
|
294
|
+
if fallback == nil then
|
|
295
|
+
runner.live_miss(pending, strict, "Live entity test blocked: the listed " .. name ..
|
|
296
|
+
" record has no " .. field .. ", and neither *_ENTID nor the load route's example gives one")
|
|
297
|
+
end
|
|
298
|
+
local filled = {}
|
|
299
|
+
if type(record) == "table" then
|
|
300
|
+
for k, v in pairs(record) do
|
|
301
|
+
filled[k] = v
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
filled[field] = fallback
|
|
305
|
+
record = filled
|
|
306
|
+
end
|
|
292
307
|
setup.data.existing = setup.data.existing or {}
|
|
293
308
|
setup.data.existing[name] = { live01 = record }
|
|
294
309
|
end
|
|
295
310
|
|
|
296
311
|
|
|
312
|
+
local function live_id_str(v)
|
|
313
|
+
local whole = math.type(v) == "float" and math.tointeger(v)
|
|
314
|
+
return tostring(whole or v)
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
-- Identities compared by their string forms: a route parameter's value
|
|
319
|
+
-- is a string where the record may hold a number.
|
|
320
|
+
function runner.live_same_id(a, b)
|
|
321
|
+
if a == nil or b == nil then
|
|
322
|
+
return a == nil and b == nil
|
|
323
|
+
end
|
|
324
|
+
return live_id_str(a) == live_id_str(b)
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
-- Sleeps test.live.delayMs before a live request.
|
|
329
|
+
function runner.live_pace()
|
|
330
|
+
local ms = runner.live_delay_ms()
|
|
331
|
+
if ms <= 0 then
|
|
332
|
+
return
|
|
333
|
+
end
|
|
334
|
+
local ok, socket = pcall(require, "socket")
|
|
335
|
+
if ok and type(socket) == "table" and type(socket.sleep) == "function" then
|
|
336
|
+
socket.sleep(ms / 1000)
|
|
337
|
+
return
|
|
338
|
+
end
|
|
339
|
+
local target = os.clock() + (ms / 1000)
|
|
340
|
+
while os.clock() < target do end
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
|
|
297
344
|
-- In a lenient live run a failing check skips, observing the live API.
|
|
298
345
|
function runner.live_observe(pending, err, setup, strict)
|
|
299
346
|
local mt = getmetatable(err)
|