-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add conformance tests for clients on Cloudflare workers (#1066)
- Loading branch information
1 parent
d4891c9
commit 9a16b1e
Showing
13 changed files
with
368 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env node | ||
|
||
const { run } = require("../dist/cjs/cloudflare/client.js"); | ||
|
||
run(); |
28 changes: 28 additions & 0 deletions
28
packages/connect-conformance/conformance-cloudflare-client.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
features: | ||
versions: | ||
- HTTP_VERSION_1 | ||
- HTTP_VERSION_2 | ||
protocols: | ||
- PROTOCOL_CONNECT | ||
# - PROTOCOL_GRPC Cloudflare turns gRPC-Web requests to gRPC, if the invoking request is gRPC. It doesn't directly support gRPC. | ||
- PROTOCOL_GRPC_WEB | ||
codecs: | ||
- CODEC_PROTO | ||
- CODEC_JSON | ||
compressions: | ||
- COMPRESSION_IDENTITY | ||
- COMPRESSION_GZIP | ||
- COMPRESSION_DEFLATE | ||
supportsTls: true | ||
supportsTlsClientCerts: false | ||
supportsConnectGet: true | ||
supportsHalfDuplexBidiOverHttp1: false | ||
supportsMessageReceiveLimit: false | ||
excludeCases: | ||
- useTls: false | ||
- protocol: PROTOCOL_CONNECT | ||
streamType: STREAM_TYPE_FULL_DUPLEX_BIDI_STREAM | ||
- protocol: PROTOCOL_GRPC_WEB | ||
streamType: STREAM_TYPE_FULL_DUPLEX_BIDI_STREAM | ||
- compression: COMPRESSION_DEFLATE | ||
streamType: STREAM_TYPE_UNARY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/connect-conformance/src/cloudflare/client-worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2021-2024 The Connect Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { createRegistry } from "@bufbuild/protobuf"; | ||
|
||
import { | ||
ClientCompatResponse, | ||
ClientErrorResult, | ||
} from "../gen/connectrpc/conformance/v1/client_compat_pb.js"; | ||
import { createTransport } from "./transport.js"; | ||
import invoke from "../invoke.js"; | ||
import { | ||
UnaryRequest, | ||
ServerStreamRequest, | ||
ClientStreamRequest, | ||
BidiStreamRequest, | ||
UnimplementedRequest, | ||
ConformancePayload_RequestInfo, | ||
IdempotentUnaryRequest, | ||
} from "../gen/connectrpc/conformance/v1/service_pb.js"; | ||
import { createWorkerHandler } from "./handler.js"; | ||
import { InvokeService } from "./invoke-service.js"; | ||
|
||
export default createWorkerHandler({ | ||
jsonOptions: { | ||
typeRegistry: createRegistry( | ||
UnaryRequest, | ||
ServerStreamRequest, | ||
ClientStreamRequest, | ||
BidiStreamRequest, | ||
UnimplementedRequest, | ||
IdempotentUnaryRequest, | ||
ConformancePayload_RequestInfo, | ||
), | ||
}, | ||
routes({ service }) { | ||
service(InvokeService, { | ||
async invoke(req) { | ||
const res = new ClientCompatResponse({ | ||
testName: req.testName, | ||
}); | ||
try { | ||
const invokeResult = await invoke(createTransport(req), req); | ||
res.result = { case: "response", value: invokeResult }; | ||
} catch (e) { | ||
res.result = { | ||
case: "error", | ||
value: new ClientErrorResult({ message: (e as Error).message }), | ||
}; | ||
} | ||
return res; | ||
}, | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2021-2024 The Connect Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { | ||
ClientCompatRequest, | ||
ClientCompatResponse, | ||
ClientErrorResult, | ||
} from "../gen/connectrpc/conformance/v1/client_compat_pb.js"; | ||
import { | ||
createConnectTransport, | ||
createGrpcTransport, | ||
createGrpcWebTransport, | ||
} from "@connectrpc/connect-node"; | ||
import { createPromiseClient } from "@connectrpc/connect"; | ||
import type { Transport } from "@connectrpc/connect"; | ||
import { InvokeService } from "./invoke-service.js"; | ||
import { parseArgs } from "node:util"; | ||
import { | ||
readSizeDelimitedBuffers, | ||
writeSizeDelimitedBuffer, | ||
} from "../protocol.js"; | ||
|
||
const { values: flags } = parseArgs({ | ||
args: process.argv.slice(2), | ||
options: { | ||
protocol: { | ||
type: "string", | ||
default: "connect", | ||
}, | ||
}, | ||
}); | ||
|
||
export async function run() { | ||
const workerUrl = `https://${process.env["CLOUDFLARE_WORKERS_CLIENT_HOST"]}/`; | ||
const transportOptions = { baseUrl: workerUrl, httpVersion: "2" } as const; | ||
let transport: Transport; | ||
switch (flags.protocol) { | ||
case "connect": | ||
transport = createConnectTransport(transportOptions); | ||
break; | ||
case "grpc": | ||
transport = createGrpcTransport(transportOptions); | ||
break; | ||
case "grpc-web": | ||
transport = createGrpcWebTransport(transportOptions); | ||
break; | ||
default: | ||
throw new Error(`Unknown protocol: ${flags.protocol}`); | ||
} | ||
const client = createPromiseClient(InvokeService, transport); | ||
for await (const next of readSizeDelimitedBuffers(process.stdin)) { | ||
const req = ClientCompatRequest.fromBinary(next); | ||
req.host = process.env["CLOUDFLARE_WORKERS_REFERENCE_SERVER_HOST"]!; | ||
let res = new ClientCompatResponse({ | ||
testName: req.testName, | ||
}); | ||
try { | ||
res = await client.invoke(req); | ||
} catch (e) { | ||
res.result = { | ||
case: "error", | ||
value: new ClientErrorResult({ | ||
message: (e as Error).message, | ||
}), | ||
}; | ||
} | ||
process.stdout.write(writeSizeDelimitedBuffer(res.toBinary())); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/connect-conformance/src/cloudflare/invoke-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2021-2024 The Connect Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { MethodKind } from "@bufbuild/protobuf"; | ||
import type { ServiceType } from "@bufbuild/protobuf"; | ||
import { | ||
ClientCompatRequest, | ||
ClientCompatResponse, | ||
} from "../gen/connectrpc/conformance/v1/client_compat_pb"; | ||
|
||
/** | ||
* Used to relay the test request to the worker. | ||
*/ | ||
export const InvokeService = { | ||
typeName: "conformance.v1.InvokeService", | ||
methods: { | ||
invoke: { | ||
name: "Invoke", | ||
kind: MethodKind.Unary, | ||
I: ClientCompatRequest, | ||
O: ClientCompatResponse, | ||
}, | ||
}, | ||
} satisfies ServiceType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.