Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update connect event logic #18568

Merged
2 commits merged into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions sdk/web-pubsub/web-pubsub-express/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ npm install @azure/web-pubsub-express
const express = require("express");

const { WebPubSubEventHandler } = require("@azure/web-pubsub-express");
const handler = new WebPubSubEventHandler("chat", {
handleConnect: (req, res) => {
// auth the connection and set the userId of the connection
res.success();
}
});
const handler = new WebPubSubEventHandler("chat");

const app = express();

Expand Down Expand Up @@ -114,9 +109,6 @@ const express = require("express");

const { WebPubSubEventHandler } = require("@azure/web-pubsub-express");
const handler = new WebPubSubEventHandler("chat", {
handleConnect: (req, res) => {
res.success();
},
allowedEndpoints: [
"https://<yourAllowedService1>.webpubsub.azure.com",
"https://<yourAllowedService2>.webpubsub.azure.com"
Expand All @@ -139,18 +131,15 @@ const express = require("express");

const { WebPubSubEventHandler } = require("@azure/web-pubsub-express");
const handler = new WebPubSubEventHandler("chat", {
path: "customPath1",
handleConnect: (req, res) => {
// auth the connection and set the userId of the connection
res.success();
}
path: "customPath1"
});

const app = express();

app.use(handler.getMiddleware());

app.listen(3000, () =>
// Azure WebPubSub Upstream ready at http://localhost:3000/customPath1
console.log(`Azure WebPubSub Upstream ready at http://localhost:3000${handler.path}`)
);
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const { WebPubSubEventHandler } = require("@azure/web-pubsub-express");
const express = require("express");

const handler = new WebPubSubEventHandler("chat", {
path: "/api/webpubsub",
handleConnect(req, res) {
console.log(req);
res.success();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ export class CloudEventsDispatcher {
switch (eventType) {
case EventType.Connect:
if (!this.eventHandler?.handleConnect) {
response.statusCode = 401;
response.end("Connect event handler is not configured.");
response.end();
return true;
}
break;
Expand Down
8 changes: 4 additions & 4 deletions sdk/web-pubsub/web-pubsub-express/test/connect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,26 @@ describe("Can handle connect event", function() {
assert.isTrue(endSpy.notCalled);
});

it("Should response with 401 when option is not specified", async function() {
it("Should response with 200 when option is not specified", async function() {
const endSpy = sinon.spy(res, "end");
buildRequest(req, "hub", "conn1");

const dispatcher = new CloudEventsDispatcher("hub");
const result = await dispatcher.handleRequest(req, res);
assert.isTrue(result, "should handle");
assert.isTrue(endSpy.calledOnce, "should call once");
assert.equal(401, res.statusCode, "should be 401");
assert.equal(200, res.statusCode, "should be 200");
});

it("Should response with 401 when handler is not specified", async function() {
it("Should response with 200 when handler is not specified", async function() {
const endSpy = sinon.spy(res, "end");
buildRequest(req, "hub", "conn1");

const dispatcher = new CloudEventsDispatcher("hub", {});
const result = await dispatcher.handleRequest(req, res);
assert.isTrue(result, "should handle");
assert.isTrue(endSpy.calledOnce, "should call once");
assert.equal(401, res.statusCode, "should be 401");
assert.equal(200, res.statusCode, "should be 200");
});

it("Should response with error when handler returns error", async function() {
Expand Down