Skip to content

Commit

Permalink
Update connect event logic (#18568)
Browse files Browse the repository at this point in the history
Since now the service requires explicit "AllowAnonymous" set, there is no need for the SDK side to 401 connect request
  • Loading branch information
vicancy authored Nov 8, 2021
1 parent 746bb4f commit bd6989f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 21 deletions.
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

0 comments on commit bd6989f

Please sign in to comment.