-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket-integration.test.mjs
More file actions
236 lines (190 loc) · 6.65 KB
/
websocket-integration.test.mjs
File metadata and controls
236 lines (190 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { test } from 'node:test'
import { strictEqual } from 'node:assert'
import { join } from 'node:path'
import { createServer, request as httpRequest } from 'node:http'
import { once } from 'node:events'
import { Python, Request } from '../index.js'
const fixturesDir = join(import.meta.dirname, 'fixtures')
test('Python - WebSocket Integration', async (t) => {
await t.test('HTTP upgrade to WebSocket simulation', async () => {
const python = new Python({
docroot: fixturesDir,
appTarget: 'websocket_app:app'
})
// Create a simple HTTP server
const server = createServer(async (nodeReq, nodeRes) => {
// Check if this is a WebSocket upgrade request
const isUpgrade = nodeReq.headers.upgrade?.toLowerCase() === 'websocket'
if (isUpgrade) {
// In a real implementation, you'd:
// 1. Perform WebSocket handshake
// 2. Switch protocols
// 3. Forward socket data to Python
// For this test, we simulate by creating a WebSocket request
const req = new Request({
method: nodeReq.method,
url: `http://${nodeReq.headers.host}${nodeReq.url}`,
headers: nodeReq.headers,
websocket: true
})
const res = await python.handleStream(req)
// Simulate sending a message through the WebSocket
await req.write('Integration test message')
// Read response
const chunk = await res.next()
const response = chunk.toString('utf8')
await req.end()
// Send response back through HTTP for test purposes
nodeRes.writeHead(200, { 'Content-Type': 'text/plain' })
nodeRes.end(response)
} else {
// Regular HTTP request
const req = new Request({
method: nodeReq.method,
url: `http://${nodeReq.headers.host}${nodeReq.url}`,
headers: nodeReq.headers,
websocket: false
})
const res = await python.handleRequest(req)
nodeRes.writeHead(res.status, Object.fromEntries(res.headers.entries()))
nodeRes.end(res.body)
}
})
server.listen(0)
await once(server, 'listening')
const { port } = server.address()
try {
// Test WebSocket upgrade request using http.request (fetch doesn't support upgrade headers)
const upgradeResponse = await new Promise((resolve, reject) => {
const req = httpRequest({
hostname: 'localhost',
port,
path: '/echo',
method: 'GET',
headers: {
'Upgrade': 'websocket',
'Connection': 'Upgrade',
'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',
'Sec-WebSocket-Version': '13'
}
}, (res) => {
let data = ''
res.on('data', chunk => { data += chunk })
res.on('end', () => {
resolve({ status: res.statusCode, body: data })
})
})
req.on('error', reject)
req.end()
})
strictEqual(upgradeResponse.body, 'Integration test message', 'Should echo the message through WebSocket')
// Test regular HTTP request (should get 426 Upgrade Required)
const httpResponse = await fetch(`http://localhost:${port}/echo`)
strictEqual(httpResponse.status, 426, 'Should require upgrade for WebSocket app')
const body = await httpResponse.text()
strictEqual(body, 'Upgrade Required')
} finally {
server.close()
}
})
await t.test('Bidirectional communication simulation', async () => {
const python = new Python({
docroot: fixturesDir,
appTarget: 'websocket_app:app'
})
// Simulate a bidirectional WebSocket connection
const req = new Request({
method: 'GET',
url: 'http://example.com/echo',
websocket: true
})
const res = await python.handleStream(req)
// Simulate multiple back-and-forth messages
const messages = [
{ send: 'Message 1', expect: 'Message 1' },
{ send: 'Message 2', expect: 'Message 2' },
{ send: 'Message 3', expect: 'Message 3' }
]
for (const { send, expect } of messages) {
// Client sends message
await req.write(send)
// Server responds
const chunk = await res.next()
strictEqual(chunk.toString('utf8'), expect)
}
await req.end()
})
await t.test('Concurrent WebSocket connections', async () => {
const python = new Python({
docroot: fixturesDir,
appTarget: 'websocket_app:app'
})
// Create multiple concurrent WebSocket connections
const connections = []
const numConnections = 10
for (let i = 0; i < numConnections; i++) {
const req = new Request({
method: 'GET',
url: 'http://example.com/echo',
websocket: true
})
const res = python.handleStream(req)
connections.push({ req, res, id: i })
}
// Wait for all connections to establish
await Promise.all(connections.map(c => c.res))
// Send messages on all connections concurrently
const sendPromises = connections.map(async ({ req, res, id }) => {
const message = `Connection ${id}`
await req.write(message)
const awaitedRes = await res
const chunk = await awaitedRes.next()
strictEqual(chunk.toString('utf8'), message)
await req.end()
})
await Promise.all(sendPromises)
})
await t.test('WebSocket with large message payload', async () => {
const python = new Python({
docroot: fixturesDir,
appTarget: 'websocket_app:app'
})
const req = new Request({
method: 'GET',
url: 'http://example.com/echo',
websocket: true
})
const res = await python.handleStream(req)
// Send a large message (1MB)
const largeMessage = 'x'.repeat(1024 * 1024)
await req.write(largeMessage)
// Receive and verify
const chunk = await res.next()
strictEqual(chunk.toString('utf8').length, largeMessage.length)
strictEqual(chunk.toString('utf8'), largeMessage)
await req.end()
})
await t.test('WebSocket message buffering', async () => {
const python = new Python({
docroot: fixturesDir,
appTarget: 'websocket_app:app'
})
const req = new Request({
method: 'GET',
url: 'http://example.com/echo',
websocket: true
})
const res = await python.handleStream(req)
// Send multiple messages quickly without waiting for responses
const messages = ['fast1', 'fast2', 'fast3', 'fast4', 'fast5']
for (const msg of messages) {
await req.write(msg)
}
// Now read all responses
for (const msg of messages) {
const chunk = await res.next()
strictEqual(chunk.toString('utf8'), msg)
}
await req.end()
})
})