|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +import {ApiTestSetup, runApiTests} from '../../../../common/rpc/__tests__/runApiTests'; |
| 6 | +import {StreamingRpcClient} from '../../../../common'; |
| 7 | +import {RpcCodecs} from '../../../../common/codec/RpcCodecs'; |
| 8 | +import {RpcMessageCodecs} from '../../../../common/codec/RpcMessageCodecs'; |
| 9 | +import {Writer} from '@jsonjoy.com/util/lib/buffers/Writer'; |
| 10 | +import {Codecs} from '@jsonjoy.com/json-pack/lib/codecs/Codecs'; |
| 11 | +import {RpcMessageCodec} from '../../../../common/codec/types'; |
| 12 | +import {JsonValueCodec} from '@jsonjoy.com/json-pack/lib/codecs/types'; |
| 13 | + |
| 14 | +if (process.env.TEST_E2E) { |
| 15 | + const codecs = new RpcCodecs(new Codecs(new Writer()), new RpcMessageCodecs()); |
| 16 | + const {binary, compact, jsonRpc2} = codecs.messages; |
| 17 | + const {json, cbor, msgpack} = codecs.value; |
| 18 | + const cases: [specifier: string, protocol: RpcMessageCodec, req: JsonValueCodec, res: JsonValueCodec][] = [ |
| 19 | + ['rpc.rx.compact.json', compact, json, json], |
| 20 | + ['rpc.rx.compact.cbor', compact, cbor, cbor], |
| 21 | + ['rpc.rx.compact.msgpack', compact, msgpack, msgpack], |
| 22 | + ['rpc.rx.compact.json-cbor', compact, json, cbor], |
| 23 | + ['rpc.rx.compact.json-msgpack', compact, json, msgpack], |
| 24 | + ['rpc.rx.compact.cbor-json', compact, cbor, json], |
| 25 | + ['rpc.rx.compact.cbor-msgpack', compact, cbor, msgpack], |
| 26 | + ['rpc.rx.compact.msgpack-json', compact, msgpack, json], |
| 27 | + ['rpc.rx.compact.msgpack-cbor', compact, msgpack, cbor], |
| 28 | + |
| 29 | + ['rpc.rx.binary.cbor', binary, cbor, cbor], |
| 30 | + ['rpc.rx.binary.msgpack', binary, msgpack, msgpack], |
| 31 | + ['rpc.rx.binary.json', binary, json, json], |
| 32 | + ['rpc.rx.binary.json-cbor', binary, json, cbor], |
| 33 | + ['rpc.rx.binary.json-msgpack', binary, json, msgpack], |
| 34 | + ['rpc.rx.binary.cbor-json', binary, cbor, json], |
| 35 | + ['rpc.rx.binary.cbor-msgpack', binary, cbor, msgpack], |
| 36 | + ['rpc.rx.binary.msgpack-json', binary, msgpack, json], |
| 37 | + ['rpc.rx.binary.msgpack-cbor', binary, msgpack, cbor], |
| 38 | + |
| 39 | + ['rpc.json2.verbose.json', jsonRpc2, json, json], |
| 40 | + ['rpc.json2.verbose.cbor', jsonRpc2, cbor, cbor], |
| 41 | + ['rpc.json2.verbose.msgpack', jsonRpc2, msgpack, msgpack], |
| 42 | + ['rpc.json2.verbose.json-cbor', jsonRpc2, json, cbor], |
| 43 | + ['rpc.json2.verbose.json-msgpack', jsonRpc2, json, msgpack], |
| 44 | + ['rpc.json2.verbose.cbor-json', jsonRpc2, cbor, json], |
| 45 | + ['rpc.json2.verbose.cbor-msgpack', jsonRpc2, cbor, msgpack], |
| 46 | + ['rpc.json2.verbose.msgpack-json', jsonRpc2, msgpack, json], |
| 47 | + ['rpc.json2.verbose.msgpack-cbor', jsonRpc2, msgpack, cbor], |
| 48 | + ]; |
| 49 | + |
| 50 | + for (const [protocolSpecifier, msgCodec, reqCodec, resCodec] of cases) { |
| 51 | + const contentType = 'application/x.' + protocolSpecifier; |
| 52 | + const setup: ApiTestSetup = async () => { |
| 53 | + const client = new StreamingRpcClient({ |
| 54 | + send: async (messages) => { |
| 55 | + const port = +(process.env.PORT || 9999); |
| 56 | + const url = `http://localhost:${port}/rpc`; |
| 57 | + reqCodec.encoder.writer.reset(); |
| 58 | + msgCodec.encodeBatch(reqCodec, messages); |
| 59 | + const body = reqCodec.encoder.writer.flush(); |
| 60 | + try { |
| 61 | + const response = await fetch(url, { |
| 62 | + method: 'POST', |
| 63 | + headers: { |
| 64 | + 'Content-Type': contentType, |
| 65 | + }, |
| 66 | + body, |
| 67 | + }); |
| 68 | + const buffer = await response.arrayBuffer(); |
| 69 | + const data = new Uint8Array(buffer); |
| 70 | + const responseMessages = msgCodec.decodeBatch(resCodec, data); |
| 71 | + client.onMessages(responseMessages as any); |
| 72 | + } catch (err) { |
| 73 | + // tslint:disable-next-line:no-console |
| 74 | + console.error(err); |
| 75 | + } |
| 76 | + }, |
| 77 | + }); |
| 78 | + return {client}; |
| 79 | + }; |
| 80 | + describe(`Content-Type: ${contentType}`, () => { |
| 81 | + runApiTests(setup, {staticOnly: true}); |
| 82 | + }); |
| 83 | + } |
| 84 | +} else { |
| 85 | + test.skip('set TEST_E2E=1 env var to run this test suite', () => {}); |
| 86 | +} |
0 commit comments