-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: properly detect requests without body
Related: socketio/socket.io#4786
- Loading branch information
1 parent
e7f0375
commit ac7bb8e
Showing
3 changed files
with
78 additions
and
9 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,71 @@ | ||
const cluster = require("cluster"); | ||
const { createServer, request } = require("http"); | ||
const { Server } = require("socket.io"); | ||
const { setupMaster, setupWorker } = require("../.."); | ||
const assert = require("assert").strict; | ||
|
||
if (cluster.isWorker) { | ||
const httpServer = createServer(); | ||
const io = new Server(httpServer, { | ||
cors: { | ||
origin: true, | ||
}, | ||
}); | ||
setupWorker(io); | ||
|
||
io.on("connection", (socket) => { | ||
socket.on("foo", (val) => { | ||
socket.emit("bar", val); | ||
}); | ||
}); | ||
|
||
return; | ||
} | ||
|
||
const WORKER_COUNT = 3; | ||
|
||
for (let i = 0; i < WORKER_COUNT; i++) { | ||
cluster.fork(); | ||
} | ||
|
||
const httpServer = createServer(); | ||
|
||
setupMaster(httpServer, { | ||
loadBalancingMethod: process.env.LB_METHOD || "least-connection", | ||
}); | ||
|
||
const waitFor = (emitter, event) => { | ||
return new Promise((resolve) => { | ||
emitter.once(event, resolve); | ||
}); | ||
}; | ||
|
||
httpServer.listen(async () => { | ||
const port = httpServer.address().port; | ||
|
||
const res = await sendRequest({ | ||
port, | ||
method: "options", | ||
path: "/socket.io/", | ||
headers: { | ||
origin: "https://example.com", | ||
}, | ||
}); | ||
|
||
assert.equal(res.statusCode, 204); | ||
|
||
assert.equal( | ||
res.headers["access-control-allow-origin"], | ||
"https://example.com" | ||
); | ||
|
||
// cleanup | ||
for (const id in cluster.workers) { | ||
cluster.workers[id].kill(); | ||
} | ||
httpServer.close(); | ||
}); | ||
|
||
function sendRequest(options) { | ||
return new Promise((resolve) => request(options, resolve).end()); | ||
} |
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