@voxgig/sdkgen 4.48.1 → 4.49.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 (119) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/package.json +1 -1
  3. package/project/.sdk/model/feature/secrets.aontu +3 -0
  4. package/project/.sdk/src/cmp/c/Config_c.ts +6 -1
  5. package/project/.sdk/src/cmp/c/fragment/Entity.fragment.c +0 -10
  6. package/project/.sdk/src/cmp/clojure/fragment/Entity.fragment.clj +0 -9
  7. package/project/.sdk/src/cmp/csharp/fragment/EntityBase.fragment.cs +0 -11
  8. package/project/.sdk/src/cmp/go/fragment/Entity.fragment.go +0 -7
  9. package/project/.sdk/src/cmp/java/fragment/EntityBase.fragment.java +0 -10
  10. package/project/.sdk/src/cmp/js/fragment/EntityBase.fragment.js +2 -11
  11. package/project/.sdk/src/cmp/kotlin/fragment/EntityBase.fragment.kt +0 -10
  12. package/project/.sdk/src/cmp/lua/fragment/Entity.fragment.lua +0 -11
  13. package/project/.sdk/src/cmp/ocaml/Entity_ocaml.ts +2 -10
  14. package/project/.sdk/src/cmp/perl/fragment/Entity.fragment.pm +0 -13
  15. package/project/.sdk/src/cmp/php/fragment/Entity.fragment.php +0 -10
  16. package/project/.sdk/src/cmp/py/fragment/Entity.fragment.py +0 -10
  17. package/project/.sdk/src/cmp/rb/fragment/Entity.fragment.rb +0 -10
  18. package/project/.sdk/src/cmp/rust/fragment/Entity.fragment.rs +0 -12
  19. package/project/.sdk/src/cmp/scala/fragment/EntityBase.fragment.scala +0 -10
  20. package/project/.sdk/src/cmp/swift/fragment/EntityBase.fragment.swift +0 -9
  21. package/project/.sdk/src/cmp/ts/fragment/EntityBase.fragment.ts +0 -7
  22. package/project/.sdk/src/cmp/zig/fragment/Entity.fragment.zig +1 -14
  23. package/project/.sdk/tm/c/feature/cache.c +16 -1
  24. package/project/.sdk/tm/c/feature/secrets.c +149 -18
  25. package/project/.sdk/tm/c/tests/feature/secrets/secrets_test.c +319 -70
  26. package/project/.sdk/tm/c/tests/feature_test.c +31 -0
  27. package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +74 -29
  28. package/project/.sdk/tm/clojure/src/sdk/core.clj +55 -6
  29. package/project/.sdk/tm/clojure/src/sdk/features.clj +16 -2
  30. package/project/.sdk/tm/clojure/test/sdk/test/feature/secrets.clj +191 -48
  31. package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +16 -0
  32. package/project/.sdk/tm/cpp/core/types.hpp +1 -11
  33. package/project/.sdk/tm/cpp/feature/cache.hpp +14 -1
  34. package/project/.sdk/tm/cpp/feature/secrets.hpp +133 -18
  35. package/project/.sdk/tm/cpp/test/feature/secrets/secrets_test.cpp +175 -8
  36. package/project/.sdk/tm/cpp/test/feature_test.cpp +27 -0
  37. package/project/.sdk/tm/csharp/core/Control.cs +0 -5
  38. package/project/.sdk/tm/csharp/feature/CacheFeature.cs +16 -0
  39. package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +77 -38
  40. package/project/.sdk/tm/csharp/test/FeatureTest.cs +29 -0
  41. package/project/.sdk/tm/csharp/test/feature/secrets/SecretsFeatureTest.cs +315 -68
  42. package/project/.sdk/tm/elixir/lib/projectname/entity_base.ex +0 -13
  43. package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +32 -10
  44. package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +78 -22
  45. package/project/.sdk/tm/elixir/test/feature/secrets/secrets_test.exs +178 -19
  46. package/project/.sdk/tm/elixir/test/feature_test.exs +20 -0
  47. package/project/.sdk/tm/go/feature/cache_feature.go +14 -1
  48. package/project/.sdk/tm/go/feature/secrets_feature.go +110 -15
  49. package/project/.sdk/tm/go/test/feature/secrets/secrets_feature_test.go +373 -87
  50. package/project/.sdk/tm/go/test/feature_test.go +32 -0
  51. package/project/.sdk/tm/go/utility/media.go +2 -2
  52. package/project/.sdk/tm/java/core/Control.java +0 -5
  53. package/project/.sdk/tm/java/feature/CacheFeature.java +12 -0
  54. package/project/.sdk/tm/java/feature/SecretsFeature.java +104 -34
  55. package/project/.sdk/tm/java/test/FeatureTest.java +25 -0
  56. package/project/.sdk/tm/java/test/PrimaryUtilityTest.java +39 -0
  57. package/project/.sdk/tm/java/test/feature/secrets/SecretsFeatureTest.java +267 -53
  58. package/project/.sdk/tm/java/utility/Fetcher.java +18 -2
  59. package/project/.sdk/tm/java/utility/Media.java +2 -2
  60. package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +13 -0
  61. package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +79 -29
  62. package/project/.sdk/tm/js/test/feature/secrets/Secrets.test.js +282 -93
  63. package/project/.sdk/tm/js/test/feature.test.js +17 -0
  64. package/project/.sdk/tm/kotlin/core/Control.kt +0 -5
  65. package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +12 -0
  66. package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +84 -24
  67. package/project/.sdk/tm/kotlin/test/FeatureTest.kt +24 -0
  68. package/project/.sdk/tm/kotlin/test/PrimaryUtilityTest.kt +35 -0
  69. package/project/.sdk/tm/kotlin/test/feature/secrets/SecretsTest.kt +236 -54
  70. package/project/.sdk/tm/kotlin/utility/Fetcher.kt +13 -1
  71. package/project/.sdk/tm/lua/feature/cache_feature.lua +20 -0
  72. package/project/.sdk/tm/lua/feature/secrets_feature.lua +92 -15
  73. package/project/.sdk/tm/lua/test/feature/secrets/secrets_feature_test.lua +208 -43
  74. package/project/.sdk/tm/lua/test/feature_test.lua +18 -0
  75. package/project/.sdk/tm/lua/utility/clean.lua +1 -0
  76. package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +100 -14
  77. package/project/.sdk/tm/ocaml/sdk_features.ml +7 -1
  78. package/project/.sdk/tm/ocaml/test/feature/secrets/t_secrets.ml +143 -7
  79. package/project/.sdk/tm/ocaml/test/t_feature.ml +15 -0
  80. package/project/.sdk/tm/perl/feature/cache_feature.pm +13 -0
  81. package/project/.sdk/tm/perl/feature/secrets_feature.pm +87 -32
  82. package/project/.sdk/tm/perl/t/feature/secrets/secrets.t +177 -24
  83. package/project/.sdk/tm/perl/t/feature.t +24 -0
  84. package/project/.sdk/tm/php/feature/CacheFeature.php +13 -0
  85. package/project/.sdk/tm/php/feature/SecretsFeature.php +79 -23
  86. package/project/.sdk/tm/php/test/FeatureTest.php +23 -0
  87. package/project/.sdk/tm/php/test/feature/secrets/SecretsTest.php +258 -48
  88. package/project/.sdk/tm/py/pkg/feature/cache_feature.py +7 -1
  89. package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +90 -29
  90. package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +313 -80
  91. package/project/.sdk/tm/py/test/test_feature.py +18 -0
  92. package/project/.sdk/tm/rb/feature/cache_feature.rb +12 -0
  93. package/project/.sdk/tm/rb/feature/secrets_feature.rb +68 -21
  94. package/project/.sdk/tm/rb/test/feature/secrets/secrets_feature_test.rb +214 -43
  95. package/project/.sdk/tm/rb/test/feature_test.rb +20 -0
  96. package/project/.sdk/tm/rust/feature/cache.rs +15 -1
  97. package/project/.sdk/tm/rust/feature/secrets.rs +103 -23
  98. package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +357 -67
  99. package/project/.sdk/tm/rust/tests/feature_test.rs +41 -0
  100. package/project/.sdk/tm/scala/core/Control.scala +0 -5
  101. package/project/.sdk/tm/scala/feature/CacheFeature.scala +12 -0
  102. package/project/.sdk/tm/scala/feature/SecretsFeature.scala +88 -22
  103. package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +61 -1
  104. package/project/.sdk/tm/scala/sdktest/feature/secrets/SecretsTestMain.scala +230 -73
  105. package/project/.sdk/tm/scala/utility/Fetcher.scala +13 -1
  106. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Control.swift +0 -5
  107. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +11 -0
  108. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +76 -16
  109. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +21 -0
  110. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/feature/secrets/SecretsFeatureTest.swift +257 -73
  111. package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +13 -0
  112. package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +77 -17
  113. package/project/.sdk/tm/ts/test/feature/secrets/Secrets.test.ts +282 -87
  114. package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
  115. package/project/.sdk/tm/zig/feature/cache.zig +15 -1
  116. package/project/.sdk/tm/zig/feature/secrets.zig +109 -32
  117. package/project/.sdk/tm/zig/test/feature/secrets/secrets_test.zig +232 -9
  118. package/project/.sdk/tm/zig/test/feature_test.zig +33 -0
  119. package/project/sdkgen-package.json +1 -1
@@ -16,19 +16,24 @@ const BASE: &str = "http://secrets.test/api";
16
16
  // The test harness has no skip of its own at run time, so the skip is a
17
17
  // printed reason.
18
18
  fn skipped_without_auth() -> bool {
19
- if matches!(getp(&getp(&RUSTCRATE::make_config(), "options"), "auth"), Value::Map(_)) {
19
+ if declares_auth() {
20
20
  return false;
21
21
  }
22
22
  println!("SKIP secrets: this SDK's config declares no auth, so it places no credential");
23
23
  true
24
24
  }
25
25
 
26
+ fn declares_auth() -> bool {
27
+ matches!(getp(&getp(&RUSTCRATE::make_config(), "options"), "auth"), Value::Map(_))
28
+ }
29
+
26
30
  // ---- the counted transport ---------------------------------------------
27
31
 
28
32
  #[derive(Clone)]
29
33
  struct Call {
30
34
  url: String,
31
35
  auth: Option<String>,
36
+ values: String,
32
37
  body: Option<String>,
33
38
  redirect: Option<String>,
34
39
  }
@@ -100,9 +105,22 @@ impl Wire {
100
105
  _ => String::new(),
101
106
  };
102
107
  let fetchdef = getp(arg, "1");
108
+ let values = match getp(&fetchdef, "headers") {
109
+ Value::Map(m) => m
110
+ .borrow()
111
+ .values()
112
+ .map(|v| match v {
113
+ Value::Str(s) => s.clone(),
114
+ _ => String::new(),
115
+ })
116
+ .collect::<Vec<String>>()
117
+ .join("\n"),
118
+ _ => String::new(),
119
+ };
103
120
  w.calls.borrow_mut().push(Call {
104
121
  url: url.clone(),
105
122
  auth: authof(&fetchdef),
123
+ values,
106
124
  body: get_str(&fetchdef, "body"),
107
125
  redirect: get_str(&fetchdef, "redirect"),
108
126
  });
@@ -200,14 +218,47 @@ fn authof(fetchdef: &Value) -> Option<String> {
200
218
  }
201
219
  }
202
220
 
221
+ // The form prepare_auth writes, from the DEFAULT client's auth: under Basic
222
+ // the token is the key half of the encoded pair, and a prefix is
223
+ // space-joined.
224
+ fn cred_form() -> &'static (String, bool) {
225
+ static FORM: std::sync::OnceLock<(String, bool)> = std::sync::OnceLock::new();
226
+ FORM.get_or_init(|| {
227
+ let auth = getp(&RUSTCRATE::test_sdk(Value::Noval, Value::Noval).options_map(), "auth");
228
+ let prefix = match getp(&auth, "prefix") {
229
+ Value::Str(s) => s,
230
+ _ => String::new(),
231
+ };
232
+ (prefix, matches!(getp(&auth, "basic"), Value::Bool(true)))
233
+ })
234
+ }
235
+
236
+ // The header value prepare_auth writes for a token.
237
+ fn credential(token: &str, secret: &str) -> String {
238
+ let (prefix, basic) = cred_form();
239
+ let value = if *basic {
240
+ RUSTCRATE::utility::clean::base64_encode(format!("{}:{}", token, secret).as_bytes())
241
+ } else {
242
+ token.to_string()
243
+ };
244
+ if prefix.is_empty() {
245
+ value
246
+ } else {
247
+ format!("{} {}", prefix, value)
248
+ }
249
+ }
250
+
203
251
  fn credential_is(header: Option<String>, token: &str) {
204
- let got = header.unwrap_or_default();
205
- assert!(
206
- got == token || got.ends_with(&format!(" {}", token)),
207
- "expected the {} header to carry {}, got: {:?}",
252
+ paired_is(header, token, "");
253
+ }
254
+
255
+ fn paired_is(header: Option<String>, token: &str, secret: &str) {
256
+ assert_eq!(
257
+ header,
258
+ Some(credential(token, secret)),
259
+ "expected the {} header to carry {}",
208
260
  cred_header(),
209
- token,
210
- got
261
+ token
211
262
  );
212
263
  }
213
264
 
@@ -218,17 +269,27 @@ fn call_auth(calls: &[Call], i: usize) -> Option<String> {
218
269
  // ---- clients ------------------------------------------------------------
219
270
 
220
271
  fn sdk(wire: &Wire, secrets: Vec<(&str, Value)>) -> Rc<ProjectNameSDK> {
272
+ sdk_with(wire, secrets, vec![])
273
+ }
274
+
275
+ // `extra` is client options beside the feature's own.
276
+ fn sdk_with(wire: &Wire, secrets: Vec<(&str, Value)>, extra: Vec<(&str, Value)>) -> Rc<ProjectNameSDK> {
221
277
  let fopts = jo(vec![("active", Value::Bool(true))]);
222
278
  for (k, v) in secrets {
223
279
  RUSTCRATE::core::helpers::setp(&fopts, k, v);
224
280
  }
225
281
 
226
- ProjectNameSDK::new(jo(vec![
282
+ let opts = jo(vec![
227
283
  ("base", Value::str(BASE)),
228
284
  ("allow", jo(vec![("op", Value::str("direct,graphql"))])),
229
285
  ("system", jo(vec![("fetch", wire.fetch())])),
230
286
  ("feature", jo(vec![("secrets", fopts)])),
231
- ])).unwrap()
287
+ ]);
288
+ for (k, v) in extra {
289
+ RUSTCRATE::core::helpers::setp(&opts, k, v);
290
+ }
291
+
292
+ ProjectNameSDK::new(opts).unwrap()
232
293
  }
233
294
 
234
295
  fn providers(list: Vec<Value>) -> (&'static str, Value) {
@@ -434,6 +495,25 @@ fn an_omitted_apikey_defers_to_the_chain() {
434
495
  );
435
496
  }
436
497
 
498
+ #[test]
499
+ fn a_resolved_key_is_placed_as_prepare_auth_places_it_the_secret_kept() {
500
+ if skipped_without_auth() {
501
+ return;
502
+ }
503
+ let wire = Wire::new();
504
+ let client = sdk_with(
505
+ &wire,
506
+ vec![providers(vec![memory("APIKEY", "CHAINKEY05")])],
507
+ vec![("secret", Value::str("PASS01"))],
508
+ );
509
+
510
+ client
511
+ .direct(jo(vec![("path", Value::str("/thing"))]))
512
+ .expect("direct");
513
+
514
+ paired_is(call_auth(&wire.api(), 0), "CHAINKEY05", "PASS01");
515
+ }
516
+
437
517
  #[test]
438
518
  fn an_explicitly_empty_apikey_also_defers_to_the_chain() {
439
519
  if skipped_without_auth() {
@@ -1288,6 +1368,31 @@ fn an_expiry_buys_another_token_and_retries_the_same_request() {
1288
1368
  );
1289
1369
  }
1290
1370
 
1371
+ #[test]
1372
+ fn a_token_bought_on_a_refusal_is_placed_as_prepare_auth_places_it() {
1373
+ if skipped_without_auth() {
1374
+ return;
1375
+ }
1376
+ let wire = Wire::new().apistatus(&[401, 200]);
1377
+ let client = sdk_with(
1378
+ &wire,
1379
+ vec![
1380
+ ("name", Value::str("refresh_token")),
1381
+ providers(vec![memory("REFRESH_TOKEN", REFRESH)]),
1382
+ exchange(vec![]),
1383
+ ],
1384
+ vec![("secret", Value::str("PASS01"))],
1385
+ );
1386
+
1387
+ client
1388
+ .direct(jo(vec![("path", Value::str("/thing"))]))
1389
+ .expect("direct");
1390
+
1391
+ assert_eq!(wire.api().len(), 2, "expected the request to be retried");
1392
+ paired_is(call_auth(&wire.api(), 0), "ACCESS01", "PASS01");
1393
+ paired_is(call_auth(&wire.api(), 1), "ACCESS02", "PASS01");
1394
+ }
1395
+
1291
1396
  #[test]
1292
1397
  fn the_retry_happens_once_not_in_a_loop() {
1293
1398
  if skipped_without_auth() {
@@ -1656,51 +1761,69 @@ fn listener(
1656
1761
  (port, handle)
1657
1762
  }
1658
1763
 
1659
- #[test]
1660
- fn a_redirect_answering_the_exchange_is_a_failure_and_nothing_follows_it() {
1661
- if skipped_without_auth() {
1662
- return;
1663
- }
1664
- use std::sync::atomic::{AtomicBool, Ordering};
1665
- use std::sync::{Arc, Mutex};
1666
-
1667
- let stop = Arc::new(AtomicBool::new(false));
1668
- let case: Arc<Mutex<(i64, String)>> = Arc::new(Mutex::new((307, String::new())));
1669
- let elsewhere = Seen::default();
1670
- let followed = Seen::default();
1671
-
1672
- let (otherport, otherthread) =
1673
- listener(Arc::new(|_path: &str| None), elsewhere.clone(), stop.clone());
1674
- let answering = case.clone();
1675
- let (originport, originthread) = listener(
1676
- Arc::new(move |path: &str| {
1677
- if "/api/auth/token" != path {
1678
- return None;
1679
- }
1680
- let (status, location) = answering.lock().unwrap().clone();
1681
- Some(format!("{} Moved\r\nLocation: {}", status, location))
1682
- }),
1683
- followed.clone(),
1684
- stop.clone(),
1685
- );
1686
-
1687
- let base = format!("http://127.0.0.1:{}/api", originport);
1688
- let away = format!("http://127.0.0.1:{}/api/auth/token", otherport);
1689
- let cases: Vec<(i64, String)> = vec![
1690
- (307, away.clone()),
1691
- (308, away.clone()),
1692
- (302, away.clone()),
1693
- (307, "/api/moved/token".to_string()),
1694
- ];
1695
-
1696
- let mut refused = 0;
1697
- let mut allowed: Vec<String> = Vec::new();
1764
+ // The origin answers the exchange as `case` says, counting it, and records
1765
+ // what else it gets; the other origin records everything.
1766
+ struct Origins {
1767
+ case: std::sync::Arc<std::sync::Mutex<(i64, String)>>,
1768
+ exchanged: std::sync::Arc<std::sync::atomic::AtomicUsize>,
1769
+ elsewhere: Seen,
1770
+ followed: Seen,
1771
+ origin: String,
1772
+ base: String,
1773
+ away: String,
1774
+ ports: Vec<u16>,
1775
+ stop: std::sync::Arc<std::sync::atomic::AtomicBool>,
1776
+ threads: Vec<std::thread::JoinHandle<()>>,
1777
+ }
1778
+
1779
+ impl Origins {
1780
+ fn start() -> Origins {
1781
+ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
1782
+ use std::sync::{Arc, Mutex};
1783
+
1784
+ let stop = Arc::new(AtomicBool::new(false));
1785
+ let case: Arc<Mutex<(i64, String)>> = Arc::new(Mutex::new((307, String::new())));
1786
+ let exchanged = Arc::new(AtomicUsize::new(0));
1787
+ let elsewhere = Seen::default();
1788
+ let followed = Seen::default();
1789
+
1790
+ let (otherport, otherthread) =
1791
+ listener(Arc::new(|_path: &str| None), elsewhere.clone(), stop.clone());
1792
+ let answering = case.clone();
1793
+ let counting = exchanged.clone();
1794
+ let (originport, originthread) = listener(
1795
+ Arc::new(move |path: &str| {
1796
+ if "/api/auth/token" != path {
1797
+ return None;
1798
+ }
1799
+ counting.fetch_add(1, Ordering::SeqCst);
1800
+ let (status, location) = answering.lock().unwrap().clone();
1801
+ Some(format!("{} Moved\r\nLocation: {}", status, location))
1802
+ }),
1803
+ followed.clone(),
1804
+ stop.clone(),
1805
+ );
1698
1806
 
1699
- for (status, location) in cases.iter() {
1700
- *case.lock().unwrap() = (*status, location.clone());
1807
+ let origin = format!("http://127.0.0.1:{}", originport);
1808
+ Origins {
1809
+ case,
1810
+ exchanged,
1811
+ elsewhere,
1812
+ followed,
1813
+ base: format!("{}/api", origin),
1814
+ origin,
1815
+ away: format!("http://127.0.0.1:{}/api/auth/token", otherport),
1816
+ ports: vec![otherport, originport],
1817
+ stop,
1818
+ threads: vec![otherthread, originthread],
1819
+ }
1820
+ }
1701
1821
 
1702
- let client = ProjectNameSDK::new(jo(vec![
1703
- ("base", Value::str(base.clone())),
1822
+ fn client(&self, extra: Vec<(&str, Value)>) -> Rc<ProjectNameSDK> {
1823
+ let mut xopts = vec![("refresh", Value::str(REFRESH))];
1824
+ xopts.extend(extra);
1825
+ ProjectNameSDK::new(jo(vec![
1826
+ ("base", Value::str(self.base.clone())),
1704
1827
  ("allow", jo(vec![("op", Value::str("direct,graphql"))])),
1705
1828
  (
1706
1829
  "feature",
@@ -1709,21 +1832,57 @@ fn a_redirect_answering_the_exchange_is_a_failure_and_nothing_follows_it() {
1709
1832
  jo(vec![
1710
1833
  ("active", Value::Bool(true)),
1711
1834
  ("name", Value::str("refresh_token")),
1712
- exchange(vec![("refresh", Value::str(REFRESH))]),
1835
+ exchange(xopts),
1713
1836
  ]),
1714
1837
  )]),
1715
1838
  ),
1716
1839
  ]))
1717
- .unwrap();
1840
+ .unwrap()
1841
+ }
1718
1842
 
1719
- let message = match client.direct(jo(vec![("path", Value::str("/thing"))])) {
1720
- Ok(res) => get_str(&res, "err").unwrap_or_else(|| format!("{:?}", res)),
1721
- Err(err) => err.msg,
1722
- };
1843
+ fn close(self) {
1844
+ self.stop.store(true, std::sync::atomic::Ordering::SeqCst);
1845
+ for port in &self.ports {
1846
+ let _ = std::net::TcpStream::connect(("127.0.0.1", *port));
1847
+ }
1848
+ for thread in self.threads {
1849
+ thread.join().unwrap();
1850
+ }
1851
+ }
1852
+ }
1853
+
1854
+ fn direct_message(res: Result<Value, RUSTCRATE::core::error::ProjectNameError>) -> String {
1855
+ match res {
1856
+ Ok(res) => get_str(&res, "err").unwrap_or_else(|| format!("{:?}", res)),
1857
+ Err(err) => err.msg,
1858
+ }
1859
+ }
1860
+
1861
+ #[test]
1862
+ fn a_redirect_answering_the_exchange_is_a_failure_and_nothing_follows_it() {
1863
+ if skipped_without_auth() {
1864
+ return;
1865
+ }
1866
+ let o = Origins::start();
1867
+ let cases: Vec<(i64, String)> = vec![
1868
+ (307, o.away.clone()),
1869
+ (308, o.away.clone()),
1870
+ (302, o.away.clone()),
1871
+ (307, "/api/moved/token".to_string()),
1872
+ ];
1873
+
1874
+ let mut refused = 0;
1875
+ let mut allowed: Vec<String> = Vec::new();
1876
+
1877
+ for (status, location) in cases.iter() {
1878
+ *o.case.lock().unwrap() = (*status, location.clone());
1879
+
1880
+ let message =
1881
+ direct_message(o.client(vec![]).direct(jo(vec![("path", Value::str("/thing"))])));
1723
1882
 
1724
1883
  if message.contains(&format!(
1725
1884
  "token exchange failed: {} from {}/auth/token",
1726
- status, base
1885
+ status, o.base
1727
1886
  )) {
1728
1887
  refused += 1;
1729
1888
  } else {
@@ -1731,14 +1890,9 @@ fn a_redirect_answering_the_exchange_is_a_failure_and_nothing_follows_it() {
1731
1890
  }
1732
1891
  }
1733
1892
 
1734
- stop.store(true, Ordering::SeqCst);
1735
- let _ = std::net::TcpStream::connect(("127.0.0.1", otherport));
1736
- let _ = std::net::TcpStream::connect(("127.0.0.1", originport));
1737
- otherthread.join().unwrap();
1738
- originthread.join().unwrap();
1739
-
1740
- let elsewhere = elsewhere.lock().unwrap().clone();
1741
- let followed = followed.lock().unwrap().clone();
1893
+ let elsewhere = o.elsewhere.lock().unwrap().clone();
1894
+ let followed = o.followed.lock().unwrap().clone();
1895
+ o.close();
1742
1896
  println!(
1743
1897
  "secrets: exchange refused {} redirect(s), {} followed",
1744
1898
  refused,
@@ -1751,6 +1905,84 @@ fn a_redirect_answering_the_exchange_is_a_failure_and_nothing_follows_it() {
1751
1905
  assert_eq!(refused, cases.len());
1752
1906
  }
1753
1907
 
1908
+ #[test]
1909
+ fn with_the_option_a_redirect_on_the_exchanges_own_origin_is_followed_and_no_other() {
1910
+ if skipped_without_auth() {
1911
+ return;
1912
+ }
1913
+ use std::sync::atomic::Ordering;
1914
+
1915
+ let o = Origins::start();
1916
+ // The target the exchange is asked again at, if followed; a Location of a
1917
+ // query alone keeps the path that answered.
1918
+ let cases: Vec<(i64, String, Option<&str>)> = vec![
1919
+ (307, "/api/moved/token".to_string(), Some("/api/moved/token")),
1920
+ (308, format!("{}/api/moved/token", o.origin), Some("/api/moved/token")),
1921
+ (307, "?step=2".to_string(), Some("/api/auth/token?step=2")),
1922
+ (307, o.away.clone(), None),
1923
+ (302, "/api/moved/token".to_string(), None),
1924
+ (307, "/api/auth/token".to_string(), None),
1925
+ ];
1926
+
1927
+ let mut wrong: Vec<String> = Vec::new();
1928
+ let mut followed = 0;
1929
+ let mut refused = 0;
1930
+ let mut looped = 0;
1931
+
1932
+ for (status, location, next) in cases.iter() {
1933
+ *o.case.lock().unwrap() = (*status, location.clone());
1934
+ o.followed.lock().unwrap().clear();
1935
+ o.exchanged.store(0, Ordering::SeqCst);
1936
+
1937
+ let res = o
1938
+ .client(vec![("redirect", Value::str("same-origin"))])
1939
+ .direct(jo(vec![("path", Value::str("/thing"))]));
1940
+ let ok = matches!(&res, Ok(r) if getp(r, "ok") == Value::Bool(true));
1941
+ let message = direct_message(res);
1942
+ let seen: Vec<String> =
1943
+ o.followed.lock().unwrap().iter().map(|s| s.trim().to_string()).collect();
1944
+ let report = format!("{} to {}: {} {:?}", status, location, message, seen);
1945
+
1946
+ if let Some(next) = next {
1947
+ let asked = format!("POST {} {{\"refresh_token\":\"{}\"}}", next, REFRESH);
1948
+ if ok && 2 == seen.len() && asked == seen[0] {
1949
+ followed += 1;
1950
+ } else {
1951
+ wrong.push(report);
1952
+ }
1953
+ } else if seen.is_empty()
1954
+ && message.contains(&format!(
1955
+ "token exchange failed: {} from {}/auth/token",
1956
+ status, o.base
1957
+ ))
1958
+ {
1959
+ refused += 1;
1960
+ } else {
1961
+ wrong.push(report);
1962
+ }
1963
+
1964
+ if "/api/auth/token" == location {
1965
+ looped = o.exchanged.load(Ordering::SeqCst);
1966
+ }
1967
+ }
1968
+
1969
+ let elsewhere = o.elsewhere.lock().unwrap().clone();
1970
+ o.close();
1971
+ println!(
1972
+ "secrets: exchange opt-in followed {} redirect(s) on its own origin, refused {}, {} elsewhere",
1973
+ followed,
1974
+ refused,
1975
+ elsewhere.len()
1976
+ );
1977
+
1978
+ assert!(elsewhere.is_empty(), "the exchange reached another origin: {:?}", elsewhere);
1979
+ assert!(wrong.is_empty(), "{:?}", wrong);
1980
+ assert_eq!(followed, 3);
1981
+ assert_eq!(refused, 3);
1982
+ // A redirect to itself stops at the hop limit.
1983
+ assert_eq!(looped, 6);
1984
+ }
1985
+
1754
1986
 
1755
1987
  // TEST MODE BUYS NOTHING: the test feature replaces the transport so no
1756
1988
  // request leaves the process, and an exchange would be the one call it
@@ -1822,3 +2054,61 @@ fn helpers_are_the_ones_the_seam_uses() {
1822
2054
  assert_eq!(to_int(&Value::Num(401.0)), 401);
1823
2055
  assert!(matches!(ja(vec![Value::Noval]), Value::List(_)));
1824
2056
  }
2057
+
2058
+ // Off by the config where it declares no auth, else by `auth: null`: either
2059
+ // way the feature places no credential, buys no token and retries nothing.
2060
+ #[test]
2061
+ fn auth_off_places_no_credential_and_buys_no_token() {
2062
+ let declared = declares_auth();
2063
+ let secrets = ["OFFKEY01", "OFFKEY02", "OFFREFRESH01", "ACCESS01"];
2064
+ let carries =
2065
+ |call: &Call| call.auth.is_some() || secrets.iter().any(|s| call.values.contains(s));
2066
+ let mut sent: Vec<Call> = Vec::new();
2067
+ let mut bought = 0;
2068
+
2069
+ let scenarios: Vec<(&str, Vec<(&str, Value)>, Vec<(&str, Value)>, i64)> = vec![
2070
+ ("a key the chain resolves", vec![providers(vec![memory("APIKEY", "OFFKEY01")])], vec![], 200),
2071
+ ("an explicit apikey", vec![], vec![("apikey", Value::str("OFFKEY02"))], 200),
2072
+ (
2073
+ "a refusal",
2074
+ vec![
2075
+ ("name", Value::str("refresh_token")),
2076
+ providers(vec![memory("REFRESH_TOKEN", "OFFREFRESH01")]),
2077
+ exchange(vec![]),
2078
+ ],
2079
+ vec![],
2080
+ 401,
2081
+ ),
2082
+ ];
2083
+
2084
+ for (what, fopts, mut extra, status) in scenarios {
2085
+ if declared {
2086
+ extra.push(("auth", Value::Null));
2087
+ }
2088
+ let wire = Wire::new().apistatus(&[status]);
2089
+ let client = sdk_with(&wire, fopts, extra);
2090
+
2091
+ let _ = client.direct(jo(vec![("path", Value::str("/thing"))]));
2092
+
2093
+ assert_eq!(wire.api().len(), 1, "{}: a request was retried with auth off: {}", what, wire.trace());
2094
+ assert_eq!(wire.token().len(), 0, "{}: a token was bought with auth off: {}", what, wire.trace());
2095
+ assert!(!carries(&wire.api()[0]), "{}: a credential was sent with auth off: {}", what, wire.trace());
2096
+ assert_eq!(
2097
+ secrets_call(&client, vec![Value::str("credential")]),
2098
+ Value::str(""),
2099
+ "{}: the feature resolved a credential with auth off",
2100
+ what
2101
+ );
2102
+
2103
+ sent.extend(wire.api());
2104
+ bought += wire.token().len();
2105
+ }
2106
+
2107
+ println!(
2108
+ "secrets: auth off by {}: {} request(s), {} credential(s), {} token(s)",
2109
+ if declared { "option" } else { "config" },
2110
+ sent.len(),
2111
+ sent.iter().filter(|c| carries(c)).count(),
2112
+ bought
2113
+ );
2114
+ }
@@ -646,6 +646,47 @@ fn feature_cache_does_not_cache_non_2xx() {
646
646
  assert_eq!(f.borrow().track.borrow().bypass, 2, "expected 2 bypasses");
647
647
  }
648
648
 
649
+ #[test]
650
+ fn feature_cache_passes_a_body_of_another_type_than_json_by_unread() {
651
+ if !fh_present(&["cache"]) {
652
+ return;
653
+ }
654
+ let sent: Rc<RefCell<Vec<Value>>> = Rc::new(RefCell::new(Vec::new()));
655
+ let keep = sent.clone();
656
+ let (server, calls) = fh_recorder(Some(Rc::new(move |_n, _fd| {
657
+ let res = fh_response(
658
+ 200,
659
+ Value::Noval,
660
+ jo(vec![("content-type", Value::str("audio/wav"))]),
661
+ );
662
+ keep.borrow_mut().push(res.clone());
663
+ Ok(res)
664
+ })));
665
+ let f = Rc::new(RefCell::new(CacheFeature::new()));
666
+ let h = fh_make(Some(server), vec![(fr(&f), Value::Noval)]);
667
+ let read = || {
668
+ h.op(FhOpSpec {
669
+ op: "load".to_string(),
670
+ path: "/w/1/audio".to_string(),
671
+ ..Default::default()
672
+ })
673
+ };
674
+ let handed = |r: &FhOpResult| {
675
+ r.ctx.response.borrow().as_ref().map(|res| res.borrow().headers.clone())
676
+ };
677
+ let a = read();
678
+ let b = read();
679
+ assert_eq!(calls.borrow().len(), 2, "the body was stored");
680
+ // The transport's own response shares its headers map; a rebuilt copy does not.
681
+ let same = |got: Option<Value>, i: usize| match (got, getp(&sent.borrow()[i], "headers")) {
682
+ (Some(Value::Map(x)), Value::Map(y)) => Rc::ptr_eq(&x, &y),
683
+ _ => false,
684
+ };
685
+ assert!(same(handed(&a), 0), "the cache handed on a copy it had read");
686
+ assert!(same(handed(&b), 1), "the cache handed on a copy it had read");
687
+ assert_eq!(f.borrow().track.borrow().bypass, 2, "expected 2 bypasses");
688
+ }
689
+
649
690
  #[test]
650
691
  fn feature_cache_refetches_after_ttl() {
651
692
  if !fh_present(&["cache"]) {
@@ -15,9 +15,4 @@ class Control {
15
15
  var actor: String = ""
16
16
 
17
17
  var paging: JMap[String, Object] = null
18
-
19
- // Outbound streaming marker: the entity `stream` method sets this to the
20
- // caller-supplied streamable payload so the request builder / transport can
21
- // stream it as the request body.
22
- var streamOut: Object = null
23
18
  }
@@ -3,6 +3,7 @@ package SCALAPACKAGE.feature
3
3
  import java.util.{ArrayList, LinkedHashMap, List => JList, Map => JMap}
4
4
  import java.util.function.Supplier
5
5
  import SCALAPACKAGE.core.{Context, FetcherFn, SdkClient}
6
+ import SCALAPACKAGE.utility.Media
6
7
 
7
8
  // Response caching for safe (read) requests. Wraps the active transport and
8
9
  // serves a fresh cached snapshot instead of hitting the network when the
@@ -97,6 +98,12 @@ class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
97
98
  throw err
98
99
  }
99
100
 
101
+ // A body that declares another type than JSON passes by, unread.
102
+ if (storable(res) && !json(res)) {
103
+ this.bypass += 1
104
+ return res
105
+ }
106
+
100
107
  if (storable(res)) {
101
108
  val snapshot = this.snapshot(res)
102
109
  val ttl = FeatureOptions.foptInt(this.options, "ttl", 5000)
@@ -121,6 +128,11 @@ class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
121
128
  java.lang.Boolean.TRUE != res.asInstanceOf[JMap[String, Object]].get("unreadable")
122
129
  }
123
130
 
131
+ private def json(res: Object): Boolean = {
132
+ val ctype = FeatureOptions.fresHeader(res, "content-type")
133
+ "" == ctype || Media.isJsonMedia(ctype)
134
+ }
135
+
124
136
  private def snapshot(res: Object): CacheSnapshot = {
125
137
  val rm: JMap[String, Object] = res match {
126
138
  case m: JMap[_, _] => m.asInstanceOf[JMap[String, Object]]