Skip to content

Commit

Permalink
Setup SSE Server to handle responses/feed requests back and forth
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieSinn committed Jun 12, 2024
1 parent c2a879f commit 7eeb208
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
15 changes: 14 additions & 1 deletion harness/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
callVariableValue,
sendCommand,
} from './commands'
import { config_sse } from '../mockData'
import { config_sse, SSEMessage } from '../mockData'
import { ProxyClientOptions } from './proxyClientOptions'
import SseStream from 'ssestream'
import { sseStreams } from '../mockServer'

const oldFetch = fetch

Expand Down Expand Up @@ -439,6 +441,17 @@ export class LocalTestClient extends BaseTestClient {
getValidConfig() {
return config_sse(getMockServerUrl(), `/client/${this.clientId}/sse`)
}

getSSEStream(): SseStream {
return sseStreams[this.clientId]
}
sendSSEMessage(message: SSEMessage) {
const sseStream = this.getSSEStream()
if (!sseStream) {
throw new Error('SSE Stream not initialized')
}
sseStream.write(JSON.stringify(message))
}
}

export class CloudTestClient extends BaseTestClient {
Expand Down
12 changes: 11 additions & 1 deletion harness/mockServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import Koa from 'koa'
import Router from '@koa/router'
import axios from 'axios'
import bodyParser from 'koa-bodyparser'
import SseStream from 'ssestream'

// NOTE: This file is excecuted by the jest environment, which does not share a memory space / module cache with the
// jest test files. This means that any things you want to be able to access from test context
// (like the unmatchedRequests) will need to be bound to the global scope inside the jest-environment and accessed
// through that

let unmatchedRequests = []

export const sseStreams: Map<string, SseStream> = new Map()
export const assertNoUnmatchedRequests = async (
currentClientId,
testNameMap,
Expand Down Expand Up @@ -48,6 +49,15 @@ export function initialize() {
return next()
}).all('/client/:clientId/sse', async (ctx) => {
console.log(ctx.body)
let sseStream = sseStreams[ctx.clientId]
if (!sseStream) {
sseStream = new SseStream(ctx.req)
sseStreams[ctx.clientId] = sseStream
}
sseStream.pipe(ctx.res)
ctx.res.on('close', () => {
sseStream.unpipe(ctx.res)
})
})
router.all('/(.*)', async (ctx) => {
const { headers, request } = ctx
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"@eresearchqut/jest-testcontainers": "^3.2.0",
"@koa/router": "^12.0.1",
"@types/jest": "^29.2.3",
"@types/koa__router": "^12.0.4",
"@types/koa-bodyparser": "^4.3.12",
"@types/koa__router": "^12.0.4",
"@typescript-eslint/eslint-plugin": "^5.44.0",
"@typescript-eslint/parser": "^5.44.0",
"axios": "^1.1.3",
Expand All @@ -52,6 +52,7 @@
"dependencies": {
"dotenv": "^16.0.3",
"object-path-immutable": "^4.1.2",
"ssestream": "^1.1.0",
"uuid": "^9.0.0"
},
"lint-staged": {
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ __metadata:
prettier: "npm:^3.2.5"
prettier-eslint: "npm:^16.3.0"
prettier-eslint-cli: "npm:^8.0.1"
ssestream: "npm:^1.1.0"
ts-jest: "npm:^29.0.3"
ts-node: "npm:^10.9.1"
typescript: "npm:^4.9.3"
Expand Down Expand Up @@ -6672,6 +6673,13 @@ __metadata:
languageName: node
linkType: hard

"ssestream@npm:^1.1.0":
version: 1.1.0
resolution: "ssestream@npm:1.1.0"
checksum: 10/29b0f313fd0a599360432a866ebf3d6355a3b137d3007f07fb32572f85dd3142575214264ff7f479fbb65e87f79c048c3af90506ad0303eecffa95c62b7656c5
languageName: node
linkType: hard

"ssh-remote-port-forward@npm:^1.0.4":
version: 1.0.4
resolution: "ssh-remote-port-forward@npm:1.0.4"
Expand Down

0 comments on commit 7eeb208

Please sign in to comment.