-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
104 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import type * as http from 'http'; | ||
import { createSession } from 'better-sse'; | ||
import * as EventSource from 'eventsource'; | ||
import * as express from 'express'; | ||
import * as getPort from 'get-port'; | ||
import { createAppWithPath, createProxyMiddleware } from './test-kit'; | ||
import { RequestHandler } from '../../src'; | ||
|
||
describe('E2E SSE proxy', () => { | ||
let proxyServer: http.Server; | ||
let targetSSEServer: http.Server; | ||
|
||
const targetSSEApp = express(); | ||
|
||
targetSSEApp.get('/sse', async (req, res) => { | ||
const session = await createSession(req, res); | ||
|
||
// push 4x messages to client (stop character: Ø) | ||
'123Ø'.split('').forEach((val, index) => { | ||
setTimeout(() => { | ||
session.push(`Hello world! (sse message #${val})`); | ||
}, index * 100); | ||
}); | ||
}); | ||
|
||
let sseProxyMiddleware: RequestHandler; | ||
|
||
beforeEach(async () => { | ||
const targetSSEServerPort = await getPort(); | ||
|
||
return new Promise((resolve: any) => { | ||
sseProxyMiddleware = createProxyMiddleware({ | ||
target: `http://localhost:${targetSSEServerPort}/sse`, | ||
}); | ||
|
||
targetSSEServer = targetSSEApp.listen(targetSSEServerPort, resolve); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
return Promise.all([ | ||
new Promise((resolve) => targetSSEServer.close(resolve)), | ||
new Promise((resolve) => proxyServer.close(resolve)), | ||
]); | ||
}); | ||
|
||
it('should proxy SSE request', async () => { | ||
const freePort = await getPort(); | ||
proxyServer = createAppWithPath('/sse', sseProxyMiddleware).listen(freePort); | ||
|
||
const sse = new EventSource(`http://localhost:${freePort}/sse`); | ||
|
||
async function* receiveSseMessages() { | ||
while (true) { | ||
const message: string = await new Promise((resolve, reject) => { | ||
sse.addEventListener('message', ({ data }) => resolve(data)); | ||
sse.addEventListener('error', (err) => reject(err)); | ||
}); | ||
|
||
yield message; | ||
|
||
// stop generator | ||
if (message.includes('#Ø')) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
const receivedMessages: string[] = []; | ||
|
||
for await (const message of receiveSseMessages()) { | ||
receivedMessages.push(message); | ||
} | ||
|
||
expect(receivedMessages).toMatchInlineSnapshot(` | ||
[ | ||
""Hello world! (sse message #1)"", | ||
""Hello world! (sse message #2)"", | ||
""Hello world! (sse message #3)"", | ||
""Hello world! (sse message #Ø)"", | ||
] | ||
`); | ||
|
||
sse.close(); | ||
}); | ||
}); |
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