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,127 @@
|
|
|
1
|
+
// @requirement SDKL-JSON-001
|
|
2
|
+
// @requirement SDKL-JSON-002
|
|
3
|
+
// @requirement SDKL-JSON-003
|
|
4
|
+
// @requirement SDKL-JSON-004
|
|
5
|
+
package sdkruntime
|
|
6
|
+
|
|
7
|
+
import (
|
|
8
|
+
"strings"
|
|
9
|
+
"testing"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
func parse(t *testing.T, text string) any {
|
|
13
|
+
t.Helper()
|
|
14
|
+
value, err := ParseJSON([]byte(text))
|
|
15
|
+
if err != nil {
|
|
16
|
+
t.Fatal(err)
|
|
17
|
+
}
|
|
18
|
+
return value
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
func TestDecodersCheckTypesAndNamePaths(t *testing.T) {
|
|
22
|
+
object, _ := AsObject(parse(t, `{"n":2.0,"big":9007199254740993,"d":"10.50","e":"brand_new","x":"s"}`), "$")
|
|
23
|
+
|
|
24
|
+
if n, err := Required(object, "n", "$", AsInteger); err != nil || n != 2 {
|
|
25
|
+
t.Fatalf("2.0 is an integer: %v %v", n, err)
|
|
26
|
+
}
|
|
27
|
+
if big, _ := Required(object, "big", "$", AsInteger); big != 9007199254740993 {
|
|
28
|
+
t.Fatalf("64-bit integers stay exact, got %d", big)
|
|
29
|
+
}
|
|
30
|
+
if d, _ := Required(object, "d", "$", AsDecimal); d != "10.50" {
|
|
31
|
+
t.Fatalf("decimal digits kept, got %s", d)
|
|
32
|
+
}
|
|
33
|
+
if e, _ := Required(object, "e", "$", AsEnum("a", "b")); e != "brand_new" {
|
|
34
|
+
t.Fatal("unknown enum values are kept")
|
|
35
|
+
}
|
|
36
|
+
_, err := Required(object, "x", "$", AsInteger)
|
|
37
|
+
if err == nil || err.Error() != `$.x: expected an integer, got string "s"` {
|
|
38
|
+
t.Fatalf("got %v", err)
|
|
39
|
+
}
|
|
40
|
+
_, err = Required(object, "missing", "$", AsString)
|
|
41
|
+
if err == nil || err.Error() != "$.missing: is required" {
|
|
42
|
+
t.Fatalf("got %v", err)
|
|
43
|
+
}
|
|
44
|
+
_, err = AsList(AsInteger)([]any{"1"}, "$.items")
|
|
45
|
+
if err == nil || !strings.HasPrefix(err.Error(), "$.items[0]:") {
|
|
46
|
+
t.Fatalf("got %v", err)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
func TestOptionalAndNullableFields(t *testing.T) {
|
|
51
|
+
object, _ := AsObject(parse(t, `{"a":null,"b":"x"}`), "$")
|
|
52
|
+
|
|
53
|
+
absent, _ := Optional(object, "missing", "$", AsString)
|
|
54
|
+
null, _ := Required(object, "a", "$", Nullable(AsString))
|
|
55
|
+
present, _ := Optional(object, "b", "$", Nullable(AsString))
|
|
56
|
+
if absent.Set || null != nil || !present.Set || *present.Value != "x" {
|
|
57
|
+
t.Fatalf("absent %v null %v present %v", absent, null, present)
|
|
58
|
+
}
|
|
59
|
+
// Many servers send null for a field they leave out: for an optional field that cannot be null it reads as absent,
|
|
60
|
+
// while a nullable one keeps the null.
|
|
61
|
+
nullAsAbsent, err := Optional(object, "a", "$", AsString)
|
|
62
|
+
nullKept, _ := Optional(object, "a", "$", Nullable(AsString))
|
|
63
|
+
if err != nil || nullAsAbsent.Set || !nullKept.Set || nullKept.Value != nil {
|
|
64
|
+
t.Fatalf("null as absent %v (%v), kept %v", nullAsAbsent, err, nullKept)
|
|
65
|
+
}
|
|
66
|
+
if Some(3).Or(1) != 3 || (Opt[int]{}).Or(1) != 1 {
|
|
67
|
+
t.Fatal("Or")
|
|
68
|
+
}
|
|
69
|
+
extra := Extra(object, "a")
|
|
70
|
+
if len(extra) != 1 || extra["b"] != "x" {
|
|
71
|
+
t.Fatalf("extra %v", extra)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
func TestDecimalsArePlainAndExact(t *testing.T) {
|
|
76
|
+
for text, want := range map[string]string{"1E+3": "1000", "1.5e-2": "0.015", "-0.0": "0.0", "12.30": "12.30"} {
|
|
77
|
+
if got, _ := AsDecimal(parse(t, text), "$"); string(got) != want {
|
|
78
|
+
t.Errorf("%s: got %s, want %s", text, got, want)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
type point struct{ x Opt[int64] }
|
|
84
|
+
|
|
85
|
+
func (p point) ToJSON() map[string]any {
|
|
86
|
+
data := map[string]any{"kind": "point", "x": Unset}
|
|
87
|
+
if value, ok := p.x.Get(); ok {
|
|
88
|
+
data["x"] = value
|
|
89
|
+
}
|
|
90
|
+
return data
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
func TestEncodingIsCompactSortedAndLeavesOutUnsetFields(t *testing.T) {
|
|
94
|
+
encoded, err := EncodeJSON(map[string]any{"b": Unset, "a": int64(1), "c": "ş<&>", "d": Decimal("1.50"), "e": nil, "f": tenth + 0.2, "p": point{}})
|
|
95
|
+
|
|
96
|
+
want := `{"a":1,"c":"ş<&>","d":"1.50","e":null,"f":0.30000000000000004,"p":{"kind":"point"}}`
|
|
97
|
+
if err != nil || string(encoded) != want {
|
|
98
|
+
t.Fatalf("got %s (%v)", encoded, err)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
func TestEncodersTurnTypedValuesIntoJSONData(t *testing.T) {
|
|
103
|
+
encoded, err := EncodeJSON(map[string]any{
|
|
104
|
+
"list": EncodeList([]string{"a", "b"}, Plain[string]),
|
|
105
|
+
"empty": EncodeList([]int64(nil), Plain[int64]),
|
|
106
|
+
"map": EncodeMap(map[string]int64{"x": 1}, Plain[int64]),
|
|
107
|
+
"null": EncodeNullable[string](nil, Plain[string]),
|
|
108
|
+
"some": EncodeNullable(Ptr("v"), Plain[string]),
|
|
109
|
+
"opt": OptValue(Some(int64(3))),
|
|
110
|
+
"none": OptValue(Opt[int64]{}),
|
|
111
|
+
})
|
|
112
|
+
want := `{"empty":[],"list":["a","b"],"map":{"x":1},"none":null,"null":null,"opt":3,"some":"v"}`
|
|
113
|
+
if err != nil || string(encoded) != want {
|
|
114
|
+
t.Fatalf("got %s (%v)", encoded, err)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
func TestParseJSONRejectsTrailingTextLikeJSONParse(t *testing.T) {
|
|
119
|
+
for _, text := range []string{"1]", "{} }", "1 2", "{} x"} {
|
|
120
|
+
if _, err := ParseJSON([]byte(text)); err == nil {
|
|
121
|
+
t.Fatalf("%q parsed; JSON.parse rejects it", text)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if value := parse(t, " [1] \n"); len(value.([]any)) != 1 {
|
|
125
|
+
t.Fatalf("whitespace after a value is fine, got %v", value)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// @requirement SDKL-XPILE-001
|
|
2
|
+
// @requirement SDKL-HELP-001
|
|
3
|
+
package sdkruntime
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"errors"
|
|
7
|
+
"math"
|
|
8
|
+
"regexp"
|
|
9
|
+
"testing"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
// A variable, so 0.1 + 0.2 is computed in float64 rather than folded as an exact constant.
|
|
13
|
+
var tenth = 0.1
|
|
14
|
+
|
|
15
|
+
func TestStringConvertsValuesLikeJavaScript(t *testing.T) {
|
|
16
|
+
cases := []struct {
|
|
17
|
+
value any
|
|
18
|
+
want string
|
|
19
|
+
}{
|
|
20
|
+
{nil, "null"}, {true, "true"}, {int64(2), "2"}, {2.0, "2"}, {math.Copysign(0, -1), "0"}, {0.5, "0.5"},
|
|
21
|
+
{1e21, "1e+21"}, {1e-7, "1e-7"}, {1e-5, "0.00001"}, {-2.5e-6, "-0.0000025"}, {123456789012.5, "123456789012.5"},
|
|
22
|
+
{tenth + 0.2, "0.30000000000000004"}, {1e23, "1e+23"}, {8.700326121778885e16, "87003261217788850"}, {math.Pow(2, -44), "5.684341886080802e-14"},
|
|
23
|
+
{math.NaN(), "NaN"}, {math.Inf(1), "Infinity"}, {[]any{int64(1), "a", nil}, "1,a,"}, {map[string]any{"a": 1}, "[object Object]"},
|
|
24
|
+
}
|
|
25
|
+
for _, c := range cases {
|
|
26
|
+
if got := JsStr(c.value); got != c.want {
|
|
27
|
+
t.Errorf("String(%v): got %q, want %q", c.value, got, c.want)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
func TestNumberParsesTextLikeJavaScript(t *testing.T) {
|
|
33
|
+
cases := map[string]float64{" 12 ": 12, "": 0, "0x1F": 31, "-1.5": -1.5, "1e3": 1000, "Infinity": math.Inf(1), "\u00a0 7 \ufeff": 7}
|
|
34
|
+
for text, want := range cases {
|
|
35
|
+
if got := JsNumber(text); got != want {
|
|
36
|
+
t.Errorf("Number(%q): got %v, want %v", text, got, want)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if !math.IsNaN(JsNumber("12px")) {
|
|
40
|
+
t.Error("12px is NaN")
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
func TestOperatorsFollowJavaScript(t *testing.T) {
|
|
45
|
+
if JsMod(-7, 3) != -1 || JsMod(7, -3) != 1 || !math.IsNaN(JsMod(1, 0)) {
|
|
46
|
+
t.Error("% takes the sign of the dividend")
|
|
47
|
+
}
|
|
48
|
+
if !math.IsInf(JsDiv(1, 0), 1) || !math.IsInf(JsDiv(-1, 0), -1) || !math.IsNaN(JsDiv(0, 0)) {
|
|
49
|
+
t.Error("division by zero")
|
|
50
|
+
}
|
|
51
|
+
if !math.IsInf(JsPow(0, -1), 1) || !math.IsNaN(JsPow(-8, 1.0/3)) || !math.IsNaN(JsPow(1, math.Inf(1))) {
|
|
52
|
+
t.Error("powers")
|
|
53
|
+
}
|
|
54
|
+
if !JsSame(int64(2), 2.0) || JsSame(true, int64(1)) || !JsSame(nil, nil) || JsSame("1", int64(1)) {
|
|
55
|
+
t.Error("===")
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
func TestBase64AndFieldReads(t *testing.T) {
|
|
60
|
+
if encoded, _ := JsBtoa("user:pä"); encoded != "dXNlcjpw5A==" {
|
|
61
|
+
t.Errorf("btoa: %s", encoded)
|
|
62
|
+
}
|
|
63
|
+
if _, err := JsBtoa("ş"); err == nil {
|
|
64
|
+
t.Error("a character above U+00FF fails")
|
|
65
|
+
}
|
|
66
|
+
if value, _ := JsGet(map[string]any{"token": "t"}, "token"); value != "t" {
|
|
67
|
+
t.Error("field read")
|
|
68
|
+
}
|
|
69
|
+
if value, _ := JsGet([]any{1}, "token"); value != nil {
|
|
70
|
+
t.Error("a field of a non-object is undefined")
|
|
71
|
+
}
|
|
72
|
+
if _, err := JsGet(nil, "token"); err == nil {
|
|
73
|
+
t.Error("reading from null fails")
|
|
74
|
+
}
|
|
75
|
+
if JsMessage(errors.New("boom")) != "boom" {
|
|
76
|
+
t.Error("message")
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
func TestMiddlewareHelpers(t *testing.T) {
|
|
81
|
+
seen := map[string]bool{}
|
|
82
|
+
for i := 0; i < 100; i++ {
|
|
83
|
+
id := RandomUUID()
|
|
84
|
+
if !regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`).MatchString(id) || seen[id] {
|
|
85
|
+
t.Fatalf("bad or repeated UUID %q", id)
|
|
86
|
+
}
|
|
87
|
+
seen[id] = true
|
|
88
|
+
}
|
|
89
|
+
if got := HmacSha256Hex("Jefe", "what do ya want for nothing?"); got != "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843" {
|
|
90
|
+
t.Fatal(got)
|
|
91
|
+
}
|
|
92
|
+
if got := HmacSha256Base64("Jefe", "what do ya want for nothing?"); got != "W9zBRr9gdU5qBCQmCJV1x1oAPwidJzmDnexYuWTsOEM=" {
|
|
93
|
+
t.Fatal(got)
|
|
94
|
+
}
|
|
95
|
+
if got := NewRequest("POST", "https://a.example").WithBody([]byte(`{"é":1}`)).Text(); got != `{"é":1}` {
|
|
96
|
+
t.Fatal(got)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// @requirement SDKL-HTTP-005
|
|
2
|
+
package sdkruntime
|
|
3
|
+
|
|
4
|
+
import (
|
|
5
|
+
"regexp"
|
|
6
|
+
"testing"
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
func TestMultipartEncodesFieldsAndFilesInOrder(t *testing.T) {
|
|
10
|
+
form := MultipartForm{}.Field("title", "Q4 \u00fc").File("report", `q4 "final".csv`, "text/csv", []byte("a,b\r\n1,2"))
|
|
11
|
+
want := "--b0undary\r\nContent-Disposition: form-data; name=\"title\"\r\n\r\nQ4 \u00fc\r\n" +
|
|
12
|
+
"--b0undary\r\nContent-Disposition: form-data; name=\"report\"; filename=\"q4 %22final%22.csv\"\r\nContent-Type: text/csv\r\n\r\na,b\r\n1,2\r\n" +
|
|
13
|
+
"--b0undary--\r\n"
|
|
14
|
+
if got := string(form.Encode("b0undary")); got != want {
|
|
15
|
+
t.Fatalf("got %q", got)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
func TestMultipartFormIsImmutableAndBoundariesAreRandom(t *testing.T) {
|
|
20
|
+
empty := MultipartForm{}
|
|
21
|
+
one := empty.Field("a", "1")
|
|
22
|
+
if len(empty.Parts) != 0 || len(one.Parts) != 1 {
|
|
23
|
+
t.Fatal(empty, one)
|
|
24
|
+
}
|
|
25
|
+
first := MultipartBoundary()
|
|
26
|
+
if !regexp.MustCompile(`^sdkloom-[0-9a-f-]{36}$`).MatchString(first) || first == MultipartBoundary() {
|
|
27
|
+
t.Fatal(first)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// @requirement SDKL-PAGE-001
|
|
2
|
+
package sdkruntime
|
|
3
|
+
|
|
4
|
+
import (
|
|
5
|
+
"context"
|
|
6
|
+
"errors"
|
|
7
|
+
"reflect"
|
|
8
|
+
"testing"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
func TestPagerYieldsEveryItemPageByPage(t *testing.T) {
|
|
12
|
+
pages := map[string]struct {
|
|
13
|
+
items []int
|
|
14
|
+
next string
|
|
15
|
+
}{"": {[]int{1, 2}, "b"}, "b": {[]int{}, "c"}, "c": {[]int{3}, ""}}
|
|
16
|
+
var fetched []string
|
|
17
|
+
pager := NewPager(context.Background(), "", func(_ context.Context, cursor string) (string, error) {
|
|
18
|
+
fetched = append(fetched, cursor)
|
|
19
|
+
return cursor, nil
|
|
20
|
+
}, func(cursor string) []int { return pages[cursor].items }, func(_ string, cursor string, _ []int) (string, bool) {
|
|
21
|
+
next := pages[cursor].next
|
|
22
|
+
return next, next != ""
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
all, err := pager.All()
|
|
26
|
+
if err != nil || !reflect.DeepEqual(all, []int{1, 2, 3}) || !reflect.DeepEqual(fetched, []string{"", "b", "c"}) {
|
|
27
|
+
t.Fatalf("items %v fetched %v err %v", all, fetched, err)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
func TestPagerFetchesLazilyAndStopsOnError(t *testing.T) {
|
|
32
|
+
calls := 0
|
|
33
|
+
pager := NewPager(context.Background(), 1, func(_ context.Context, page int) ([]int, error) {
|
|
34
|
+
calls++
|
|
35
|
+
if page == 2 {
|
|
36
|
+
return nil, errors.New("page 2 failed")
|
|
37
|
+
}
|
|
38
|
+
return []int{page}, nil
|
|
39
|
+
}, func(page []int) []int { return page }, func(page int, _ []int, _ []int) (int, bool) { return page + 1, true })
|
|
40
|
+
if calls != 0 {
|
|
41
|
+
t.Fatal("fetched before Next")
|
|
42
|
+
}
|
|
43
|
+
if !pager.Next() || pager.Item() != 1 || pager.Next() || pager.Err() == nil || pager.Err().Error() != "page 2 failed" || pager.Next() {
|
|
44
|
+
t.Fatalf("calls %d err %v", calls, pager.Err())
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
func TestNextLinkReadsRelNextAndResolveURLJoinsRelativeLinks(t *testing.T) {
|
|
49
|
+
text := func(value string) *string { return &value }
|
|
50
|
+
cases := map[string]string{
|
|
51
|
+
`<https://a.test/x?page=2&s=a,b>; rel="next", <https://a.test/x?page=9>; rel="last"`: "https://a.test/x?page=2&s=a,b",
|
|
52
|
+
`<https://a.test/x?page=1>; rel="prev first", <https://a.test/x?page=3>; REL="Next last"`: "https://a.test/x?page=3",
|
|
53
|
+
`</x?page=4>; title=more; rel=next`: "/x?page=4",
|
|
54
|
+
`<https://a.test/x?page=1>; rel="prev"`: "",
|
|
55
|
+
}
|
|
56
|
+
for header, want := range cases {
|
|
57
|
+
if got := NextLink(text(header)); got != want {
|
|
58
|
+
t.Errorf("NextLink(%q) = %q, want %q", header, got, want)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if NextLink(nil) != "" {
|
|
62
|
+
t.Error("nil header")
|
|
63
|
+
}
|
|
64
|
+
for _, c := range [][3]string{{"https://a.test/v1/", "/v1/x?page=2", "https://a.test/v1/x?page=2"}, {"https://a.test/v1", "x?page=2", "https://a.test/v1/x?page=2"}, {"https://a.test/v1", "https://b.test/y", "https://b.test/y"}} {
|
|
65
|
+
if got := ResolveURL(c[0], c[1]); got != c[2] {
|
|
66
|
+
t.Errorf("ResolveURL(%q, %q) = %q", c[0], c[1], got)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// @requirement SDKL-SSE-001
|
|
2
|
+
package sdkruntime
|
|
3
|
+
|
|
4
|
+
import (
|
|
5
|
+
"errors"
|
|
6
|
+
"io"
|
|
7
|
+
"reflect"
|
|
8
|
+
"strings"
|
|
9
|
+
"testing"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
// pieces hands over the given parts one Read at a time, like a network stream.
|
|
13
|
+
type pieces struct {
|
|
14
|
+
parts [][]byte
|
|
15
|
+
after error
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
func (p *pieces) Read(buffer []byte) (int, error) {
|
|
19
|
+
if len(p.parts) == 0 {
|
|
20
|
+
if p.after != nil {
|
|
21
|
+
return 0, p.after
|
|
22
|
+
}
|
|
23
|
+
return 0, io.EOF
|
|
24
|
+
}
|
|
25
|
+
count := copy(buffer, p.parts[0])
|
|
26
|
+
p.parts[0] = p.parts[0][count:]
|
|
27
|
+
if len(p.parts[0]) == 0 {
|
|
28
|
+
p.parts = p.parts[1:]
|
|
29
|
+
}
|
|
30
|
+
return count, nil
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
func (p *pieces) Close() error { return nil }
|
|
34
|
+
|
|
35
|
+
func textEvents(t *testing.T, stream *EventStream[string]) []ServerSentEvent[string] {
|
|
36
|
+
t.Helper()
|
|
37
|
+
var found []ServerSentEvent[string]
|
|
38
|
+
for stream.Next() {
|
|
39
|
+
found = append(found, stream.Event())
|
|
40
|
+
}
|
|
41
|
+
if err := stream.Err(); err != nil {
|
|
42
|
+
t.Fatal(err)
|
|
43
|
+
}
|
|
44
|
+
return found
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
func streamOf(parts ...string) Response {
|
|
48
|
+
body := &pieces{}
|
|
49
|
+
for _, part := range parts {
|
|
50
|
+
body.parts = append(body.parts, []byte(part))
|
|
51
|
+
}
|
|
52
|
+
return Response{Status: 200, Stream: body}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
func TestEventStreamParsesTypesMultiLineDataIdsCommentsAndEveryLineEnding(t *testing.T) {
|
|
56
|
+
stream := "\uFEFF: comment\r\nevent: created\r\ndata: {\"a\":1}\r\nid: 7\r\n\r\ndata:first\ndata: second\n\nretry: 10\rdata: spaced\r\r"
|
|
57
|
+
got := textEvents(t, NewEventStream[string](streamOf(stream), nil))
|
|
58
|
+
want := []ServerSentEvent[string]{{"created", "7", `{"a":1}`}, {"message", "7", "first\nsecond"}, {"message", "7", " spaced"}}
|
|
59
|
+
if !reflect.DeepEqual(got, want) {
|
|
60
|
+
t.Fatalf("got %q, want %q", got, want)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
func TestEventStreamSplitsAcrossReadsAndDropsAnUnfinishedEvent(t *testing.T) {
|
|
65
|
+
euro := "data: €\r\n\r\n"
|
|
66
|
+
got := textEvents(t, NewEventStream[string](streamOf(euro[:7], euro[7:10], euro[10:], "data: dropped\n"), nil))
|
|
67
|
+
if want := []ServerSentEvent[string]{{"message", "", "€"}}; !reflect.DeepEqual(got, want) {
|
|
68
|
+
t.Fatalf("got %q, want %q", got, want)
|
|
69
|
+
}
|
|
70
|
+
got = textEvents(t, NewEventStream[string](streamOf("event: ping\n\ndata\n\nid\ndata: x\n\n"), nil))
|
|
71
|
+
if want := []ServerSentEvent[string]{{"message", "", ""}, {"message", "", "x"}}; !reflect.DeepEqual(got, want) {
|
|
72
|
+
t.Fatalf("got %q, want %q", got, want)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
func TestEventStreamYieldsAnEventBeforeTheStreamEndsAndReportsReadErrors(t *testing.T) {
|
|
77
|
+
broken := errors.New("connection reset")
|
|
78
|
+
stream := NewEventStream[string](Response{Status: 200, Stream: &pieces{parts: [][]byte{[]byte("data: one\n\n")}, after: broken}}, nil)
|
|
79
|
+
if !stream.Next() || stream.Event().Data != "one" {
|
|
80
|
+
t.Fatalf("first event: %v", stream.Event())
|
|
81
|
+
}
|
|
82
|
+
if stream.Next() || !errors.Is(stream.Err(), broken) {
|
|
83
|
+
t.Fatalf("want the read error, got %v", stream.Err())
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
func TestEventStreamDecodesJSONFailsOnInvalidDataAndOpensLazily(t *testing.T) {
|
|
88
|
+
amount := func(value any, path string) (int64, error) {
|
|
89
|
+
fields, err := AsObject(value, path)
|
|
90
|
+
if err != nil {
|
|
91
|
+
return 0, err
|
|
92
|
+
}
|
|
93
|
+
n, err := AsInteger(fields["n"], path+".n")
|
|
94
|
+
return n * 10, err
|
|
95
|
+
}
|
|
96
|
+
opened := 0
|
|
97
|
+
lazy := LazyEvents(func() (*EventStream[int64], error) {
|
|
98
|
+
opened++
|
|
99
|
+
return NewEventStream(streamOf(`data: {"n": 2}`+"\n\n"), amount), nil
|
|
100
|
+
})
|
|
101
|
+
if opened != 0 {
|
|
102
|
+
t.Fatal("opened before Next")
|
|
103
|
+
}
|
|
104
|
+
if !lazy.Next() || lazy.Event().Data != 20 || lazy.Next() || lazy.Err() != nil || opened != 1 {
|
|
105
|
+
t.Fatalf("lazy stream: %v %v %d", lazy.Event(), lazy.Err(), opened)
|
|
106
|
+
}
|
|
107
|
+
failing := LazyEvents(func() (*EventStream[int64], error) { return nil, errors.New("refused") })
|
|
108
|
+
if failing.Next() || failing.Err() == nil {
|
|
109
|
+
t.Fatal("want the open error")
|
|
110
|
+
}
|
|
111
|
+
invalid := NewEventStream(streamOf("data: {nope\n\n"), amount)
|
|
112
|
+
var decodeErr *DecodeError
|
|
113
|
+
if invalid.Next() || !errors.As(invalid.Err(), &decodeErr) || !strings.HasPrefix(decodeErr.Message, "the event data is not JSON") {
|
|
114
|
+
t.Fatalf("want a DecodeError, got %v", invalid.Err())
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
func TestEncodeEventsWritesWhatTheParserReadsBack(t *testing.T) {
|
|
119
|
+
encoded, err := EncodeEvents([]ServerSentEvent[map[string]any]{{"created", "1", map[string]any{"note": "two\nlines"}}}, func(value map[string]any) any { return value })
|
|
120
|
+
if err != nil {
|
|
121
|
+
t.Fatal(err)
|
|
122
|
+
}
|
|
123
|
+
text, _ := EncodeEvents([]ServerSentEvent[string]{{"message", "", "first line\nsecond line"}}, nil)
|
|
124
|
+
got := textEvents(t, NewEventStream[string](streamOf(string(encoded)+string(text)), nil))
|
|
125
|
+
want := []ServerSentEvent[string]{{"created", "1", `{"note":"two\nlines"}`}, {"message", "1", "first line\nsecond line"}}
|
|
126
|
+
if !reflect.DeepEqual(got, want) {
|
|
127
|
+
t.Fatalf("got %q, want %q", got, want)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// @requirement SDKL-MOCK-001
|
|
2
|
+
// @requirement SDKL-MOCK-002
|
|
3
|
+
package sdkruntime
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"context"
|
|
7
|
+
"strings"
|
|
8
|
+
"testing"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
func send(transport *MockTransport, method, url string) (Response, error) {
|
|
12
|
+
return transport.Send(context.Background(), NewRequest(method, url), SendOptions{})
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
func TestRoutesMatchPathTemplatesAndRecordCalls(t *testing.T) {
|
|
16
|
+
transport := NewMockTransport()
|
|
17
|
+
route := transport.Route("GET", "/orders/{id}", nil).ReplyJSON(200, map[string]any{"id": "1"})
|
|
18
|
+
|
|
19
|
+
response, err := send(transport, "GET", "https://a.test/orders/42?x=1")
|
|
20
|
+
if err != nil || response.Text() != `{"id":"1"}` || len(route.Calls()) != 1 {
|
|
21
|
+
t.Fatalf("%v %v", response, err)
|
|
22
|
+
}
|
|
23
|
+
if _, err := send(transport, "GET", "https://a.test/orders/42/items"); err == nil || !strings.Contains(err.Error(), "no mock for GET /orders/42/items") {
|
|
24
|
+
t.Fatalf("got %v", err)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
func TestOperationMocksCheckValuesAndAnswerInOrder(t *testing.T) {
|
|
29
|
+
transport := NewMockTransport()
|
|
30
|
+
decode := func(value any, path string) (map[string]any, error) {
|
|
31
|
+
object, err := AsObject(value, path)
|
|
32
|
+
if err == nil {
|
|
33
|
+
_, err = Required(object, "id", path, AsString)
|
|
34
|
+
}
|
|
35
|
+
return object, err
|
|
36
|
+
}
|
|
37
|
+
mock := NewOperationMock("getOrder", transport.Route("GET", "/orders/{id}", nil), decode, nil)
|
|
38
|
+
|
|
39
|
+
mock.Fails(404, map[string]any{"message": "no"}).Then().Raises(ErrNoResponse).Then().Returns(map[string]any{"id": "1"})
|
|
40
|
+
|
|
41
|
+
first, _ := send(transport, "GET", "https://a.test/orders/1")
|
|
42
|
+
_, second := send(transport, "GET", "https://a.test/orders/1")
|
|
43
|
+
third, _ := send(transport, "GET", "https://a.test/orders/1")
|
|
44
|
+
fourth, _ := send(transport, "GET", "https://a.test/orders/1")
|
|
45
|
+
if first.Status != 404 || second != ErrNoResponse || third.Text() != `{"id":"1"}` || fourth.Text() != `{"id":"1"}` || len(mock.Calls()) != 4 {
|
|
46
|
+
t.Fatalf("%v %v %v %v", first, second, third, fourth)
|
|
47
|
+
}
|
|
48
|
+
defer func() {
|
|
49
|
+
if recovered := recover(); recovered == nil || !strings.Contains(recovered.(string), "$.id: is required") {
|
|
50
|
+
t.Fatalf("a value that does not decode is refused, got %v", recovered)
|
|
51
|
+
}
|
|
52
|
+
}()
|
|
53
|
+
mock.Returns(map[string]any{"name": "x"})
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
func TestWhenNarrowsAMockToMatchingRequests(t *testing.T) {
|
|
57
|
+
transport := NewMockTransport()
|
|
58
|
+
mock := NewOperationMock[map[string]any]("who", transport.Route("GET", "/who/{id}", nil), nil, nil)
|
|
59
|
+
mock.Returns(map[string]any{"who": "anyone"})
|
|
60
|
+
mock.When(func(r Request) bool { return strings.HasSuffix(r.URL, "/vip") }).Returns(map[string]any{"who": "vip"})
|
|
61
|
+
|
|
62
|
+
vip, _ := send(transport, "GET", "https://a.test/who/vip")
|
|
63
|
+
other, _ := send(transport, "GET", "https://a.test/who/x")
|
|
64
|
+
if vip.Text() != `{"who":"vip"}` || other.Text() != `{"who":"anyone"}` {
|
|
65
|
+
t.Fatalf("%s %s", vip.Text(), other.Text())
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
func TestBytesAreReturnedAsTheBody(t *testing.T) {
|
|
70
|
+
transport := NewMockTransport()
|
|
71
|
+
NewOperationMock[[]byte]("download", transport.Route("GET", "/f", nil), nil, nil).Returns([]byte{1, 2})
|
|
72
|
+
|
|
73
|
+
response, _ := send(transport, "GET", "https://a.test/f")
|
|
74
|
+
if len(response.Body) != 2 || response.Body[1] != 2 {
|
|
75
|
+
t.Fatalf("%v", response.Body)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
func TestTheFakeClockMovesOnlyWhenTold(t *testing.T) {
|
|
80
|
+
clock := NewFakeClock(5)
|
|
81
|
+
clock.Sleep(10)
|
|
82
|
+
clock.Advance(1)
|
|
83
|
+
if clock.Now() != 16 {
|
|
84
|
+
t.Fatalf("now %d", clock.Now())
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
func TestARouteWithAnswersBeatsALaterOneWithout(t *testing.T) {
|
|
89
|
+
transport := NewMockTransport()
|
|
90
|
+
transport.Route("GET", "/users/me", nil).Reply(200, nil)
|
|
91
|
+
byID := transport.Route("GET", "/users/{id}", nil)
|
|
92
|
+
|
|
93
|
+
if response, err := send(transport, "GET", "https://a.test/users/me"); err != nil || response.Status != 200 {
|
|
94
|
+
t.Fatalf("%v %v", response, err)
|
|
95
|
+
}
|
|
96
|
+
if _, err := send(transport, "GET", "https://a.test/users/7"); err == nil || !strings.Contains(err.Error(), "has no answer") {
|
|
97
|
+
t.Fatalf("got %v", err)
|
|
98
|
+
}
|
|
99
|
+
byID.Reply(204, nil)
|
|
100
|
+
if response, _ := send(transport, "GET", "https://a.test/users/me"); response.Status != 204 {
|
|
101
|
+
t.Fatalf("later answered route should win, got %d", response.Status)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
func TestReservedPlaceholderSpansSegmentsInURLsAndRoutes(t *testing.T) {
|
|
106
|
+
transport := NewMockTransport()
|
|
107
|
+
transport.Route("GET", "/files/{+path}/meta", nil).Reply(200, nil)
|
|
108
|
+
if response, err := send(transport, "GET", "https://a.test/files/a/b/meta"); err != nil || response.Status != 200 {
|
|
109
|
+
t.Fatalf("%v %v", response, err)
|
|
110
|
+
}
|
|
111
|
+
if _, err := send(transport, "GET", "https://a.test/files/meta"); err == nil {
|
|
112
|
+
t.Fatal("matched without a segment")
|
|
113
|
+
}
|
|
114
|
+
url, err := BuildURL("https://a.test", "/files/{+path}", map[string]any{"path": "docs/a b/ç.txt"}, nil)
|
|
115
|
+
if err != nil || url != "https://a.test/files/docs/a%20b/%C3%A7.txt" {
|
|
116
|
+
t.Fatalf("%q %v", url, err)
|
|
117
|
+
}
|
|
118
|
+
if _, err := BuildURL("https://a.test", "/files/{+path}", map[string]any{"path": "docs/../secret"}, nil); err == nil {
|
|
119
|
+
t.Fatal("dot segment accepted")
|
|
120
|
+
}
|
|
121
|
+
}
|