@voxgig/sdkgen 4.39.1 → 4.40.1

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.
Files changed (86) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/dist/helpers/testPolicy.d.ts +8 -2
  3. package/dist/helpers/testPolicy.js +18 -0
  4. package/dist/helpers/testPolicy.js.map +1 -1
  5. package/dist/sdkgen.d.ts +4 -4
  6. package/dist/sdkgen.js +4 -3
  7. package/dist/sdkgen.js.map +1 -1
  8. package/dist/tsconfig.tsbuildinfo +1 -1
  9. package/package.json +2 -2
  10. package/project/.sdk/model/target/rust.aontu +1 -0
  11. package/project/.sdk/src/cmp/c/fragment/Main.fragment.c +15 -0
  12. package/project/.sdk/src/cmp/cpp/TestDirect_cpp.ts +2 -0
  13. package/project/.sdk/src/cmp/csharp/TestDirect_csharp.ts +20 -3
  14. package/project/.sdk/src/cmp/csharp/TestEntity_csharp.ts +43 -15
  15. package/project/.sdk/src/cmp/csharp/fragment/Main.fragment.cs +17 -0
  16. package/project/.sdk/src/cmp/elixir/fragment/Main.fragment.ex +10 -0
  17. package/project/.sdk/src/cmp/go/TestDirect_go.ts +23 -4
  18. package/project/.sdk/src/cmp/go/TestEntity_go.ts +37 -12
  19. package/project/.sdk/src/cmp/go/fragment/Main.fragment.go +14 -0
  20. package/project/.sdk/src/cmp/java/TestDirect_java.ts +18 -3
  21. package/project/.sdk/src/cmp/java/TestEntity_java.ts +39 -14
  22. package/project/.sdk/src/cmp/js/TestDirect_js.ts +7 -1
  23. package/project/.sdk/src/cmp/js/fragment/Main.fragment.js +7 -0
  24. package/project/.sdk/src/cmp/kotlin/TestDirect_kotlin.ts +18 -3
  25. package/project/.sdk/src/cmp/kotlin/TestEntity_kotlin.ts +37 -14
  26. package/project/.sdk/src/cmp/lua/TestDirect_lua.ts +13 -3
  27. package/project/.sdk/src/cmp/lua/TestEntity_lua.ts +36 -10
  28. package/project/.sdk/src/cmp/lua/fragment/Main.fragment.lua +14 -0
  29. package/project/.sdk/src/cmp/ocaml/TestDirect_ocaml.ts +3 -1
  30. package/project/.sdk/src/cmp/perl/TestDirect_perl.ts +9 -3
  31. package/project/.sdk/src/cmp/perl/TestEntity_perl.ts +22 -8
  32. package/project/.sdk/src/cmp/perl/fragment/Main.fragment.pm +6 -0
  33. package/project/.sdk/src/cmp/php/TestDirect_php.ts +13 -3
  34. package/project/.sdk/src/cmp/php/TestEntity_php.ts +31 -9
  35. package/project/.sdk/src/cmp/php/fragment/Main.fragment.php +14 -0
  36. package/project/.sdk/src/cmp/py/TestDirect_py.ts +11 -3
  37. package/project/.sdk/src/cmp/py/TestEntity_py.ts +30 -9
  38. package/project/.sdk/src/cmp/py/fragment/Main.fragment.py +6 -0
  39. package/project/.sdk/src/cmp/rb/TestDirect_rb.ts +9 -3
  40. package/project/.sdk/src/cmp/rb/TestEntity_rb.ts +25 -10
  41. package/project/.sdk/src/cmp/rb/fragment/Main.fragment.rb +7 -0
  42. package/project/.sdk/src/cmp/rust/TestDirect_rust.ts +13 -3
  43. package/project/.sdk/src/cmp/rust/TestEntity_rust.ts +48 -27
  44. package/project/.sdk/src/cmp/rust/fragment/Main.fragment.rs +15 -0
  45. package/project/.sdk/src/cmp/swift/fragment/Main.fragment.swift +7 -0
  46. package/project/.sdk/src/cmp/ts/TestDirect_ts.ts +7 -1
  47. package/project/.sdk/src/cmp/ts/TestEntity_ts.ts +8 -2
  48. package/project/.sdk/src/cmp/ts/fragment/Main.fragment.ts +7 -0
  49. package/project/.sdk/src/cmp/zig/TestDirect_zig.ts +1 -0
  50. package/project/.sdk/src/cmp/zig/fragment/Main.fragment.zig +16 -0
  51. package/project/.sdk/tm/c/utility/http.c +256 -0
  52. package/project/.sdk/tm/clojure/src/sdk/client.clj +7 -0
  53. package/project/.sdk/tm/clojure/src/sdk/core.clj +24 -1
  54. package/project/.sdk/tm/cpp/core/types.hpp +14 -0
  55. package/project/.sdk/tm/csharp/test/Runner.cs +49 -3
  56. package/project/.sdk/tm/csharp/utility/Fetcher.cs +5 -1
  57. package/project/.sdk/tm/elixir/lib/projectname/utility.ex +54 -19
  58. package/project/.sdk/tm/go/test/testsupport_test.go +44 -3
  59. package/project/.sdk/tm/go/utility/fetcher.go +40 -0
  60. package/project/.sdk/tm/java/core/SdkClient.java +7 -0
  61. package/project/.sdk/tm/java/test/RunnerSupport.java +45 -2
  62. package/project/.sdk/tm/java/utility/Fetcher.java +67 -1
  63. package/project/.sdk/tm/js/src/utility/MakeResultUtility.js +3 -1
  64. package/project/.sdk/tm/js/test/live-entity.js +7 -3
  65. package/project/.sdk/tm/kotlin/core/SdkClient.kt +7 -0
  66. package/project/.sdk/tm/kotlin/test/RunnerSupport.kt +40 -3
  67. package/project/.sdk/tm/kotlin/utility/Fetcher.kt +52 -1
  68. package/project/.sdk/tm/lua/test/runner.lua +49 -2
  69. package/project/.sdk/tm/lua/utility/fetcher.lua +228 -0
  70. package/project/.sdk/tm/ocaml/sdk_features.ml +8 -0
  71. package/project/.sdk/tm/perl/t/runner.pm +18 -3
  72. package/project/.sdk/tm/perl/utility/fetcher.pm +42 -3
  73. package/project/.sdk/tm/php/test/Runner.php +43 -5
  74. package/project/.sdk/tm/php/utility/Fetcher.php +36 -0
  75. package/project/.sdk/tm/php/utility/MakeResult.php +3 -1
  76. package/project/.sdk/tm/py/test/runner.py +32 -3
  77. package/project/.sdk/tm/rb/test/runner.rb +24 -1
  78. package/project/.sdk/tm/rust/tests/common/mod.rs +53 -5
  79. package/project/.sdk/tm/rust/utility/fetcher.rs +43 -3
  80. package/project/.sdk/tm/scala/core/SdkClient.scala +7 -0
  81. package/project/.sdk/tm/scala/utility/Fetcher.scala +48 -1
  82. package/project/.sdk/tm/ts/src/utility/MakeResultUtility.ts +3 -1
  83. package/project/.sdk/tm/ts/test/live-entity.ts +8 -3
  84. package/project/sdkgen-package.json +1 -1
  85. package/src/helpers/testPolicy.ts +23 -2
  86. package/src/sdkgen.ts +4 -2
@@ -660,6 +660,261 @@ static const char* read_response(HttpConn* c, HttpResp* r, int head) {
660
660
  return NULL;
661
661
  }
662
662
 
663
+ // ---- content coding ------------------------------------------------------------
664
+
665
+ // Inflate: a server may compress its answer whatever the request
666
+ // asked for. Canonical Huffman decoding a bit at a time, as zlib's puff.c
667
+ // does, so there is no library to link.
668
+ typedef struct {
669
+ short count[16];
670
+ short symbol[288];
671
+ } InfHuff;
672
+
673
+ typedef struct {
674
+ const unsigned char* in;
675
+ size_t len;
676
+ size_t pos;
677
+ unsigned long buf;
678
+ int cnt;
679
+ HttpBuf* out;
680
+ const char* err;
681
+ } Inf;
682
+
683
+ static const short INF_LBASE[29] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
684
+ 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258 };
685
+ static const short INF_LEXT[29] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
686
+ 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 };
687
+ static const short INF_DBASE[30] = { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
688
+ 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 };
689
+ static const short INF_DEXT[30] = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
690
+ 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 };
691
+
692
+ static int inf_bits(Inf* s, int need) {
693
+ unsigned long val = s->buf;
694
+ while (s->cnt < need) {
695
+ if (s->pos >= s->len) {
696
+ s->err = "it ends early";
697
+ return 0;
698
+ }
699
+ val |= (unsigned long)s->in[s->pos++] << s->cnt;
700
+ s->cnt += 8;
701
+ }
702
+ s->buf = val >> need;
703
+ s->cnt -= need;
704
+ return (int)(val & ((1UL << need) - 1));
705
+ }
706
+
707
+ static void inf_huff(InfHuff* h, const unsigned char* lens, int n) {
708
+ short offs[16];
709
+ memset(h, 0, sizeof(*h));
710
+ for (int sym = 0; sym < n; sym++) h->count[lens[sym]]++;
711
+ offs[1] = 0;
712
+ for (int len = 1; len < 15; len++) offs[len + 1] = (short)(offs[len] + h->count[len]);
713
+ for (int sym = 0; sym < n; sym++) {
714
+ if (0 != lens[sym]) h->symbol[offs[lens[sym]]++] = (short)sym;
715
+ }
716
+ }
717
+
718
+ static int inf_decode(Inf* s, const InfHuff* h) {
719
+ int code = 0, first = 0, index = 0;
720
+ for (int len = 1; len < 16; len++) {
721
+ code |= inf_bits(s, 1);
722
+ if (NULL != s->err) return -1;
723
+ int count = h->count[len];
724
+ if (code - count < first) return h->symbol[index + (code - first)];
725
+ index += count;
726
+ first = (first + count) << 1;
727
+ code <<= 1;
728
+ }
729
+ s->err = "it holds an invalid code";
730
+ return -1;
731
+ }
732
+
733
+ static void inf_byte(Inf* s, char ch) {
734
+ if ((size_t)HTTP_MAX_BYTES <= s->out->len) {
735
+ s->err = "it inflates past the response bound";
736
+ return;
737
+ }
738
+ buf_add(s->out, &ch, 1);
739
+ }
740
+
741
+ static void inf_codes(Inf* s, const InfHuff* lencode, const InfHuff* distcode) {
742
+ for (;;) {
743
+ int sym = inf_decode(s, lencode);
744
+ if (NULL != s->err || 256 == sym) return;
745
+ if (sym < 256) {
746
+ inf_byte(s, (char)sym);
747
+ if (NULL != s->err) return;
748
+ continue;
749
+ }
750
+ sym -= 257;
751
+ if (29 <= sym) {
752
+ s->err = "it holds an invalid length";
753
+ return;
754
+ }
755
+ int len = INF_LBASE[sym] + inf_bits(s, INF_LEXT[sym]);
756
+ int dsym = inf_decode(s, distcode);
757
+ if (NULL != s->err) return;
758
+ if (30 <= dsym) {
759
+ s->err = "it holds an invalid distance";
760
+ return;
761
+ }
762
+ size_t dist = (size_t)INF_DBASE[dsym] + (size_t)inf_bits(s, INF_DEXT[dsym]);
763
+ if (NULL != s->err) return;
764
+ if (dist > s->out->len) {
765
+ s->err = "it reaches back past its start";
766
+ return;
767
+ }
768
+ for (int i = 0; i < len && NULL == s->err; i++) {
769
+ inf_byte(s, s->out->data[s->out->len - dist]);
770
+ }
771
+ if (NULL != s->err) return;
772
+ }
773
+ }
774
+
775
+ static void inf_dynamic(Inf* s) {
776
+ static const unsigned char order[19] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13,
777
+ 2, 14, 1, 15 };
778
+ unsigned char lens[320];
779
+ InfHuff lencode, distcode;
780
+ int nlen = inf_bits(s, 5) + 257;
781
+ int ndist = inf_bits(s, 5) + 1;
782
+ int ncode = inf_bits(s, 4) + 4;
783
+ if (NULL != s->err) return;
784
+ if (286 < nlen || 30 < ndist) {
785
+ s->err = "it declares too many codes";
786
+ return;
787
+ }
788
+ memset(lens, 0, sizeof(lens));
789
+ for (int i = 0; i < ncode; i++) lens[order[i]] = (unsigned char)inf_bits(s, 3);
790
+ if (NULL != s->err) return;
791
+ inf_huff(&lencode, lens, 19);
792
+ memset(lens, 0, sizeof(lens));
793
+ int index = 0;
794
+ while (index < nlen + ndist) {
795
+ int sym = inf_decode(s, &lencode);
796
+ if (NULL != s->err) return;
797
+ if (sym < 16) {
798
+ lens[index++] = (unsigned char)sym;
799
+ continue;
800
+ }
801
+ unsigned char len = 0;
802
+ int rep;
803
+ if (16 == sym) {
804
+ if (0 == index) {
805
+ s->err = "it repeats a length before the first";
806
+ return;
807
+ }
808
+ len = lens[index - 1];
809
+ rep = 3 + inf_bits(s, 2);
810
+ }
811
+ else if (17 == sym) rep = 3 + inf_bits(s, 3);
812
+ else rep = 11 + inf_bits(s, 7);
813
+ if (NULL != s->err) return;
814
+ if (index + rep > nlen + ndist) {
815
+ s->err = "it declares too many lengths";
816
+ return;
817
+ }
818
+ while (rep--) lens[index++] = len;
819
+ }
820
+ if (0 == lens[256]) {
821
+ s->err = "it has no end-of-block code";
822
+ return;
823
+ }
824
+ inf_huff(&lencode, lens, nlen);
825
+ inf_huff(&distcode, lens + nlen, ndist);
826
+ inf_codes(s, &lencode, &distcode);
827
+ }
828
+
829
+ static const char* inflate_raw(const unsigned char* in, size_t len, HttpBuf* out) {
830
+ Inf s;
831
+ memset(&s, 0, sizeof(s));
832
+ s.in = in;
833
+ s.len = len;
834
+ s.out = out;
835
+ int last;
836
+ do {
837
+ last = inf_bits(&s, 1);
838
+ int kind = inf_bits(&s, 2);
839
+ if (NULL != s.err) return s.err;
840
+ if (0 == kind) {
841
+ // A stored block starts on a byte boundary: what is left of this byte goes.
842
+ s.buf = 0;
843
+ s.cnt = 0;
844
+ if (s.pos + 4 > s.len) return "it ends early";
845
+ size_t n = (size_t)s.in[s.pos] | ((size_t)s.in[s.pos + 1] << 8);
846
+ size_t check = (size_t)s.in[s.pos + 2] | ((size_t)s.in[s.pos + 3] << 8);
847
+ if (n != (~check & 0xffff)) return "a stored block's length is corrupt";
848
+ s.pos += 4;
849
+ if (s.pos + n > s.len) return "it ends early";
850
+ if ((size_t)HTTP_MAX_BYTES < s.out->len + n) return "it inflates past the response bound";
851
+ buf_add(s.out, (const char*)s.in + s.pos, n);
852
+ s.pos += n;
853
+ }
854
+ else if (1 == kind) {
855
+ InfHuff fixlen, fixdist;
856
+ unsigned char lens[288];
857
+ int sym = 0;
858
+ for (; sym < 144; sym++) lens[sym] = 8;
859
+ for (; sym < 256; sym++) lens[sym] = 9;
860
+ for (; sym < 280; sym++) lens[sym] = 7;
861
+ for (; sym < 288; sym++) lens[sym] = 8;
862
+ inf_huff(&fixlen, lens, 288);
863
+ for (sym = 0; sym < 30; sym++) lens[sym] = 5;
864
+ inf_huff(&fixdist, lens, 30);
865
+ inf_codes(&s, &fixlen, &fixdist);
866
+ }
867
+ else if (2 == kind) inf_dynamic(&s);
868
+ else return "it holds an invalid block type";
869
+ if (NULL != s.err) return s.err;
870
+ } while (!last);
871
+ return NULL;
872
+ }
873
+
874
+ // A gzip or deflate body decoded. Deflate is zlib-wrapped,
875
+ // or raw as some servers send it.
876
+ static const char* decode_body(HttpResp* r) {
877
+ const char* enc = get_str(r->headers, "content-encoding");
878
+ if (NULL == enc || 0 == r->body.len) return NULL;
879
+ while (isspace((unsigned char)*enc)) enc++;
880
+ size_t elen = strlen(enc);
881
+ while (0 < elen && isspace((unsigned char)enc[elen - 1])) elen--;
882
+ int gzip = ieqn(enc, elen, "gzip") || ieqn(enc, elen, "x-gzip");
883
+ if (!gzip && !ieqn(enc, elen, "deflate")) return NULL;
884
+
885
+ const unsigned char* in = (const unsigned char*)r->body.data;
886
+ size_t len = r->body.len;
887
+ size_t at = 0;
888
+ const char* bad = NULL;
889
+ if (gzip) {
890
+ if (len < 18 || 0x1f != in[0] || 0x8b != in[1] || 8 != in[2]) bad = "it is not gzip data";
891
+ else {
892
+ int flags = in[3];
893
+ at = 10;
894
+ if (flags & 4) at = at + 2 <= len ? at + 2 + ((size_t)in[at] | ((size_t)in[at + 1] << 8)) : len;
895
+ if (flags & 8) { while (at < len && 0 != in[at]) at++; at++; }
896
+ if (flags & 16) { while (at < len && 0 != in[at]) at++; at++; }
897
+ if (flags & 2) at += 2;
898
+ if (at >= len) bad = "it ends early";
899
+ }
900
+ }
901
+ else if (2 <= len && 8 == (in[0] & 0x0f) && 0 == (((unsigned)in[0] << 8) | in[1]) % 31) {
902
+ at = 2;
903
+ }
904
+
905
+ HttpBuf out = { NULL, 0, 0 };
906
+ if (NULL == bad) bad = inflate_raw(in + at, len - at, &out);
907
+ if (NULL != bad) {
908
+ free(out.data);
909
+ snprintf(r->why, sizeof(r->why), "its %.*s body is not valid: %s", (int)elen, enc, bad);
910
+ return r->why;
911
+ }
912
+ if (NULL == out.data) buf_add(&out, "", 0);
913
+ free(r->body.data);
914
+ r->body = out;
915
+ return NULL;
916
+ }
917
+
663
918
  // ---- JSON ----------------------------------------------------------------------
664
919
 
665
920
  // A strict recognizer, because the vendored parser reads a truncated
@@ -1000,6 +1255,7 @@ voxgig_value* http_fetch(Context* ctx, const char* url, voxgig_value* fetchdef,
1000
1255
  if (NULL == bad) bad = read_response(&c, &r, ieq(method, "HEAD"));
1001
1256
  if (NULL != c.tls) http_tls_close(c.tls);
1002
1257
  if (0 <= c.fd) close(c.fd);
1258
+ if (NULL == bad) bad = decode_body(&r);
1003
1259
  if (NULL != bad) {
1004
1260
  *err = transport_error(ctx, url, "the request failed", bad);
1005
1261
  free(r.raw.data);
@@ -97,6 +97,13 @@
97
97
  (when (vs/ismap uh)
98
98
  (doseq [item (or (vs/items uh) [])]
99
99
  (.put ^java.util.Map (core/oget spec :headers) (vs/getprop item 0) (vs/getprop item 1)))))
100
+ ;; A raw request has no point to declare its response type, so it asks
101
+ ;; for the JSON it reads: an API that negotiates on Accept otherwise
102
+ ;; answers in its own default format. A caller's own accept wins.
103
+ (let [^java.util.Map sh (core/oget spec :headers)]
104
+ (when (and (instance? java.util.Map sh)
105
+ (not-any? #(= "accept" (.toLowerCase (str %) java.util.Locale/ROOT)) (.keySet sh)))
106
+ (.put sh "accept" "application/json")))
100
107
  (let [[_ err] ((core/uget ctx :prepare-auth) ctx)]
101
108
  (when err (core/sdk-throw err)))
102
109
  (let [[fetchdef fd-err] ((core/uget ctx :make-fetch-def) ctx)]
@@ -1810,6 +1810,27 @@
1810
1810
  (def ^:private http-client (delay (http-client-for HttpClient$Redirect/NORMAL)))
1811
1811
  (def ^:private manual-http-client (delay (http-client-for HttpClient$Redirect/NEVER)))
1812
1812
 
1813
+ ;; java.net.http decodes no Content-Encoding, and a server may compress its
1814
+ ;; answer whatever the request asked for. Deflate is zlib-wrapped, or raw as
1815
+ ;; some servers send it.
1816
+ (defn- decode-body ^bytes [^bytes body encoding]
1817
+ (let [enc (str/lower-case (str/trim (str encoding)))
1818
+ n (if (nil? body) 0 (alength body))]
1819
+ (cond
1820
+ (zero? n) body
1821
+ (#{"gzip" "x-gzip"} enc)
1822
+ (with-open [in (java.util.zip.GZIPInputStream. (java.io.ByteArrayInputStream. body))]
1823
+ (.readAllBytes ^java.io.InputStream in))
1824
+ (= "deflate" enc)
1825
+ (let [b0 (bit-and (aget body 0) 0xff)
1826
+ b1 (if (< 1 n) (bit-and (aget body 1) 0xff) 0)
1827
+ zlib (and (< 1 n) (= 8 (bit-and b0 0x0f))
1828
+ (zero? (mod (bit-or (bit-shift-left b0 8) b1) 31)))]
1829
+ (with-open [in (java.util.zip.InflaterInputStream.
1830
+ (java.io.ByteArrayInputStream. body) (java.util.zip.Inflater. (not zlib)))]
1831
+ (.readAllBytes ^java.io.InputStream in)))
1832
+ :else body)))
1833
+
1813
1834
  (defn- body-publisher [body]
1814
1835
  (cond
1815
1836
  (string? body) (HttpRequest$BodyPublishers/ofString body)
@@ -1850,7 +1871,9 @@
1850
1871
  (vs/setprop headers "user-agent" ua)))
1851
1872
  (let [client (if (= "manual" (vs/getprop fetchdef "redirect")) @manual-http-client @http-client)
1852
1873
  resp (.send ^HttpClient client (.build builder) (HttpResponse$BodyHandlers/ofByteArray))
1853
- body (String. ^bytes (.body resp) "UTF-8")
1874
+ body (String. ^bytes (decode-body (.body resp)
1875
+ (.orElse (.firstValue (.headers resp) "content-encoding") ""))
1876
+ "UTF-8")
1854
1877
  resp-headers (vs/jm)]
1855
1878
  (doseq [[k vlist] (.map (.headers resp))]
1856
1879
  (.put ^java.util.Map resp-headers (str/lower-case k) (str/join "," vlist)))
@@ -1054,6 +1054,20 @@ inline Value SdkClient::prepare(const Value& fetchargs_) {
1054
1054
  }
1055
1055
  }
1056
1056
 
1057
+ // A raw request has no point to declare its response type, so it asks for
1058
+ // the JSON it reads: an API that negotiates on Accept otherwise answers in
1059
+ // its own default format. A caller's own accept wins.
1060
+ bool hasAccept = false;
1061
+ for (const auto& item : Struct::items(ctx->spec->headers)) {
1062
+ std::string key = pair_key(item).as_string();
1063
+ for (auto& ch : key) ch = (char)std::tolower((unsigned char)ch);
1064
+ if ("accept" == key) {
1065
+ hasAccept = true;
1066
+ break;
1067
+ }
1068
+ }
1069
+ if (!hasAccept) map_put(ctx->spec->headers, "accept", Value("application/json"));
1070
+
1057
1071
  u->prepareAuth(ctx);
1058
1072
 
1059
1073
  return u->makeFetchDef(ctx);
@@ -555,10 +555,10 @@ public static class TestRunner
555
555
  }
556
556
 
557
557
  // The record a create-less flow reads live: the first its list returns,
558
- // put where the flow reads the fixture's existing records. False once
559
- // the test is settled and has to return.
558
+ // put where the flow reads the fixture's existing records, a record with
559
+ // no identity taking the fallback. False once the test is settled.
560
560
  public static bool LiveExisting(Dictionary<string, object?> data, bool strict, string name,
561
- Func<object?> list)
561
+ string field, object? fallback, Func<object?> list)
562
562
  {
563
563
  object? found;
564
564
  try
@@ -581,6 +581,20 @@ public static class TestRunner
581
581
  return false;
582
582
  }
583
583
  var record = items[0] is IEntity ent ? ent.Data() : items[0];
584
+ var fields = record as Dictionary<string, object?>;
585
+ if (fields == null || !fields.TryGetValue(field, out var held) || held == null)
586
+ {
587
+ if (fallback == null)
588
+ {
589
+ LiveMiss(strict, "Live entity test blocked: the listed " + name + " record has no " + field
590
+ + ", and neither *_ENTID nor the load route's example gives one");
591
+ return false;
592
+ }
593
+ var filled = fields == null
594
+ ? new Dictionary<string, object?>() : new Dictionary<string, object?>(fields);
595
+ filled[field] = fallback;
596
+ record = filled;
597
+ }
584
598
  if (!(data.TryGetValue("existing", out var existing) &&
585
599
  existing is Dictionary<string, object?> emap))
586
600
  {
@@ -591,6 +605,38 @@ public static class TestRunner
591
605
  return true;
592
606
  }
593
607
 
608
+ // Identities compared by their string forms: a route parameter's
609
+ // value is a string where the record may hold a number.
610
+ public static bool LiveSameId(object? a, object? b)
611
+ {
612
+ if (a == null || b == null)
613
+ {
614
+ return a == null && b == null;
615
+ }
616
+ return LiveIdString(a) == LiveIdString(b);
617
+ }
618
+
619
+ private static string LiveIdString(object v)
620
+ {
621
+ var invariant = System.Globalization.CultureInfo.InvariantCulture;
622
+ switch (v)
623
+ {
624
+ case bool flag:
625
+ return flag ? "true" : "false";
626
+ case double d when !double.IsInfinity(d) && d == Math.Round(d) && Math.Abs(d) < 9.0e15:
627
+ return ((long)d).ToString(invariant);
628
+ case IFormattable formattable:
629
+ return formattable.ToString(null, invariant);
630
+ }
631
+ return v.ToString() ?? "";
632
+ }
633
+
634
+ // Sleeps test.live.delayMs before a live request.
635
+ public static void LivePace()
636
+ {
637
+ System.Threading.Thread.Sleep(LiveDelayMs());
638
+ }
639
+
594
640
  // In a lenient live run a failing check is reported and the test
595
641
  // returns, observing the live API rather than failing on it.
596
642
  public static void LiveObserve(Exception err, bool live, bool strict)
@@ -16,13 +16,17 @@ public static partial class SdkUtility
16
16
  // different credential — and .NET ADDS container cookies to a request
17
17
  // that already carries a Cookie header, which an `apiKey in: cookie`
18
18
  // scheme does. Pooling is the handler's own and is unaffected. Past five
19
- // redirects the last 3xx is the answer, as in every target.
19
+ // redirects the last 3xx is the answer, as in every target. A gzip or
20
+ // deflate body is decoded, which .NET leaves to the handler to ask for: a
21
+ // server may compress its answer whatever the request asked for.
20
22
  private static HttpClientHandler CookielessHandler(bool allowRedirect) =>
21
23
  new HttpClientHandler
22
24
  {
23
25
  AllowAutoRedirect = allowRedirect,
24
26
  MaxAutomaticRedirections = 5,
25
27
  UseCookies = false,
28
+ AutomaticDecompression =
29
+ System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate,
26
30
  };
27
31
 
28
32
  private static readonly HttpClient DefaultHttpClient = new(CookielessHandler(true));
@@ -2548,34 +2548,69 @@ defmodule ProjectName.Utility do
2548
2548
  S.setprop(acc, String.downcase(to_string(k)), to_string(v))
2549
2549
  end)
2550
2550
 
2551
- body_str = if is_binary(resp_body), do: resp_body, else: to_string(resp_body)
2551
+ raw_body = if is_binary(resp_body), do: resp_body, else: to_string(resp_body)
2552
+
2553
+ case decode_body(raw_body, S.getprop(rh, "content-encoding")) do
2554
+ {:ok, body_str} ->
2555
+ {json_body, unreadable} =
2556
+ if String.trim(body_str) != "" do
2557
+ case safe_json(body_str) do
2558
+ {:ok, v} -> {v, false}
2559
+ _ -> {nil, true}
2560
+ end
2561
+ else
2562
+ {nil, false}
2563
+ end
2552
2564
 
2553
- {json_body, unreadable} =
2554
- if String.trim(body_str) != "" do
2555
- case safe_json(body_str) do
2556
- {:ok, v} -> {v, false}
2557
- _ -> {nil, true}
2558
- end
2559
- else
2560
- {nil, false}
2561
- end
2565
+ status_text = if status < 400, do: "OK", else: "Error"
2562
2566
 
2563
- status_text = if status < 400, do: "OK", else: "Error"
2567
+ {S.jm([
2568
+ "status", status,
2569
+ "statusText", status_text,
2570
+ "headers", rh,
2571
+ "json", fn -> json_body end,
2572
+ "body", body_str,
2573
+ "unreadable", unreadable
2574
+ ]), nil}
2564
2575
 
2565
- {S.jm([
2566
- "status", status,
2567
- "statusText", status_text,
2568
- "headers", rh,
2569
- "json", fn -> json_body end,
2570
- "body", body_str,
2571
- "unreadable", unreadable
2572
- ]), nil}
2576
+ {:error, message} ->
2577
+ {nil, message}
2578
+ end
2573
2579
 
2574
2580
  {:error, reason} ->
2575
2581
  {nil, inspect(reason)}
2576
2582
  end
2577
2583
  end
2578
2584
 
2585
+ # :httpc decodes no Content-Encoding, and a server may compress its answer
2586
+ # whatever the request asked for. Deflate is zlib-wrapped,
2587
+ # or raw as some servers send it.
2588
+ defp decode_body("", _encoding), do: {:ok, ""}
2589
+
2590
+ defp decode_body(body, encoding) do
2591
+ enc = encoding |> to_string() |> String.trim() |> String.downcase()
2592
+
2593
+ try do
2594
+ case enc do
2595
+ e when e in ["gzip", "x-gzip"] ->
2596
+ {:ok, :zlib.gunzip(body)}
2597
+
2598
+ "deflate" ->
2599
+ {:ok,
2600
+ try do
2601
+ :zlib.uncompress(body)
2602
+ rescue
2603
+ _ -> :zlib.unzip(body)
2604
+ end}
2605
+
2606
+ _ ->
2607
+ {:ok, body}
2608
+ end
2609
+ rescue
2610
+ e -> {:error, "response body is not valid " <> enc <> " data: " <> Exception.message(e)}
2611
+ end
2612
+ end
2613
+
2579
2614
  # Parse a JSON string into struct nodes using the vendored struct's own
2580
2615
  # constructors (no third-party dep). Returns {:ok, node} | :error.
2581
2616
  defp safe_json(str) do
@@ -8,9 +8,11 @@ import (
8
8
  "path/filepath"
9
9
  "runtime"
10
10
  "sort"
11
+ "strconv"
11
12
  "strings"
12
13
  "sync"
13
14
  "testing"
15
+ "time"
14
16
 
15
17
  sdk "GOMODULE"
16
18
  )
@@ -348,9 +350,10 @@ func (o liveObserver) Error(args ...any) {
348
350
  }
349
351
 
350
352
  // liveExisting finds the record a create-less flow reads live: the first its
351
- // list returns, put where the flow reads the fixture's existing records.
353
+ // list returns, put where the flow reads the fixture's existing records. A
354
+ // record with no value in the identity field takes the fallback identity.
352
355
  func liveExisting(t testing.TB, strict bool, data map[string]any, name string,
353
- list func() (any, error)) {
356
+ field string, fallback any, list func() (any, error)) {
354
357
  t.Helper()
355
358
  found, err := list()
356
359
  if err != nil {
@@ -366,10 +369,48 @@ func liveExisting(t testing.TB, strict bool, data map[string]any, name string,
366
369
  liveEmpty(t, "The account has no %s record to load", name)
367
370
  return
368
371
  }
372
+ record := entityData(items[0])
373
+ if fields, _ := record.(map[string]any); fields == nil || fields[field] == nil {
374
+ if fallback == nil {
375
+ liveMiss(t, strict, "Live entity test blocked: the listed %s record has no %s, "+
376
+ "and neither *_ENTID nor the load route's example gives one", name, field)
377
+ return
378
+ }
379
+ filled := map[string]any{}
380
+ for k, v := range fields {
381
+ filled[k] = v
382
+ }
383
+ filled[field] = fallback
384
+ record = filled
385
+ }
369
386
  existing, ok := data["existing"].(map[string]any)
370
387
  if !ok {
371
388
  existing = map[string]any{}
372
389
  data["existing"] = existing
373
390
  }
374
- existing[name] = map[string]any{"live01": entityData(items[0])}
391
+ existing[name] = map[string]any{"live01": record}
392
+ }
393
+
394
+ // liveSameId compares identities by their string forms: a route
395
+ // parameter's value is a string where the record may hold a number.
396
+ func liveSameId(a, b any) bool {
397
+ if a == nil || b == nil {
398
+ return a == nil && b == nil
399
+ }
400
+ return liveIdString(a) == liveIdString(b)
401
+ }
402
+
403
+ func liveIdString(v any) string {
404
+ switch n := v.(type) {
405
+ case float64:
406
+ return strconv.FormatFloat(n, 'f', -1, 64)
407
+ case float32:
408
+ return strconv.FormatFloat(float64(n), 'f', -1, 32)
409
+ }
410
+ return fmt.Sprint(v)
411
+ }
412
+
413
+ // livePace sleeps test.live.delayMs before a live request.
414
+ func livePace() {
415
+ time.Sleep(time.Duration(liveDelayMs()) * time.Millisecond)
375
416
  }
@@ -2,7 +2,11 @@ package utility
2
2
 
3
3
  import (
4
4
  "bytes"
5
+ "compress/flate"
6
+ "compress/gzip"
7
+ "compress/zlib"
5
8
  "encoding/json"
9
+ "fmt"
6
10
  "io"
7
11
  "net/http"
8
12
  "net/url"
@@ -145,6 +149,10 @@ func defaultHTTPFetch(fullurl string, fetchdef map[string]any) (map[string]any,
145
149
  if err != nil {
146
150
  return nil, err
147
151
  }
152
+ bodyBytes, err = decodeBody(bodyBytes, resp.Header.Get("Content-Encoding"))
153
+ if err != nil {
154
+ return nil, err
155
+ }
148
156
 
149
157
  headers := map[string]any{}
150
158
  for k, vals := range resp.Header {
@@ -176,6 +184,38 @@ func defaultHTTPFetch(fullurl string, fetchdef map[string]any) (map[string]any,
176
184
  }, nil
177
185
  }
178
186
 
187
+ // A body net/http left encoded: it decodes gzip only when it asked for gzip
188
+ // itself, and deflate never. Deflate is zlib-wrapped, or raw
189
+ // as some servers send it.
190
+ func decodeBody(body []byte, encoding string) ([]byte, error) {
191
+ encoding = strings.ToLower(strings.TrimSpace(encoding))
192
+ if len(body) == 0 {
193
+ return body, nil
194
+ }
195
+ var reader io.Reader
196
+ switch encoding {
197
+ case "gzip", "x-gzip":
198
+ zr, err := gzip.NewReader(bytes.NewReader(body))
199
+ if err != nil {
200
+ return nil, fmt.Errorf("response body is not valid %s data: %w", encoding, err)
201
+ }
202
+ reader = zr
203
+ case "deflate":
204
+ if zr, err := zlib.NewReader(bytes.NewReader(body)); err == nil {
205
+ reader = zr
206
+ } else {
207
+ reader = flate.NewReader(bytes.NewReader(body))
208
+ }
209
+ default:
210
+ return body, nil
211
+ }
212
+ out, err := io.ReadAll(reader)
213
+ if err != nil {
214
+ return nil, fmt.Errorf("response body is not valid %s data: %w", encoding, err)
215
+ }
216
+ return out, nil
217
+ }
218
+
179
219
  func fetcherUtil(ctx *core.Context, fullurl string, fetchdef map[string]any) (any, error) {
180
220
  if ctx.Client.Mode != "live" {
181
221
  return nil, ctx.MakeError("fetch_mode_block",
@@ -181,6 +181,13 @@ public abstract class SdkClient {
181
181
  ctx.spec.headers.putAll(uheaders);
182
182
  }
183
183
 
184
+ // A raw request has no point to declare its response type, so it asks
185
+ // for the JSON it reads: an API that negotiates on Accept otherwise
186
+ // answers in its own default format. A caller's own accept wins.
187
+ if (ctx.spec.headers.keySet().stream().noneMatch(k -> "accept".equalsIgnoreCase(k))) {
188
+ ctx.spec.headers.put("accept", "application/json");
189
+ }
190
+
184
191
  utility.prepareAuth.apply(ctx);
185
192
 
186
193
  return utility.makeFetchDef.apply(ctx);