Skip to main content
Glama
websockets.test.ts1.9 kB
// SPDX-FileCopyrightText: Copyright Orangebot, Inc. and Medplum contributors // SPDX-License-Identifier: Apache-2.0 import { sleep } from '@medplum/core'; import type { Express } from 'express'; import express from 'express'; import type { Server } from 'http'; import type { AddressInfo } from 'net'; import request from 'superwstest'; import WebSocket from 'ws'; import { initApp, shutdownApp } from './app'; import { loadTestConfig } from './config/loader'; import type { MedplumServerConfig } from './config/types'; import { withTestContext } from './test.setup'; describe('WebSockets', () => { let app: Express; let config: MedplumServerConfig; let server: Server; beforeAll(async () => { app = express(); config = await loadTestConfig(); server = await initApp(app, config); await new Promise<void>((resolve) => { server.listen(0, 'localhost', 8511, resolve); }); }); afterAll(async () => { await shutdownApp(); }); test('Echo', () => withTestContext(async () => { await request(server) .ws('/ws/echo') .exec(async () => { await sleep(10); }) .sendText('foo') .expectText('foo') .sendText('bar') .expectText('bar') .close() .expectClosed(); })); test('Invalid endpoint', () => withTestContext(async () => { await request(server).ws('/foo').expectConnectionError(); const serverUrl = `localhost:${(server.address() as AddressInfo).port}`; // Make sure even when we error, we are getting back a response from server to prevent hanging socket connection const ws = new WebSocket(`ws://${serverUrl}/fhircast/STU3`); const err = await new Promise<Error>((resolve) => { ws.on('error', (err: Error) => { resolve(err); }); }); expect(err.message).toContain('404'); })); });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/medplum/medplum'

If you have feedback or need assistance with the MCP directory API, please join our Discord server