sdkloom 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +239 -2
- package/kits/allowed-sources.json +24 -0
- package/kits/csharp/kit.json +6 -0
- package/kits/csharp/src/Runtime/Client.cs +295 -0
- package/kits/csharp/src/Runtime/Context.cs +216 -0
- package/kits/csharp/src/Runtime/Errors.cs +121 -0
- package/kits/csharp/src/Runtime/Http.cs +218 -0
- package/kits/csharp/src/Runtime/Json.cs +650 -0
- package/kits/csharp/src/Runtime/Lang.cs +212 -0
- package/kits/csharp/src/Runtime/Models.cs +206 -0
- package/kits/csharp/src/Runtime/Multipart.cs +108 -0
- package/kits/csharp/src/Runtime/Pagination.cs +122 -0
- package/kits/csharp/src/Runtime/Sse.cs +197 -0
- package/kits/csharp/src/Runtime/Testing.cs +375 -0
- package/kits/csharp/src/Runtime/Transport.cs +212 -0
- package/kits/csharp/src/Runtime/Wait.cs +57 -0
- package/kits/csharp/src/Runtime/Webhooks.cs +148 -0
- package/kits/csharp/tests/ClientTests.cs +128 -0
- package/kits/csharp/tests/HttpTests.cs +44 -0
- package/kits/csharp/tests/JsonTests.cs +132 -0
- package/kits/csharp/tests/LangTests.cs +57 -0
- package/kits/csharp/tests/MultipartTests.cs +31 -0
- package/kits/csharp/tests/PaginationTests.cs +93 -0
- package/kits/csharp/tests/SseTests.cs +122 -0
- package/kits/csharp/tests/TestRunner.cs +93 -0
- package/kits/csharp/tests/TestingTests.cs +80 -0
- package/kits/csharp/tests/TransportTests.cs +145 -0
- package/kits/csharp/tests/WaitTests.cs +39 -0
- package/kits/csharp/tests/WebhooksTests.cs +46 -0
- package/kits/csharp/versions.json +12 -0
- package/kits/go/kit.json +6 -0
- package/kits/go/src/sdkruntime/client.go +322 -0
- package/kits/go/src/sdkruntime/context.go +207 -0
- package/kits/go/src/sdkruntime/errors.go +74 -0
- package/kits/go/src/sdkruntime/http.go +261 -0
- package/kits/go/src/sdkruntime/json.go +527 -0
- package/kits/go/src/sdkruntime/lang.go +462 -0
- package/kits/go/src/sdkruntime/multipart.go +75 -0
- package/kits/go/src/sdkruntime/pagination.go +150 -0
- package/kits/go/src/sdkruntime/sse.go +267 -0
- package/kits/go/src/sdkruntime/testing.go +343 -0
- package/kits/go/src/sdkruntime/transport.go +168 -0
- package/kits/go/src/sdkruntime/wait.go +49 -0
- package/kits/go/src/sdkruntime/webhooks.go +129 -0
- package/kits/go/tests/client_test.go +185 -0
- package/kits/go/tests/http_test.go +78 -0
- package/kits/go/tests/json_test.go +127 -0
- package/kits/go/tests/lang_test.go +98 -0
- package/kits/go/tests/multipart_test.go +29 -0
- package/kits/go/tests/pagination_test.go +69 -0
- package/kits/go/tests/sse_test.go +129 -0
- package/kits/go/tests/testing_test.go +121 -0
- package/kits/go/tests/transport_test.go +133 -0
- package/kits/go/tests/wait_test.go +46 -0
- package/kits/go/tests/webhooks_test.go +62 -0
- package/kits/go/versions.json +12 -0
- package/kits/java/kit.json +6 -0
- package/kits/java/src/dev/sdkloom/runtime/ApiException.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/ArgumentException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Assert.java +72 -0
- package/kits/java/src/dev/sdkloom/runtime/ClientRuntime.java +220 -0
- package/kits/java/src/dev/sdkloom/runtime/Clock.java +39 -0
- package/kits/java/src/dev/sdkloom/runtime/Context.java +79 -0
- package/kits/java/src/dev/sdkloom/runtime/Decode.java +200 -0
- package/kits/java/src/dev/sdkloom/runtime/DecodeException.java +26 -0
- package/kits/java/src/dev/sdkloom/runtime/Decoder.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/Emitter.java +56 -0
- package/kits/java/src/dev/sdkloom/runtime/ErrorFactory.java +18 -0
- package/kits/java/src/dev/sdkloom/runtime/EventListener.java +20 -0
- package/kits/java/src/dev/sdkloom/runtime/EventStream.java +232 -0
- package/kits/java/src/dev/sdkloom/runtime/FakeClock.java +37 -0
- package/kits/java/src/dev/sdkloom/runtime/Headers.java +42 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpRequest.java +88 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpResponse.java +69 -0
- package/kits/java/src/dev/sdkloom/runtime/JdkTransport.java +97 -0
- package/kits/java/src/dev/sdkloom/runtime/Js.java +230 -0
- package/kits/java/src/dev/sdkloom/runtime/Json.java +281 -0
- package/kits/java/src/dev/sdkloom/runtime/JsonValue.java +15 -0
- package/kits/java/src/dev/sdkloom/runtime/Links.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/Middleware.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/MiddlewareEntry.java +43 -0
- package/kits/java/src/dev/sdkloom/runtime/MockRoute.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/MockTransport.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/MultipartForm.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/Next.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationInfo.java +35 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationMock.java +105 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationSpec.java +125 -0
- package/kits/java/src/dev/sdkloom/runtime/Pages.java +55 -0
- package/kits/java/src/dev/sdkloom/runtime/Pipeline.java +29 -0
- package/kits/java/src/dev/sdkloom/runtime/RawResult.java +31 -0
- package/kits/java/src/dev/sdkloom/runtime/RecordedEvent.java +36 -0
- package/kits/java/src/dev/sdkloom/runtime/SdkInfo.java +38 -0
- package/kits/java/src/dev/sdkloom/runtime/SendOptions.java +34 -0
- package/kits/java/src/dev/sdkloom/runtime/ServerSentEvent.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/State.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/Test.java +21 -0
- package/kits/java/src/dev/sdkloom/runtime/TestRunner.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/Transport.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/TransportException.java +30 -0
- package/kits/java/src/dev/sdkloom/runtime/UrlConnectionTransport.java +195 -0
- package/kits/java/src/dev/sdkloom/runtime/Urls.java +116 -0
- package/kits/java/src/dev/sdkloom/runtime/Waits.java +53 -0
- package/kits/java/src/dev/sdkloom/runtime/WebhookSignatureException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Webhooks.java +146 -0
- package/kits/java/tests/dev/sdkloom/runtime/ClientTest.java +266 -0
- package/kits/java/tests/dev/sdkloom/runtime/EventStreamTest.java +131 -0
- package/kits/java/tests/dev/sdkloom/runtime/HttpTest.java +95 -0
- package/kits/java/tests/dev/sdkloom/runtime/JdkTransportTest.java +12 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsTest.java +102 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsonTest.java +87 -0
- package/kits/java/tests/dev/sdkloom/runtime/LinksTest.java +22 -0
- package/kits/java/tests/dev/sdkloom/runtime/MultipartFormTest.java +30 -0
- package/kits/java/tests/dev/sdkloom/runtime/PagesTest.java +75 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestData.java +53 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestingTest.java +156 -0
- package/kits/java/tests/dev/sdkloom/runtime/TransportContract.java +201 -0
- package/kits/java/tests/dev/sdkloom/runtime/UrlConnectionTransportTest.java +49 -0
- package/kits/java/tests/dev/sdkloom/runtime/WaitsTest.java +43 -0
- package/kits/java/tests/dev/sdkloom/runtime/WebhooksTest.java +45 -0
- package/kits/java/versions.json +41 -0
- package/kits/python/kit.json +6 -0
- package/kits/python/src/sdkloom_runtime/__init__.py +1 -0
- package/kits/python/src/sdkloom_runtime/client.py +232 -0
- package/kits/python/src/sdkloom_runtime/compat.py +195 -0
- package/kits/python/src/sdkloom_runtime/context.py +134 -0
- package/kits/python/src/sdkloom_runtime/errors.py +65 -0
- package/kits/python/src/sdkloom_runtime/http.py +192 -0
- package/kits/python/src/sdkloom_runtime/lang.py +207 -0
- package/kits/python/src/sdkloom_runtime/models.py +221 -0
- package/kits/python/src/sdkloom_runtime/multipart.py +69 -0
- package/kits/python/src/sdkloom_runtime/pagination.py +61 -0
- package/kits/python/src/sdkloom_runtime/pipeline.py +39 -0
- package/kits/python/src/sdkloom_runtime/sse.py +121 -0
- package/kits/python/src/sdkloom_runtime/testing.py +224 -0
- package/kits/python/src/sdkloom_runtime/transport.py +120 -0
- package/kits/python/src/sdkloom_runtime/wait.py +44 -0
- package/kits/python/src/sdkloom_runtime/webhooks.py +84 -0
- package/kits/python/tests/support.py +28 -0
- package/kits/python/tests/test_client.py +275 -0
- package/kits/python/tests/test_compat.py +87 -0
- package/kits/python/tests/test_http.py +94 -0
- package/kits/python/tests/test_lang.py +104 -0
- package/kits/python/tests/test_models.py +110 -0
- package/kits/python/tests/test_multipart.py +37 -0
- package/kits/python/tests/test_pagination.py +70 -0
- package/kits/python/tests/test_sse.py +61 -0
- package/kits/python/tests/test_testing.py +130 -0
- package/kits/python/tests/test_transport.py +124 -0
- package/kits/python/tests/test_wait.py +46 -0
- package/kits/python/tests/test_webhooks.py +57 -0
- package/kits/python/versions.json +33 -0
- package/kits/rust/kit.json +6 -0
- package/kits/rust/src/runtime/client.rs +212 -0
- package/kits/rust/src/runtime/context.rs +182 -0
- package/kits/rust/src/runtime/crypto.rs +84 -0
- package/kits/rust/src/runtime/errors.rs +147 -0
- package/kits/rust/src/runtime/http.rs +248 -0
- package/kits/rust/src/runtime/json.rs +424 -0
- package/kits/rust/src/runtime/lang.rs +296 -0
- package/kits/rust/src/runtime/mod.rs +30 -0
- package/kits/rust/src/runtime/models.rs +266 -0
- package/kits/rust/src/runtime/multipart.rs +81 -0
- package/kits/rust/src/runtime/pagination.rs +117 -0
- package/kits/rust/src/runtime/sse.rs +195 -0
- package/kits/rust/src/runtime/testing.rs +308 -0
- package/kits/rust/src/runtime/transport.rs +324 -0
- package/kits/rust/src/runtime/wait.rs +29 -0
- package/kits/rust/src/runtime/webhooks.rs +142 -0
- package/kits/rust/tests/client.rs +152 -0
- package/kits/rust/tests/http.rs +44 -0
- package/kits/rust/tests/json.rs +82 -0
- package/kits/rust/tests/lang.rs +74 -0
- package/kits/rust/tests/multipart.rs +21 -0
- package/kits/rust/tests/pagination.rs +61 -0
- package/kits/rust/tests/sse.rs +77 -0
- package/kits/rust/tests/testing.rs +90 -0
- package/kits/rust/tests/transport.rs +218 -0
- package/kits/rust/tests/wait.rs +32 -0
- package/kits/rust/tests/webhooks.rs +73 -0
- package/kits/rust/versions.json +6 -0
- package/kits/typescript/kit.json +6 -0
- package/kits/typescript/src/client.ts +223 -0
- package/kits/typescript/src/context.ts +108 -0
- package/kits/typescript/src/errors.ts +63 -0
- package/kits/typescript/src/helpers.ts +49 -0
- package/kits/typescript/src/http.ts +192 -0
- package/kits/typescript/src/models.ts +139 -0
- package/kits/typescript/src/multipart.ts +66 -0
- package/kits/typescript/src/pagination.ts +54 -0
- package/kits/typescript/src/pipeline.ts +25 -0
- package/kits/typescript/src/sse.ts +102 -0
- package/kits/typescript/src/testing.ts +217 -0
- package/kits/typescript/src/transport.ts +101 -0
- package/kits/typescript/src/wait.ts +36 -0
- package/kits/typescript/src/webhooks.ts +168 -0
- package/kits/typescript/tests/client.test.ts +269 -0
- package/kits/typescript/tests/helpers.test.ts +27 -0
- package/kits/typescript/tests/http.test.ts +80 -0
- package/kits/typescript/tests/models.test.ts +71 -0
- package/kits/typescript/tests/multipart.test.ts +28 -0
- package/kits/typescript/tests/pagination.test.ts +76 -0
- package/kits/typescript/tests/sse.test.ts +82 -0
- package/kits/typescript/tests/testing.test.ts +103 -0
- package/kits/typescript/tests/transport.test.ts +127 -0
- package/kits/typescript/tests/wait.test.ts +31 -0
- package/kits/typescript/tests/webhooks.test.ts +53 -0
- package/kits/typescript/versions.json +15 -0
- package/package.json +37 -4
- package/packages/cli/recipes/apiKeyHeader.ts +16 -0
- package/packages/cli/recipes/apiVersionHeader.ts +16 -0
- package/packages/cli/recipes/basic.ts +13 -0
- package/packages/cli/recipes/bearer.ts +10 -0
- package/packages/cli/recipes/hmacSign.ts +23 -0
- package/packages/cli/recipes/idempotencyKey.ts +19 -0
- package/packages/cli/recipes/oauthClientCredentials.ts +50 -0
- package/packages/cli/recipes/retry.ts +42 -0
- package/packages/cli/recipes/timing.ts +12 -0
- package/packages/cli/recipes/userAgent.ts +8 -0
- package/packages/cli/src/build.js +93 -0
- package/packages/cli/src/commands/build.js +31 -0
- package/packages/cli/src/commands/conformance-check.js +50 -0
- package/packages/cli/src/commands/contract.js +181 -0
- package/packages/cli/src/commands/diff.js +113 -0
- package/packages/cli/src/commands/doctor.js +43 -0
- package/packages/cli/src/commands/import-openapi.js +42 -0
- package/packages/cli/src/commands/init.js +27 -0
- package/packages/cli/src/commands/kit-check.js +145 -0
- package/packages/cli/src/commands/publish.js +81 -0
- package/packages/cli/src/commands/recipe.js +49 -0
- package/packages/cli/src/commands/record.js +181 -0
- package/packages/cli/src/commands/serve.js +156 -0
- package/packages/cli/src/commands/spec-check.js +57 -0
- package/packages/cli/src/commands/test.js +177 -0
- package/packages/cli/src/commands/variants.js +32 -0
- package/packages/cli/src/commands/verify.js +57 -0
- package/packages/cli/src/commands/version.js +63 -0
- package/packages/cli/src/containers.js +82 -0
- package/packages/cli/src/coverage.js +72 -0
- package/packages/cli/src/git.js +25 -0
- package/packages/cli/src/io.js +50 -0
- package/packages/cli/src/java/ScenarioHarness.java +297 -0
- package/packages/cli/src/java/SchemaRoundTrip.java +63 -0
- package/packages/cli/src/java.js +70 -0
- package/packages/cli/src/js/harness.mjs +120 -0
- package/packages/cli/src/kits.js +103 -0
- package/packages/cli/src/languages/csharp.js +274 -0
- package/packages/cli/src/languages/go.js +185 -0
- package/packages/cli/src/languages/index.js +18 -0
- package/packages/cli/src/languages/java.js +187 -0
- package/packages/cli/src/languages/python.js +120 -0
- package/packages/cli/src/languages/rust.js +189 -0
- package/packages/cli/src/languages/types.js +93 -0
- package/packages/cli/src/languages/typescript.js +112 -0
- package/packages/cli/src/main.js +13 -0
- package/packages/cli/src/packaging.js +39 -0
- package/packages/cli/src/portability.js +45 -0
- package/packages/cli/src/profiles.js +55 -0
- package/packages/cli/src/project.js +56 -0
- package/packages/cli/src/python/scenario_harness.py +162 -0
- package/packages/cli/src/python/schema_roundtrip.py +42 -0
- package/packages/cli/src/registries.js +214 -0
- package/packages/cli/src/resolve-core.js +39 -0
- package/packages/cli/src/run.js +133 -0
- package/packages/cli/src/runtimes.js +61 -0
- package/packages/cli/src/scenarios-java.js +82 -0
- package/packages/cli/src/scenarios-javascript.js +54 -0
- package/packages/cli/src/scenarios-python.js +90 -0
- package/packages/cli/src/scenarios.js +183 -0
- package/packages/cli/src/schema.js +84 -0
- package/packages/cli/src/templates.js +85 -0
- package/packages/cli/src/toolchains.js +68 -0
- package/packages/cli/src/transpiled-tests.js +92 -0
- package/packages/codegen/src/canonical-json.js +31 -0
- package/packages/codegen/src/capabilities.js +50 -0
- package/packages/codegen/src/code-writer.js +54 -0
- package/packages/codegen/src/file-tree.js +25 -0
- package/packages/codegen/src/hash.js +6 -0
- package/packages/codegen/src/index.js +16 -0
- package/packages/codegen/src/lock.js +106 -0
- package/packages/conformance/src/case.js +143 -0
- package/packages/conformance/src/checks.js +40 -0
- package/packages/conformance/src/index.js +3 -0
- package/packages/conformance/src/matrix.js +48 -0
- package/packages/conformance/src/suite.js +48 -0
- package/packages/differ/src/diff.js +500 -0
- package/packages/differ/src/index.js +3 -0
- package/packages/differ/src/notes.js +112 -0
- package/packages/differ/src/support.js +34 -0
- package/packages/gen-csharp/src/emit-client.js +397 -0
- package/packages/gen-csharp/src/emit-harness.js +333 -0
- package/packages/gen-csharp/src/emit-mock.js +111 -0
- package/packages/gen-csharp/src/emit-models.js +119 -0
- package/packages/gen-csharp/src/emit-webhooks.js +57 -0
- package/packages/gen-csharp/src/generate.js +245 -0
- package/packages/gen-csharp/src/index.js +5 -0
- package/packages/gen-csharp/src/names.js +284 -0
- package/packages/gen-go/src/emit-client.js +405 -0
- package/packages/gen-go/src/emit-harness.js +294 -0
- package/packages/gen-go/src/emit-mock.js +112 -0
- package/packages/gen-go/src/emit-models.js +146 -0
- package/packages/gen-go/src/emit-webhooks.js +58 -0
- package/packages/gen-go/src/generate.js +205 -0
- package/packages/gen-go/src/index.js +5 -0
- package/packages/gen-go/src/names.js +197 -0
- package/packages/gen-java/src/emit-client.js +441 -0
- package/packages/gen-java/src/emit-models.js +342 -0
- package/packages/gen-java/src/emit-testing.js +123 -0
- package/packages/gen-java/src/emit-webhooks.js +70 -0
- package/packages/gen-java/src/generate.js +244 -0
- package/packages/gen-java/src/index.js +4 -0
- package/packages/gen-java/src/names.js +197 -0
- package/packages/gen-python/src/emit-client.js +272 -0
- package/packages/gen-python/src/emit-models.js +139 -0
- package/packages/gen-python/src/emit-testing.js +93 -0
- package/packages/gen-python/src/emit-webhooks.js +48 -0
- package/packages/gen-python/src/generate.js +237 -0
- package/packages/gen-python/src/index.js +2 -0
- package/packages/gen-python/src/names.js +161 -0
- package/packages/gen-python/src/style.js +67 -0
- package/packages/gen-rust/src/emit-client.js +379 -0
- package/packages/gen-rust/src/emit-harness.js +278 -0
- package/packages/gen-rust/src/emit-mock.js +121 -0
- package/packages/gen-rust/src/emit-models.js +117 -0
- package/packages/gen-rust/src/emit-webhooks.js +58 -0
- package/packages/gen-rust/src/generate.js +243 -0
- package/packages/gen-rust/src/index.js +5 -0
- package/packages/gen-rust/src/names.js +223 -0
- package/packages/gen-typescript/src/emit-client.js +434 -0
- package/packages/gen-typescript/src/emit-models.js +93 -0
- package/packages/gen-typescript/src/emit-package.js +90 -0
- package/packages/gen-typescript/src/emit-testing.js +138 -0
- package/packages/gen-typescript/src/emit-webhooks.js +52 -0
- package/packages/gen-typescript/src/generate.js +70 -0
- package/packages/gen-typescript/src/index.js +3 -0
- package/packages/gen-typescript/src/naming.js +64 -0
- package/packages/gen-typescript/src/runtime.js +81 -0
- package/packages/gen-typescript/src/types.js +136 -0
- package/packages/ir/src/api-surface.js +22 -0
- package/packages/ir/src/checks.js +52 -0
- package/packages/ir/src/index.js +32 -0
- package/packages/ir/src/ir.js +214 -0
- package/packages/ir/src/pages.js +18 -0
- package/packages/ir/src/validate-ir.js +462 -0
- package/packages/ir/src/validate-types.js +140 -0
- package/packages/kit/src/block-parser.js +196 -0
- package/packages/kit/src/index.js +15 -0
- package/packages/kit/src/kit-check.js +203 -0
- package/packages/kit/src/sources.js +53 -0
- package/packages/kit/src/version-policy.js +133 -0
- package/packages/kit/src/version-range.js +132 -0
- package/packages/openapi/src/import.js +292 -0
- package/packages/openapi/src/index.js +1 -0
- package/packages/openapi/src/names.js +50 -0
- package/packages/openapi/src/schemas.js +264 -0
- package/packages/packager/src/archive.js +106 -0
- package/packages/packager/src/crate.js +39 -0
- package/packages/packager/src/gomodule.js +11 -0
- package/packages/packager/src/index.js +9 -0
- package/packages/packager/src/maven.js +85 -0
- package/packages/packager/src/metadata.js +34 -0
- package/packages/packager/src/npm.js +71 -0
- package/packages/packager/src/nuget.js +78 -0
- package/packages/packager/src/sbom.js +56 -0
- package/packages/packager/src/wheel.js +62 -0
- package/packages/spec/src/index.js +11 -0
- package/packages/spec/src/requirements.js +139 -0
- package/packages/transpile/src/ast.js +51 -0
- package/packages/transpile/src/check.js +669 -0
- package/packages/transpile/src/emit-csharp.js +469 -0
- package/packages/transpile/src/emit-go.js +529 -0
- package/packages/transpile/src/emit-java.js +360 -0
- package/packages/transpile/src/emit-python.js +320 -0
- package/packages/transpile/src/emit-rust.js +499 -0
- package/packages/transpile/src/errors.js +15 -0
- package/packages/transpile/src/index.js +13 -0
- package/packages/transpile/src/lexer.js +161 -0
- package/packages/transpile/src/parser.js +543 -0
- package/packages/transpile/src/tests/ast.js +46 -0
- package/packages/transpile/src/tests/parse.js +376 -0
- package/packages/transpile/src/tests/translate.js +849 -0
- package/packages/transpile/src/transpile.js +57 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// sdkloom Go kit: reading a stream of server-sent events.
|
|
2
|
+
package sdkruntime
|
|
3
|
+
|
|
4
|
+
import (
|
|
5
|
+
"bytes"
|
|
6
|
+
"encoding/json"
|
|
7
|
+
"errors"
|
|
8
|
+
"io"
|
|
9
|
+
"strings"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
// @block sse.parse
|
|
13
|
+
// @summary Reads a text/event-stream response as it arrives, like bufio.Scanner: splits lines on CRLF, LF or CR (also across reads), collects event, data and id fields, dispatches on a blank line, drops an unfinished event at the end, and decodes each event's data (JSON for a typed stream, text for a string stream). Close stops early.
|
|
14
|
+
// @versions >=1.18
|
|
15
|
+
// @source [1] https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
16
|
+
// @quote [1] "Event streams in this format must always be encoded as UTF-8."
|
|
17
|
+
// @quote [1] "Once the end of the file is reached, any pending data must be discarded."
|
|
18
|
+
// @quote [1] "If value starts with a U+0020 SPACE"
|
|
19
|
+
// @source [2] https://pkg.go.dev/io#Reader
|
|
20
|
+
// @quote [2] "Read reads up to len(p) bytes into p."
|
|
21
|
+
// @retrieved 2026-12-06
|
|
22
|
+
// @tests tests/sse_test.go
|
|
23
|
+
// @requirement SDKL-SSE-001
|
|
24
|
+
|
|
25
|
+
// ServerSentEvent is one server-sent event: its type ("message" unless the server named one), the last event ID and its data.
|
|
26
|
+
type ServerSentEvent[T any] struct {
|
|
27
|
+
Event string
|
|
28
|
+
ID string
|
|
29
|
+
Data T
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// EventStream reads server-sent events:
|
|
33
|
+
//
|
|
34
|
+
// events := client.WatchEvents(ctx, params)
|
|
35
|
+
// defer events.Close()
|
|
36
|
+
// for events.Next() {
|
|
37
|
+
// use(events.Event())
|
|
38
|
+
// }
|
|
39
|
+
// if err := events.Err(); err != nil { … }
|
|
40
|
+
type EventStream[T any] struct {
|
|
41
|
+
open func() (*EventStream[T], error)
|
|
42
|
+
opened *EventStream[T]
|
|
43
|
+
body io.ReadCloser
|
|
44
|
+
decode Decoder[T]
|
|
45
|
+
chunk []byte
|
|
46
|
+
buffer []byte
|
|
47
|
+
line []byte
|
|
48
|
+
crLast bool
|
|
49
|
+
first bool
|
|
50
|
+
kind string
|
|
51
|
+
data *strings.Builder
|
|
52
|
+
lastID string
|
|
53
|
+
current ServerSentEvent[T]
|
|
54
|
+
err error
|
|
55
|
+
done bool
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// NewEventStream reads the events of a response; decode reads each event's JSON data, or nil keeps it as text (T string).
|
|
59
|
+
func NewEventStream[T any](response Response, decode Decoder[T]) *EventStream[T] {
|
|
60
|
+
return &EventStream[T]{body: response.BodyReader(), decode: decode, chunk: make([]byte, 8192), first: true}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// LazyEvents is an event stream whose request is sent by the first Next.
|
|
64
|
+
func LazyEvents[T any](open func() (*EventStream[T], error)) *EventStream[T] {
|
|
65
|
+
return &EventStream[T]{open: open}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// EncodeEvents writes events in the event-stream format (mocks and tests); encode writes data as JSON, nil keeps text.
|
|
69
|
+
func EncodeEvents[T any](events []ServerSentEvent[T], encode func(T) any) ([]byte, error) {
|
|
70
|
+
var out strings.Builder
|
|
71
|
+
for _, event := range events {
|
|
72
|
+
var text string
|
|
73
|
+
if encode == nil {
|
|
74
|
+
text = toText(any(event.Data))
|
|
75
|
+
} else {
|
|
76
|
+
encoded, err := EncodeJSON(encode(event.Data))
|
|
77
|
+
if err != nil {
|
|
78
|
+
return nil, err
|
|
79
|
+
}
|
|
80
|
+
text = string(encoded)
|
|
81
|
+
}
|
|
82
|
+
if event.Event != "message" && event.Event != "" {
|
|
83
|
+
out.WriteString("event: " + event.Event + "\n")
|
|
84
|
+
}
|
|
85
|
+
if event.ID != "" {
|
|
86
|
+
out.WriteString("id: " + event.ID + "\n")
|
|
87
|
+
}
|
|
88
|
+
for _, part := range strings.Split(strings.NewReplacer("\r\n", "\n", "\r", "\n").Replace(text), "\n") {
|
|
89
|
+
out.WriteString("data: " + part + "\n")
|
|
90
|
+
}
|
|
91
|
+
out.WriteString("\n")
|
|
92
|
+
}
|
|
93
|
+
return []byte(out.String()), nil
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
func toText(value any) string {
|
|
97
|
+
if text, ok := value.(string); ok {
|
|
98
|
+
return text
|
|
99
|
+
}
|
|
100
|
+
encoded, _ := json.Marshal(value)
|
|
101
|
+
return string(encoded)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
func (s *EventStream[T]) source() *EventStream[T] {
|
|
105
|
+
if s.open != nil {
|
|
106
|
+
open := s.open
|
|
107
|
+
s.open = nil
|
|
108
|
+
opened, err := open()
|
|
109
|
+
if err != nil {
|
|
110
|
+
s.err = err
|
|
111
|
+
s.done = true
|
|
112
|
+
return s
|
|
113
|
+
}
|
|
114
|
+
s.opened = opened
|
|
115
|
+
}
|
|
116
|
+
if s.opened != nil {
|
|
117
|
+
return s.opened
|
|
118
|
+
}
|
|
119
|
+
return s
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Next reads up to the next event and reports whether there is one.
|
|
123
|
+
func (s *EventStream[T]) Next() bool {
|
|
124
|
+
source := s.source()
|
|
125
|
+
if source != s {
|
|
126
|
+
return source.Next()
|
|
127
|
+
}
|
|
128
|
+
for !s.done {
|
|
129
|
+
if event, ok := s.readLine(); ok {
|
|
130
|
+
s.current = event
|
|
131
|
+
return true
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return false
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Event returns the event Next found.
|
|
138
|
+
func (s *EventStream[T]) Event() ServerSentEvent[T] {
|
|
139
|
+
if s.opened != nil {
|
|
140
|
+
return s.opened.Event()
|
|
141
|
+
}
|
|
142
|
+
return s.current
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Err reports why the stream stopped early; nil when it simply ended.
|
|
146
|
+
func (s *EventStream[T]) Err() error {
|
|
147
|
+
if s.opened != nil {
|
|
148
|
+
return s.opened.Err()
|
|
149
|
+
}
|
|
150
|
+
return s.err
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Close stops reading and ends the request.
|
|
154
|
+
func (s *EventStream[T]) Close() error {
|
|
155
|
+
s.open = nil
|
|
156
|
+
s.done = true
|
|
157
|
+
if s.opened != nil {
|
|
158
|
+
return s.opened.Close()
|
|
159
|
+
}
|
|
160
|
+
if s.body != nil {
|
|
161
|
+
return s.body.Close()
|
|
162
|
+
}
|
|
163
|
+
return nil
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// readLine handles one line; it returns an event when the line was the blank line ending one.
|
|
167
|
+
func (s *EventStream[T]) readLine() (ServerSentEvent[T], bool) {
|
|
168
|
+
for {
|
|
169
|
+
if len(s.buffer) == 0 {
|
|
170
|
+
count, err := s.body.Read(s.chunk)
|
|
171
|
+
s.buffer = s.chunk[:count]
|
|
172
|
+
if count == 0 && err != nil {
|
|
173
|
+
if !errors.Is(err, io.EOF) {
|
|
174
|
+
s.err = err
|
|
175
|
+
}
|
|
176
|
+
s.done = true
|
|
177
|
+
_ = s.body.Close()
|
|
178
|
+
return ServerSentEvent[T]{}, false
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
value := s.buffer[0]
|
|
182
|
+
s.buffer = s.buffer[1:]
|
|
183
|
+
if s.crLast {
|
|
184
|
+
s.crLast = false
|
|
185
|
+
if value == '\n' {
|
|
186
|
+
continue
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if value == '\r' || value == '\n' {
|
|
190
|
+
s.crLast = value == '\r'
|
|
191
|
+
text := s.text()
|
|
192
|
+
return s.handle(text)
|
|
193
|
+
}
|
|
194
|
+
s.line = append(s.line, value)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
func (s *EventStream[T]) text() string {
|
|
199
|
+
line := s.line
|
|
200
|
+
s.line = nil
|
|
201
|
+
if s.first {
|
|
202
|
+
s.first = false
|
|
203
|
+
// One leading byte order mark is not part of the stream.
|
|
204
|
+
line = bytes.TrimPrefix(line, []byte{0xEF, 0xBB, 0xBF})
|
|
205
|
+
}
|
|
206
|
+
return strings.ToValidUTF8(string(line), "\uFFFD")
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
func (s *EventStream[T]) handle(text string) (ServerSentEvent[T], bool) {
|
|
210
|
+
if text == "" {
|
|
211
|
+
if s.data == nil {
|
|
212
|
+
s.kind = ""
|
|
213
|
+
return ServerSentEvent[T]{}, false
|
|
214
|
+
}
|
|
215
|
+
data := strings.TrimSuffix(s.data.String(), "\n")
|
|
216
|
+
kind := s.kind
|
|
217
|
+
if kind == "" {
|
|
218
|
+
kind = "message"
|
|
219
|
+
}
|
|
220
|
+
s.kind, s.data = "", nil
|
|
221
|
+
value, err := s.read(data)
|
|
222
|
+
if err != nil {
|
|
223
|
+
s.err = err
|
|
224
|
+
s.done = true
|
|
225
|
+
_ = s.body.Close()
|
|
226
|
+
return ServerSentEvent[T]{}, false
|
|
227
|
+
}
|
|
228
|
+
return ServerSentEvent[T]{Event: kind, ID: s.lastID, Data: value}, true
|
|
229
|
+
}
|
|
230
|
+
if strings.HasPrefix(text, ":") {
|
|
231
|
+
return ServerSentEvent[T]{}, false
|
|
232
|
+
}
|
|
233
|
+
field, value := text, ""
|
|
234
|
+
if colon := strings.IndexByte(text, ':'); colon >= 0 {
|
|
235
|
+
field, value = text[:colon], strings.TrimPrefix(text[colon+1:], " ")
|
|
236
|
+
}
|
|
237
|
+
switch {
|
|
238
|
+
case field == "event":
|
|
239
|
+
s.kind = value
|
|
240
|
+
case field == "data":
|
|
241
|
+
if s.data == nil {
|
|
242
|
+
s.data = &strings.Builder{}
|
|
243
|
+
}
|
|
244
|
+
s.data.WriteString(value + "\n")
|
|
245
|
+
case field == "id" && !strings.Contains(value, "\x00"):
|
|
246
|
+
s.lastID = value
|
|
247
|
+
}
|
|
248
|
+
return ServerSentEvent[T]{}, false
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
func (s *EventStream[T]) read(data string) (T, error) {
|
|
252
|
+
var zero T
|
|
253
|
+
if s.decode == nil {
|
|
254
|
+
text, ok := any(data).(T)
|
|
255
|
+
if !ok {
|
|
256
|
+
return zero, &DecodeError{Path: "$", Message: "an event stream without a decoder has string data"}
|
|
257
|
+
}
|
|
258
|
+
return text, nil
|
|
259
|
+
}
|
|
260
|
+
parsed, err := ParseJSON([]byte(data))
|
|
261
|
+
if err != nil {
|
|
262
|
+
return zero, &DecodeError{Path: "$", Message: "the event data is not JSON: " + err.Error()}
|
|
263
|
+
}
|
|
264
|
+
return s.decode(parsed, "$")
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// @end
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
// sdkloom Go kit: test helpers shipped inside every generated SDK.
|
|
2
|
+
package sdkruntime
|
|
3
|
+
|
|
4
|
+
import (
|
|
5
|
+
"context"
|
|
6
|
+
"errors"
|
|
7
|
+
"fmt"
|
|
8
|
+
"net/url"
|
|
9
|
+
"regexp"
|
|
10
|
+
"strings"
|
|
11
|
+
"sync"
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
// @block testing.mock_transport
|
|
15
|
+
// @summary A transport for tests: answers requests from routes registered by method and path template, records every call and fails requests no route answers.
|
|
16
|
+
// @versions >=1.18
|
|
17
|
+
// @source [1] https://pkg.go.dev/encoding/json
|
|
18
|
+
// @quote [1] "UseNumber causes the Decoder to unmarshal a number into an interface value as a Number instead of as a float64."
|
|
19
|
+
// @retrieved 2026-12-05
|
|
20
|
+
// @tests tests/testing_test.go
|
|
21
|
+
// @requirement SDKL-MOCK-001
|
|
22
|
+
|
|
23
|
+
// MockRoute answers requests for one method and path template.
|
|
24
|
+
type MockRoute struct {
|
|
25
|
+
Method string
|
|
26
|
+
Template string
|
|
27
|
+
transport *MockTransport
|
|
28
|
+
predicate func(Request) bool
|
|
29
|
+
pattern *regexp.Regexp
|
|
30
|
+
answers []any
|
|
31
|
+
calls []Request
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// MockTransport answers requests from routes; routes added later take priority. Safe for concurrent use.
|
|
35
|
+
type MockTransport struct {
|
|
36
|
+
mutex sync.Mutex
|
|
37
|
+
routes []*MockRoute
|
|
38
|
+
calls []Request
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// NewMockTransport returns a transport without routes.
|
|
42
|
+
func NewMockTransport() *MockTransport {
|
|
43
|
+
return &MockTransport{}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// templatePattern matches a path: {name} is one segment, {+name} one or more.
|
|
47
|
+
func templatePattern(template string) *regexp.Regexp {
|
|
48
|
+
placeholders := regexp.MustCompile(`\{[^{}]+\}`)
|
|
49
|
+
var pattern strings.Builder
|
|
50
|
+
last := 0
|
|
51
|
+
for _, found := range placeholders.FindAllStringIndex(template, -1) {
|
|
52
|
+
pattern.WriteString(regexp.QuoteMeta(template[last:found[0]]))
|
|
53
|
+
if strings.HasPrefix(template[found[0]:], "{+") {
|
|
54
|
+
pattern.WriteString(".+")
|
|
55
|
+
} else {
|
|
56
|
+
pattern.WriteString("[^/]+")
|
|
57
|
+
}
|
|
58
|
+
last = found[1]
|
|
59
|
+
}
|
|
60
|
+
pattern.WriteString(regexp.QuoteMeta(template[last:]))
|
|
61
|
+
return regexp.MustCompile("^" + pattern.String() + "$")
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Route registers a route; predicate nil answers every matching request.
|
|
65
|
+
func (t *MockTransport) Route(method, template string, predicate func(Request) bool) *MockRoute {
|
|
66
|
+
t.mutex.Lock()
|
|
67
|
+
defer t.mutex.Unlock()
|
|
68
|
+
route := &MockRoute{Method: method, Template: template, transport: t, predicate: predicate, pattern: templatePattern(template)}
|
|
69
|
+
t.routes = append(t.routes, route)
|
|
70
|
+
return route
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Calls returns every request sent.
|
|
74
|
+
func (t *MockTransport) Calls() []Request {
|
|
75
|
+
t.mutex.Lock()
|
|
76
|
+
defer t.mutex.Unlock()
|
|
77
|
+
return append([]Request(nil), t.calls...)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
func pathOf(raw string) string {
|
|
81
|
+
parsed, err := url.Parse(raw)
|
|
82
|
+
if err != nil {
|
|
83
|
+
return raw
|
|
84
|
+
}
|
|
85
|
+
return parsed.EscapedPath()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Send implements Transport.
|
|
89
|
+
func (t *MockTransport) Send(_ context.Context, request Request, _ SendOptions) (Response, error) {
|
|
90
|
+
t.mutex.Lock()
|
|
91
|
+
t.calls = append(t.calls, request)
|
|
92
|
+
// Routes added later take priority, and a route with answers beats one without.
|
|
93
|
+
var first, match *MockRoute
|
|
94
|
+
for i := len(t.routes) - 1; i >= 0 && match == nil; i-- {
|
|
95
|
+
if route := t.routes[i]; route.matches(request) {
|
|
96
|
+
if first == nil {
|
|
97
|
+
first = route
|
|
98
|
+
}
|
|
99
|
+
if len(route.answers) > 0 {
|
|
100
|
+
match = route
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
t.mutex.Unlock()
|
|
105
|
+
if match == nil {
|
|
106
|
+
match = first
|
|
107
|
+
}
|
|
108
|
+
if match != nil {
|
|
109
|
+
return match.answer(request)
|
|
110
|
+
}
|
|
111
|
+
return Response{}, fmt.Errorf("no mock for %s %s", request.Method, pathOf(request.URL))
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
func (r *MockRoute) matches(request Request) bool {
|
|
115
|
+
return request.Method == r.Method && r.pattern.MatchString(pathOf(request.URL)) && (r.predicate == nil || r.predicate(request))
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Reply adds an answer with a raw body; answers are used in order and the last one repeats.
|
|
119
|
+
func (r *MockRoute) Reply(status int, body []byte, headers ...Header) *MockRoute {
|
|
120
|
+
r.transport.mutex.Lock()
|
|
121
|
+
defer r.transport.mutex.Unlock()
|
|
122
|
+
r.answers = append(r.answers, Response{Status: status, Headers: headers, Body: body})
|
|
123
|
+
return r
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ReplyJSON adds an answer with a JSON body.
|
|
127
|
+
func (r *MockRoute) ReplyJSON(status int, value any) *MockRoute {
|
|
128
|
+
encoded, err := EncodeJSON(value)
|
|
129
|
+
if err != nil {
|
|
130
|
+
panic(err)
|
|
131
|
+
}
|
|
132
|
+
return r.Reply(status, encoded, Header{Name: "content-type", Value: "application/json"})
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// ReplyError makes the call fail without a response.
|
|
136
|
+
func (r *MockRoute) ReplyError(err error) *MockRoute {
|
|
137
|
+
r.transport.mutex.Lock()
|
|
138
|
+
defer r.transport.mutex.Unlock()
|
|
139
|
+
r.answers = append(r.answers, err)
|
|
140
|
+
return r
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Calls returns the requests this route answered.
|
|
144
|
+
func (r *MockRoute) Calls() []Request {
|
|
145
|
+
r.transport.mutex.Lock()
|
|
146
|
+
defer r.transport.mutex.Unlock()
|
|
147
|
+
return append([]Request(nil), r.calls...)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
func (r *MockRoute) answer(request Request) (Response, error) {
|
|
151
|
+
r.transport.mutex.Lock()
|
|
152
|
+
defer r.transport.mutex.Unlock()
|
|
153
|
+
r.calls = append(r.calls, request)
|
|
154
|
+
if len(r.answers) == 0 {
|
|
155
|
+
return Response{}, fmt.Errorf("mock for %s %s has no answer", r.Method, r.Template)
|
|
156
|
+
}
|
|
157
|
+
index := len(r.calls) - 1
|
|
158
|
+
if index >= len(r.answers) {
|
|
159
|
+
index = len(r.answers) - 1
|
|
160
|
+
}
|
|
161
|
+
switch answer := r.answers[index].(type) {
|
|
162
|
+
case error:
|
|
163
|
+
return Response{}, answer
|
|
164
|
+
default:
|
|
165
|
+
return answer.(Response), nil
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// @end
|
|
170
|
+
|
|
171
|
+
// @block testing.operation_mock
|
|
172
|
+
// @summary A typed mock for one operation: Returns() turns values into their wire form and checks them against the response type, Fails() and Raises() simulate errors, Calls() records requests.
|
|
173
|
+
// @versions >=1.18
|
|
174
|
+
// @source [1] https://pkg.go.dev/encoding/json
|
|
175
|
+
// @quote [1] "SetEscapeHTML specifies whether problematic HTML characters should be escaped inside JSON quoted strings."
|
|
176
|
+
// @retrieved 2026-12-05
|
|
177
|
+
// @tests tests/testing_test.go
|
|
178
|
+
// @requirement SDKL-MOCK-002
|
|
179
|
+
|
|
180
|
+
// OperationMock mocks one operation whose result is T.
|
|
181
|
+
type OperationMock[T any] struct {
|
|
182
|
+
Name string
|
|
183
|
+
route *MockRoute
|
|
184
|
+
decode Decoder[T]
|
|
185
|
+
encode func(T) any
|
|
186
|
+
// reply answers Returns itself, for results that are not JSON (event streams).
|
|
187
|
+
reply func(route *MockRoute, value T)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// NewOperationMock is used by generated mocks; decode checks values given to Returns, encode writes them (a union tag, say).
|
|
191
|
+
func NewOperationMock[T any](name string, route *MockRoute, decode Decoder[T], encode func(T) any) *OperationMock[T] {
|
|
192
|
+
return &OperationMock[T]{Name: name, route: route, decode: decode, encode: encode}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Returns answers with a success; the value is checked against the operation's response type first.
|
|
196
|
+
func (m *OperationMock[T]) Returns(value T) *OperationMock[T] {
|
|
197
|
+
if m.reply != nil {
|
|
198
|
+
m.reply(m.route, value)
|
|
199
|
+
return m
|
|
200
|
+
}
|
|
201
|
+
if raw, ok := any(value).([]byte); ok {
|
|
202
|
+
m.route.Reply(200, raw)
|
|
203
|
+
return m
|
|
204
|
+
}
|
|
205
|
+
var wire any = value
|
|
206
|
+
if m.encode != nil {
|
|
207
|
+
wire = m.encode(value)
|
|
208
|
+
}
|
|
209
|
+
encoded, err := EncodeJSON(wire)
|
|
210
|
+
if err != nil {
|
|
211
|
+
panic(err)
|
|
212
|
+
}
|
|
213
|
+
if m.decode != nil {
|
|
214
|
+
parsed, err := ParseJSON(encoded)
|
|
215
|
+
if err == nil {
|
|
216
|
+
_, err = m.decode(parsed, "$")
|
|
217
|
+
}
|
|
218
|
+
if err != nil {
|
|
219
|
+
panic(fmt.Sprintf("%s mock: %v", m.Name, err))
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
m.route.Reply(200, encoded, Header{Name: "content-type", Value: "application/json"})
|
|
223
|
+
return m
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// NewEventsMock is used by generated mocks of an event stream: Returns takes the events to send; decode checks each
|
|
227
|
+
// event's data and encode writes it as JSON (both nil for text data).
|
|
228
|
+
func NewEventsMock[T any](name string, route *MockRoute, decode Decoder[T], encode func(T) any) *OperationMock[[]ServerSentEvent[T]] {
|
|
229
|
+
mock := &OperationMock[[]ServerSentEvent[T]]{Name: name, route: route}
|
|
230
|
+
mock.reply = func(target *MockRoute, events []ServerSentEvent[T]) {
|
|
231
|
+
if decode != nil {
|
|
232
|
+
for _, event := range events {
|
|
233
|
+
encoded, err := EncodeJSON(encode(event.Data))
|
|
234
|
+
if err == nil {
|
|
235
|
+
var parsed any
|
|
236
|
+
if parsed, err = ParseJSON(encoded); err == nil {
|
|
237
|
+
_, err = decode(parsed, "$")
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if err != nil {
|
|
241
|
+
panic(fmt.Sprintf("%s mock: %v", name, err))
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
body, err := EncodeEvents(events, encode)
|
|
246
|
+
if err != nil {
|
|
247
|
+
panic(err)
|
|
248
|
+
}
|
|
249
|
+
target.Reply(200, body, Header{Name: "content-type", Value: "text/event-stream"})
|
|
250
|
+
}
|
|
251
|
+
return mock
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// ReturnsNothing answers with an empty success, for operations without a result.
|
|
255
|
+
func (m *OperationMock[T]) ReturnsNothing() *OperationMock[T] {
|
|
256
|
+
m.route.Reply(200, nil)
|
|
257
|
+
return m
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Fails answers with an HTTP error status and an optional JSON body.
|
|
261
|
+
func (m *OperationMock[T]) Fails(status int, body ...any) *OperationMock[T] {
|
|
262
|
+
if len(body) == 0 {
|
|
263
|
+
m.route.Reply(status, nil)
|
|
264
|
+
} else {
|
|
265
|
+
m.route.ReplyJSON(status, body[0])
|
|
266
|
+
}
|
|
267
|
+
return m
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Raises makes the call fail without a response, e.g. a network error or a timeout.
|
|
271
|
+
func (m *OperationMock[T]) Raises(err error) *OperationMock[T] {
|
|
272
|
+
m.route.ReplyError(err)
|
|
273
|
+
return m
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Then reads naturally between answers: mock.GetPayment.Fails(503).Then().Returns(value).
|
|
277
|
+
func (m *OperationMock[T]) Then() *OperationMock[T] {
|
|
278
|
+
return m
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// When is a mock for the same operation that only answers requests matching predicate.
|
|
282
|
+
func (m *OperationMock[T]) When(predicate func(Request) bool) *OperationMock[T] {
|
|
283
|
+
narrowed := NewOperationMock(m.Name, m.route.transport.Route(m.route.Method, m.route.Template, predicate), m.decode, m.encode)
|
|
284
|
+
narrowed.reply = m.reply
|
|
285
|
+
return narrowed
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Calls returns the requests this operation received.
|
|
289
|
+
func (m *OperationMock[T]) Calls() []Request {
|
|
290
|
+
return m.route.Calls()
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// ErrNoResponse is a ready-made transport failure for Raises.
|
|
294
|
+
var ErrNoResponse = &TransportError{Reason: "network", Cause: errors.New("no response")}
|
|
295
|
+
|
|
296
|
+
// @end
|
|
297
|
+
|
|
298
|
+
// @block testing.fake_clock
|
|
299
|
+
// @summary A clock for tests that moves only when slept or advanced, so retries and expiries run instantly.
|
|
300
|
+
// @versions >=1.18
|
|
301
|
+
// @source [1] https://pkg.go.dev/time
|
|
302
|
+
// @quote [1] "Sleep pauses the current goroutine for at least the duration d."
|
|
303
|
+
// @retrieved 2026-12-05
|
|
304
|
+
// @tests tests/testing_test.go
|
|
305
|
+
// @requirement SDKL-MOCK-002
|
|
306
|
+
|
|
307
|
+
// FakeClock moves only when slept or advanced. Safe for concurrent use.
|
|
308
|
+
type FakeClock struct {
|
|
309
|
+
mutex sync.Mutex
|
|
310
|
+
now int64
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// NewFakeClock starts at start milliseconds.
|
|
314
|
+
func NewFakeClock(start int64) *FakeClock {
|
|
315
|
+
return &FakeClock{now: start}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Now implements Clock.
|
|
319
|
+
func (c *FakeClock) Now() int64 {
|
|
320
|
+
c.mutex.Lock()
|
|
321
|
+
defer c.mutex.Unlock()
|
|
322
|
+
return c.now
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Sleep implements Clock: time moves on at once.
|
|
326
|
+
func (c *FakeClock) Sleep(ms int64) {
|
|
327
|
+
c.Advance(ms)
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Advance moves the clock on.
|
|
331
|
+
func (c *FakeClock) Advance(ms int64) {
|
|
332
|
+
c.mutex.Lock()
|
|
333
|
+
defer c.mutex.Unlock()
|
|
334
|
+
c.now += ms
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// RecordedEvent is one event a mock client recorded.
|
|
338
|
+
type RecordedEvent struct {
|
|
339
|
+
Name string
|
|
340
|
+
Fields map[string]any
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// @end
|