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,197 @@
|
|
|
1
|
+
// sdkloom C# kit: reading a stream of server-sent events.
|
|
2
|
+
|
|
3
|
+
using System;
|
|
4
|
+
using System.Collections.Generic;
|
|
5
|
+
using System.IO;
|
|
6
|
+
using System.Runtime.CompilerServices;
|
|
7
|
+
using System.Text;
|
|
8
|
+
using System.Threading;
|
|
9
|
+
using System.Threading.Tasks;
|
|
10
|
+
|
|
11
|
+
namespace Sdkloom.Runtime
|
|
12
|
+
{
|
|
13
|
+
// @block sse.parse
|
|
14
|
+
// @summary Reads a text/event-stream response as it arrives as an IAsyncEnumerable: 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).
|
|
15
|
+
// @versions >=3
|
|
16
|
+
// @source [1] https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
17
|
+
// @quote [1] "Event streams in this format must always be encoded as UTF-8."
|
|
18
|
+
// @quote [1] "Once the end of the file is reached, any pending data must be discarded."
|
|
19
|
+
// @quote [1] "If value starts with a U+0020 SPACE"
|
|
20
|
+
// @source [2] https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.iasyncenumerable-1
|
|
21
|
+
// @quote [2] "Exposes an enumerator that provides asynchronous iteration over values of a specified type."
|
|
22
|
+
// @retrieved 2026-12-06
|
|
23
|
+
// @tests tests/SseTests.cs
|
|
24
|
+
// @requirement SDKL-SSE-001
|
|
25
|
+
|
|
26
|
+
/// <summary>One server-sent event: its type ("message" unless the server named one), the last event ID and its data.</summary>
|
|
27
|
+
public sealed class ServerSentEvent<T> : IEquatable<ServerSentEvent<T>>
|
|
28
|
+
{
|
|
29
|
+
public ServerSentEvent(string @event, string id, T data)
|
|
30
|
+
{
|
|
31
|
+
Event = @event;
|
|
32
|
+
Id = id;
|
|
33
|
+
Data = data;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public string Event { get; }
|
|
37
|
+
|
|
38
|
+
public string Id { get; }
|
|
39
|
+
|
|
40
|
+
public T Data { get; }
|
|
41
|
+
|
|
42
|
+
public bool Equals(ServerSentEvent<T>? other) =>
|
|
43
|
+
other != null && Event == other.Event && Id == other.Id && EqualityComparer<T>.Default.Equals(Data, other.Data);
|
|
44
|
+
|
|
45
|
+
public override bool Equals(object? obj) => Equals(obj as ServerSentEvent<T>);
|
|
46
|
+
|
|
47
|
+
public override int GetHashCode() => (Event, Id, Data).GetHashCode();
|
|
48
|
+
|
|
49
|
+
public override string ToString() => $"ServerSentEvent {{ Event = {Event}, Id = {Id}, Data = {Data} }}";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// <summary>Reads and writes event streams.</summary>
|
|
53
|
+
public static class EventStreams
|
|
54
|
+
{
|
|
55
|
+
/// <summary>
|
|
56
|
+
/// Events as they arrive. decode reads each event's JSON data; without it the data stays text (T is string).
|
|
57
|
+
/// Leaving the loop early ends the request.
|
|
58
|
+
/// </summary>
|
|
59
|
+
public static async IAsyncEnumerable<ServerSentEvent<T>> Read<T>(HttpResponse response, Func<Json, string, T>? decode, [EnumeratorCancellation] CancellationToken cancellationToken = default)
|
|
60
|
+
{
|
|
61
|
+
using var body = response.OpenBody();
|
|
62
|
+
var chunk = new byte[8192];
|
|
63
|
+
var line = new MemoryStream();
|
|
64
|
+
var crLast = false;
|
|
65
|
+
var first = true;
|
|
66
|
+
var type = "";
|
|
67
|
+
StringBuilder? data = null;
|
|
68
|
+
var lastId = "";
|
|
69
|
+
for (;;)
|
|
70
|
+
{
|
|
71
|
+
cancellationToken.ThrowIfCancellationRequested();
|
|
72
|
+
var count = await body.ReadAsync(chunk, 0, chunk.Length, cancellationToken).ConfigureAwait(false);
|
|
73
|
+
if (count == 0)
|
|
74
|
+
{
|
|
75
|
+
yield break;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
for (var index = 0; index < count; index++)
|
|
79
|
+
{
|
|
80
|
+
var value = chunk[index];
|
|
81
|
+
if (crLast)
|
|
82
|
+
{
|
|
83
|
+
crLast = false;
|
|
84
|
+
if (value == (byte)'\n')
|
|
85
|
+
{
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (value != (byte)'\r' && value != (byte)'\n')
|
|
91
|
+
{
|
|
92
|
+
line.WriteByte(value);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
crLast = value == (byte)'\r';
|
|
97
|
+
var bytes = line.ToArray();
|
|
98
|
+
line.SetLength(0);
|
|
99
|
+
var skip = first && bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF ? 3 : 0;
|
|
100
|
+
first = false;
|
|
101
|
+
var text = Encoding.UTF8.GetString(bytes, skip, bytes.Length - skip);
|
|
102
|
+
if (text.Length == 0)
|
|
103
|
+
{
|
|
104
|
+
if (data == null)
|
|
105
|
+
{
|
|
106
|
+
type = "";
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
var content = data.ToString();
|
|
111
|
+
content = content.EndsWith("\n", StringComparison.Ordinal) ? content.Substring(0, content.Length - 1) : content;
|
|
112
|
+
var name = type.Length == 0 ? "message" : type;
|
|
113
|
+
type = "";
|
|
114
|
+
data = null;
|
|
115
|
+
yield return new ServerSentEvent<T>(name, lastId, ReadData(content, decode));
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (text[0] == ':')
|
|
120
|
+
{
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
var colon = text.IndexOf(':');
|
|
125
|
+
var field = colon < 0 ? text : text.Substring(0, colon);
|
|
126
|
+
var fieldValue = colon < 0 ? "" : text.Substring(colon + 1);
|
|
127
|
+
if (fieldValue.StartsWith(" ", StringComparison.Ordinal))
|
|
128
|
+
{
|
|
129
|
+
fieldValue = fieldValue.Substring(1);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (field == "event")
|
|
133
|
+
{
|
|
134
|
+
type = fieldValue;
|
|
135
|
+
}
|
|
136
|
+
else if (field == "data")
|
|
137
|
+
{
|
|
138
|
+
data = (data ?? new StringBuilder()).Append(fieldValue).Append('\n');
|
|
139
|
+
}
|
|
140
|
+
else if (field == "id" && fieldValue.IndexOf('\0') < 0)
|
|
141
|
+
{
|
|
142
|
+
lastId = fieldValue;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private static T ReadData<T>(string data, Func<Json, string, T>? decode)
|
|
149
|
+
{
|
|
150
|
+
if (decode == null)
|
|
151
|
+
{
|
|
152
|
+
return (T)(object)data;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
Json value;
|
|
156
|
+
try
|
|
157
|
+
{
|
|
158
|
+
value = Json.Parse(data);
|
|
159
|
+
}
|
|
160
|
+
catch (SdkException error) when (!(error is DecodeException))
|
|
161
|
+
{
|
|
162
|
+
throw new DecodeException("$", "the event data is not JSON: " + error.Message);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return decode(value, "$");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/// <summary>Writes events in the event-stream format (mocks and tests); each data is already text (JSON for a typed stream).</summary>
|
|
169
|
+
public static string Encode(IEnumerable<ServerSentEvent<string>> events)
|
|
170
|
+
{
|
|
171
|
+
var output = new StringBuilder();
|
|
172
|
+
foreach (var item in events)
|
|
173
|
+
{
|
|
174
|
+
if (item.Event != "message" && item.Event.Length > 0)
|
|
175
|
+
{
|
|
176
|
+
output.Append("event: ").Append(item.Event).Append('\n');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (item.Id.Length > 0)
|
|
180
|
+
{
|
|
181
|
+
output.Append("id: ").Append(item.Id).Append('\n');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
foreach (var part in item.Data.Replace("\r\n", "\n").Replace('\r', '\n').Split('\n'))
|
|
185
|
+
{
|
|
186
|
+
output.Append("data: ").Append(part).Append('\n');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
output.Append('\n');
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return output.ToString();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// @end
|
|
197
|
+
}
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
// sdkloom C# kit: test helpers shipped inside every generated SDK.
|
|
2
|
+
|
|
3
|
+
using System;
|
|
4
|
+
using System.Collections.Generic;
|
|
5
|
+
using System.Linq;
|
|
6
|
+
using System.Text;
|
|
7
|
+
using System.Threading;
|
|
8
|
+
using System.Threading.Tasks;
|
|
9
|
+
|
|
10
|
+
namespace Sdkloom.Runtime
|
|
11
|
+
{
|
|
12
|
+
// @block testing.mock_transport
|
|
13
|
+
// @summary A transport for tests: answers requests from routes registered by method and path template, records every call and fails requests no route answers.
|
|
14
|
+
// @versions >=3
|
|
15
|
+
// @source [1] https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/lock
|
|
16
|
+
// @quote [1] "The lock statement acquires the mutual-exclusion lock for a given object, executes a statement block, and then releases the lock."
|
|
17
|
+
// @retrieved 2026-12-05
|
|
18
|
+
// @tests tests/TestingTests.cs
|
|
19
|
+
// @requirement SDKL-MOCK-001
|
|
20
|
+
|
|
21
|
+
/// <summary>Answers requests from routes; routes added later take priority. Thread-safe.</summary>
|
|
22
|
+
public sealed class MockTransport : ITransport
|
|
23
|
+
{
|
|
24
|
+
private readonly object _gate = new object();
|
|
25
|
+
private readonly List<Route> _routes = new List<Route>();
|
|
26
|
+
private readonly List<HttpRequest> _calls = new List<HttpRequest>();
|
|
27
|
+
|
|
28
|
+
/// <summary>Registers a route; a null predicate answers every matching request.</summary>
|
|
29
|
+
public MockRoute Route(string method, string template, Func<HttpRequest, bool>? predicate = null)
|
|
30
|
+
{
|
|
31
|
+
var route = new Route(method, template, predicate);
|
|
32
|
+
lock (_gate)
|
|
33
|
+
{
|
|
34
|
+
_routes.Add(route);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return new MockRoute(this, route);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// <summary>Every request sent.</summary>
|
|
41
|
+
public IReadOnlyList<HttpRequest> Calls()
|
|
42
|
+
{
|
|
43
|
+
lock (_gate)
|
|
44
|
+
{
|
|
45
|
+
return _calls.ToArray();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public Task<HttpResponse> Send(HttpRequest request, SendOptions options)
|
|
50
|
+
{
|
|
51
|
+
var path = PathOf(request.Url);
|
|
52
|
+
lock (_gate)
|
|
53
|
+
{
|
|
54
|
+
_calls.Add(request);
|
|
55
|
+
// Routes added later take priority, and a route with answers beats one without.
|
|
56
|
+
var matching = _routes.AsEnumerable().Reverse().Where(candidate => candidate.Method == request.Method && Matches(candidate.Template, path) && (candidate.Predicate == null || candidate.Predicate(request))).ToList();
|
|
57
|
+
var route = matching.FirstOrDefault(candidate => candidate.Answers.Count > 0) ?? matching.FirstOrDefault()
|
|
58
|
+
?? throw new SdkException("no mock for " + request.Method + " " + path);
|
|
59
|
+
route.Calls.Add(request);
|
|
60
|
+
if (route.Answers.Count == 0)
|
|
61
|
+
{
|
|
62
|
+
throw new SdkException("mock for " + route.Method + " " + route.Template + " has no answer");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
var answer = route.Answers[Math.Min(route.Calls.Count - 1, route.Answers.Count - 1)];
|
|
66
|
+
return answer.Error != null ? Task.FromException<HttpResponse>(answer.Error) : Task.FromResult(answer.Response!);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
internal void Change(Action change)
|
|
71
|
+
{
|
|
72
|
+
lock (_gate)
|
|
73
|
+
{
|
|
74
|
+
change();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
internal T Read<T>(Func<T> read)
|
|
79
|
+
{
|
|
80
|
+
lock (_gate)
|
|
81
|
+
{
|
|
82
|
+
return read();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private static string PathOf(string url)
|
|
87
|
+
{
|
|
88
|
+
var scheme = url.IndexOf("://", StringComparison.Ordinal);
|
|
89
|
+
var rest = scheme < 0 ? url : url.Substring(scheme + 3);
|
|
90
|
+
var slash = rest.IndexOf('/');
|
|
91
|
+
var path = slash < 0 ? "/" : rest.Substring(slash);
|
|
92
|
+
var query = path.IndexOf('?');
|
|
93
|
+
return query < 0 ? path : path.Substring(0, query);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private static bool Matches(string template, string path) => Matches(template.Split('/'), 0, path.Split('/'), 0);
|
|
97
|
+
|
|
98
|
+
/// <summary>{name} matches one non-empty segment, {+name} one or more.</summary>
|
|
99
|
+
private static bool Matches(string[] wanted, int at, string[] actual, int from)
|
|
100
|
+
{
|
|
101
|
+
if (at == wanted.Length)
|
|
102
|
+
{
|
|
103
|
+
return from == actual.Length;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
var want = wanted[at];
|
|
107
|
+
var placeholder = want.StartsWith("{", StringComparison.Ordinal) && want.EndsWith("}", StringComparison.Ordinal);
|
|
108
|
+
if (placeholder && want.StartsWith("{+", StringComparison.Ordinal))
|
|
109
|
+
{
|
|
110
|
+
for (var taken = 1; from + taken <= actual.Length; taken++)
|
|
111
|
+
{
|
|
112
|
+
if (Matches(wanted, at + 1, actual, from + taken))
|
|
113
|
+
{
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return from < actual.Length && ((placeholder && actual[from].Length > 0) || want == actual[from]) && Matches(wanted, at + 1, actual, from + 1);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
internal sealed class Answer
|
|
126
|
+
{
|
|
127
|
+
public Answer(HttpResponse? response, Exception? error)
|
|
128
|
+
{
|
|
129
|
+
Response = response;
|
|
130
|
+
Error = error;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public HttpResponse? Response { get; }
|
|
134
|
+
|
|
135
|
+
public Exception? Error { get; }
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
internal sealed class Route
|
|
139
|
+
{
|
|
140
|
+
public Route(string method, string template, Func<HttpRequest, bool>? predicate)
|
|
141
|
+
{
|
|
142
|
+
Method = method;
|
|
143
|
+
Template = template;
|
|
144
|
+
Predicate = predicate;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
public string Method { get; }
|
|
148
|
+
|
|
149
|
+
public string Template { get; }
|
|
150
|
+
|
|
151
|
+
public Func<HttpRequest, bool>? Predicate { get; }
|
|
152
|
+
|
|
153
|
+
public List<Answer> Answers { get; } = new List<Answer>();
|
|
154
|
+
|
|
155
|
+
public List<HttpRequest> Calls { get; } = new List<HttpRequest>();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/// <summary>A route of a MockTransport.</summary>
|
|
159
|
+
public sealed class MockRoute
|
|
160
|
+
{
|
|
161
|
+
private readonly MockTransport _transport;
|
|
162
|
+
private readonly Route _route;
|
|
163
|
+
|
|
164
|
+
internal MockRoute(MockTransport transport, Route route)
|
|
165
|
+
{
|
|
166
|
+
_transport = transport;
|
|
167
|
+
_route = route;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/// <summary>Adds an answer with a raw body; answers are used in order and the last one repeats.</summary>
|
|
171
|
+
public MockRoute Reply(int status, byte[]? body = null, IReadOnlyList<KeyValuePair<string, string>>? headers = null)
|
|
172
|
+
{
|
|
173
|
+
_transport.Change(() => _route.Answers.Add(new Answer(new HttpResponse(status, headers, body), null)));
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/// <summary>Adds an answer with a JSON body.</summary>
|
|
178
|
+
public MockRoute ReplyJson(int status, Json value) =>
|
|
179
|
+
Reply(status, Encoding.UTF8.GetBytes(value.ToJsonString()), new[] { new KeyValuePair<string, string>("content-type", "application/json") });
|
|
180
|
+
|
|
181
|
+
/// <summary>Makes the call fail without a response.</summary>
|
|
182
|
+
public MockRoute ReplyError(Exception error)
|
|
183
|
+
{
|
|
184
|
+
_transport.Change(() => _route.Answers.Add(new Answer(null, error)));
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/// <summary>The requests this route answered.</summary>
|
|
189
|
+
public IReadOnlyList<HttpRequest> Calls() => _transport.Read(() => _route.Calls.ToArray());
|
|
190
|
+
|
|
191
|
+
internal MockRoute Narrow(Func<HttpRequest, bool> predicate) => _transport.Route(_route.Method, _route.Template, predicate);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// @end
|
|
195
|
+
|
|
196
|
+
// @block testing.operation_mock
|
|
197
|
+
// @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.
|
|
198
|
+
// @versions >=3
|
|
199
|
+
// @source [1] https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/generics
|
|
200
|
+
// @quote [1] "Generics let you write code that works with any type while keeping full type safety."
|
|
201
|
+
// @retrieved 2026-12-05
|
|
202
|
+
// @tests tests/TestingTests.cs
|
|
203
|
+
// @requirement SDKL-MOCK-002
|
|
204
|
+
|
|
205
|
+
/// <summary>Mocks one operation whose result is T.</summary>
|
|
206
|
+
public sealed class OperationMock<T>
|
|
207
|
+
{
|
|
208
|
+
private readonly MockRoute _route;
|
|
209
|
+
private readonly Func<Json, string, T>? _decode;
|
|
210
|
+
private readonly Func<T, Json>? _encode;
|
|
211
|
+
|
|
212
|
+
/// <summary>Used by generated mocks: decode checks values given to Returns(), encode writes them (with a union tag, say).</summary>
|
|
213
|
+
public OperationMock(string name, MockRoute route, Func<Json, string, T>? decode, Func<T, Json>? encode)
|
|
214
|
+
{
|
|
215
|
+
Name = name;
|
|
216
|
+
_route = route;
|
|
217
|
+
_decode = decode;
|
|
218
|
+
_encode = encode;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
public string Name { get; }
|
|
222
|
+
|
|
223
|
+
/// <summary>Answers with a success; the value is checked against the operation's response type first.</summary>
|
|
224
|
+
public OperationMock<T> Returns(T value)
|
|
225
|
+
{
|
|
226
|
+
if (_encode == null)
|
|
227
|
+
{
|
|
228
|
+
_route.Reply(200);
|
|
229
|
+
return this;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
var wire = _encode(value);
|
|
233
|
+
try
|
|
234
|
+
{
|
|
235
|
+
_decode?.Invoke(wire, "$");
|
|
236
|
+
}
|
|
237
|
+
catch (DecodeException error)
|
|
238
|
+
{
|
|
239
|
+
throw new InvalidOperationException(Name + " mock: " + error.Message, error);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
_route.ReplyJson(200, wire);
|
|
243
|
+
return this;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/// <summary>
|
|
247
|
+
/// Answers an event stream with these events. Each data is checked and written as JSON; a stream without a decoder
|
|
248
|
+
/// (string data) writes the text as it is.
|
|
249
|
+
/// </summary>
|
|
250
|
+
public OperationMock<T> Emits(params ServerSentEvent<T>[] events)
|
|
251
|
+
{
|
|
252
|
+
var written = new List<ServerSentEvent<string>>();
|
|
253
|
+
foreach (var item in events)
|
|
254
|
+
{
|
|
255
|
+
var wire = _encode == null ? Json.Null : _encode(item.Data);
|
|
256
|
+
string data;
|
|
257
|
+
if (_decode == null && wire.AsString() != null)
|
|
258
|
+
{
|
|
259
|
+
data = wire.AsString()!;
|
|
260
|
+
}
|
|
261
|
+
else
|
|
262
|
+
{
|
|
263
|
+
try
|
|
264
|
+
{
|
|
265
|
+
_decode?.Invoke(wire, "$");
|
|
266
|
+
}
|
|
267
|
+
catch (DecodeException error)
|
|
268
|
+
{
|
|
269
|
+
throw new InvalidOperationException(Name + " mock: " + error.Message, error);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
data = wire.ToJsonString();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
written.Add(new ServerSentEvent<string>(item.Event, item.Id, data));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
_route.Reply(200, Encoding.UTF8.GetBytes(EventStreams.Encode(written)), new[] { new KeyValuePair<string, string>("content-type", "text/event-stream") });
|
|
279
|
+
return this;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/// <summary>Answers with raw bytes, for operations that return bytes.</summary>
|
|
283
|
+
public OperationMock<T> ReturnsBytes(byte[] body)
|
|
284
|
+
{
|
|
285
|
+
_route.Reply(200, body);
|
|
286
|
+
return this;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/// <summary>Answers with an empty success, for operations without a result.</summary>
|
|
290
|
+
public OperationMock<T> ReturnsNothing()
|
|
291
|
+
{
|
|
292
|
+
_route.Reply(200);
|
|
293
|
+
return this;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/// <summary>Answers with an HTTP error status and an optional JSON body.</summary>
|
|
297
|
+
public OperationMock<T> Fails(int status, Json? body = null)
|
|
298
|
+
{
|
|
299
|
+
if (body == null)
|
|
300
|
+
{
|
|
301
|
+
_route.Reply(status);
|
|
302
|
+
}
|
|
303
|
+
else
|
|
304
|
+
{
|
|
305
|
+
_route.ReplyJson(status, body);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return this;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/// <summary>Makes the call fail without a response, e.g. a network error.</summary>
|
|
312
|
+
public OperationMock<T> Raises(Exception error)
|
|
313
|
+
{
|
|
314
|
+
_route.ReplyError(error);
|
|
315
|
+
return this;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/// <summary>Reads naturally between answers: mock.GetPayment.Fails(503).Then().Returns(value).</summary>
|
|
319
|
+
public OperationMock<T> Then() => this;
|
|
320
|
+
|
|
321
|
+
/// <summary>A mock for the same operation that only answers requests matching predicate.</summary>
|
|
322
|
+
public OperationMock<T> When(Func<HttpRequest, bool> predicate) => new OperationMock<T>(Name, _route.Narrow(predicate), _decode, _encode);
|
|
323
|
+
|
|
324
|
+
/// <summary>The requests this operation received.</summary>
|
|
325
|
+
public IReadOnlyList<HttpRequest> Calls() => _route.Calls();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// @end
|
|
329
|
+
|
|
330
|
+
// @block testing.fake_clock
|
|
331
|
+
// @summary A clock for tests that moves only when slept or advanced, so retries and expiries run instantly.
|
|
332
|
+
// @versions >=3
|
|
333
|
+
// @source [1] https://learn.microsoft.com/en-us/dotnet/api/system.threading.interlocked
|
|
334
|
+
// @quote [1] "Provides atomic operations for variables that are shared by multiple threads."
|
|
335
|
+
// @retrieved 2026-12-05
|
|
336
|
+
// @tests tests/TestingTests.cs
|
|
337
|
+
// @requirement SDKL-MOCK-002
|
|
338
|
+
|
|
339
|
+
/// <summary>Moves only when slept or advanced. Thread-safe.</summary>
|
|
340
|
+
public sealed class FakeClock : IClock
|
|
341
|
+
{
|
|
342
|
+
private long _now;
|
|
343
|
+
|
|
344
|
+
public FakeClock(long start = 0)
|
|
345
|
+
{
|
|
346
|
+
_now = start;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
public long Now() => Interlocked.Read(ref _now);
|
|
350
|
+
|
|
351
|
+
public Task Sleep(long ms, CancellationToken cancellationToken = default)
|
|
352
|
+
{
|
|
353
|
+
Advance(ms);
|
|
354
|
+
return Task.CompletedTask;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
public void Advance(long ms) => Interlocked.Add(ref _now, ms);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/// <summary>One event a mock client recorded.</summary>
|
|
361
|
+
public sealed class RecordedEvent
|
|
362
|
+
{
|
|
363
|
+
public RecordedEvent(string name, Json fields)
|
|
364
|
+
{
|
|
365
|
+
Name = name;
|
|
366
|
+
Fields = fields;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
public string Name { get; }
|
|
370
|
+
|
|
371
|
+
public Json Fields { get; }
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// @end
|
|
375
|
+
}
|