File
Blob: src/workerd/server/tests/python/python-rpc/worker.js
| 1 | // Copyright (c) 2025 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | import { WorkerEntrypoint } from 'cloudflare:workers'; |
| 5 | |
| 6 | import * as assert from 'node:assert'; |
| 7 | |
| 8 | export class JsRpcTester extends WorkerEntrypoint { |
| 9 | async noArgs() { |
| 10 | return 'hello from js'; |
| 11 | } |
| 12 | async oneArg(a) { |
| 13 | return `${a}`; |
| 14 | } |
| 15 | async identity(x) { |
| 16 | return x; |
| 17 | } |
| 18 | async handleResponse(resp) { |
| 19 | // Verify that we receive a JS object here... |
| 20 | assert.deepStrictEqual(resp.constructor.name, 'Response'); |
| 21 | return resp; |
| 22 | } |
| 23 | |
| 24 | async handleRequest(req) { |
| 25 | assert.deepStrictEqual(req.constructor.name, 'Request'); |
| 26 | return req; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | export default { |
| 31 | async test(ctrl, env, ctx) { |
| 32 | // JS types |
| 33 | for (const val of [ |
| 34 | 1, |
| 35 | 'test', |
| 36 | [1, 2, 3], |
| 37 | new Map([['key', 42]]), |
| 38 | 42, |
| 39 | 1.2345, |
| 40 | false, |
| 41 | true, |
| 42 | undefined, |
| 43 | ]) { |
| 44 | const response = await env.PythonRpc.identity(val); |
| 45 | assert.deepStrictEqual(response, val); |
| 46 | } |
| 47 | |
| 48 | const null_resp = await env.PythonRpc.identity(null); |
| 49 | assert.ok(null_resp === null || null_resp === undefined); |
| 50 | |
| 51 | // Web/API Types |
| 52 | const py_response = await env.PythonRpc.handle_response( |
| 53 | new Response('this is a response') |
| 54 | ); |
| 55 | assert.deepStrictEqual(await py_response.text(), 'this is a response'); |
| 56 | assert.equal(py_response.constructor.name, 'Response'); |
| 57 | |
| 58 | const py_request = await env.PythonRpc.handle_request( |
| 59 | new Request('https://test.com', { method: 'POST' }) |
| 60 | ); |
| 61 | assert.deepStrictEqual(py_request.method, 'POST'); |
| 62 | assert.equal(py_request.constructor.name, 'Request'); |
| 63 | }, |
| 64 | }; |