diff --git a/sdk/eventhub/event-hubs/src/eventHubClient.ts b/sdk/eventhub/event-hubs/src/eventHubClient.ts index 1934ded87eba..96979644f2cf 100644 --- a/sdk/eventhub/event-hubs/src/eventHubClient.ts +++ b/sdk/eventhub/event-hubs/src/eventHubClient.ts @@ -11,7 +11,9 @@ import { ConnectionConfig, isTokenCredential, RetryOptions, - Constants + Constants, + parseConnectionString, + EventHubConnectionStringModel } from "@azure/core-amqp"; import { ConnectionContext } from "./connectionContext"; @@ -285,6 +287,29 @@ export class EventHubClient { hostOrConnectionString = String(hostOrConnectionString); if (!isTokenCredential(credentialOrOptions)) { + const parsedCS = parseConnectionString(hostOrConnectionString); + if ( + !( + parsedCS.EntityPath || + (typeof eventHubNameOrOptions === "string" && eventHubNameOrOptions) + ) + ) { + throw new TypeError( + `Either provide "eventHubName" or the "connectionString": "${hostOrConnectionString}", ` + + `must contain "EntityPath=".` + ); + } + if ( + parsedCS.EntityPath && + typeof eventHubNameOrOptions === "string" && + eventHubNameOrOptions && + parsedCS.EntityPath !== eventHubNameOrOptions + ) { + throw new TypeError( + `The entity path "${parsedCS.EntityPath}" in connectionString: "${hostOrConnectionString}" ` + + `doesn't match with eventHubName: "${eventHubNameOrOptions}".` + ); + } connectionString = hostOrConnectionString; if (typeof eventHubNameOrOptions !== "string") { // connectionstring and/or options were passed to constructor @@ -303,6 +328,9 @@ export class EventHubClient { const eventHubName = eventHubNameOrOptions; let host = hostOrConnectionString; credential = credentialOrOptions; + if (!eventHubName) { + throw new TypeError(`"eventHubName" is missing`); + } if (!host.endsWith("/")) host += "/"; connectionString = `Endpoint=sb://${host};SharedAccessKeyName=defaultKeyName;SharedAccessKey=defaultKeyValue;EntityPath=${eventHubName}`; @@ -396,7 +424,7 @@ export class EventHubClient { * - `ownerLevel` : A number indicating that the consumer intends to be an exclusive consumer of events resulting in other * consumers to fail if their `ownerLevel` is lower or doesn't exist. * - `retryOptions`: The retry options used to govern retry attempts when an issue is encountered while receiving events. - * + * * @throws {Error} Thrown if the underlying connection has been closed, create a new EventHubClient. * @throws {TypeError} Thrown if a required parameter is missing. */ diff --git a/sdk/eventhub/event-hubs/test/client.spec.ts b/sdk/eventhub/event-hubs/test/client.spec.ts index 6bb692350019..0b4e8dbc9be6 100644 --- a/sdk/eventhub/event-hubs/test/client.spec.ts +++ b/sdk/eventhub/event-hubs/test/client.spec.ts @@ -23,33 +23,47 @@ import { EnvironmentCredential } from "@azure/identity"; const env = getEnvVars(); describe("Create EventHubClient #RunnableInBrowser", function(): void { - it("throws when it cannot find the Event Hub path", function(): void { + it("throws when it cannot find the Event Hub name", function(): void { const connectionString = "Endpoint=sb://abc"; const test = function(): EventHubClient { return new EventHubClient(connectionString); }; test.should.throw( Error, - `Either provide "path" or the "connectionString": "${connectionString}", ` + - `must contain EntityPath="".` + `Either provide "eventHubName" or the "connectionString": "${connectionString}", ` + + `must contain "EntityPath=".` + ); + }); + + it("throws when EntityPath in Connection string doesn't match with event hub name parameter", function(): void { + const connectionString = + "Endpoint=sb://a;SharedAccessKeyName=b;SharedAccessKey=c=;EntityPath=my-event-hub-name"; + const eventHubName = "event-hub-name"; + const test = function(): EventHubClient { + return new EventHubClient(connectionString, eventHubName); + }; + test.should.throw( + Error, + `The entity path "my-event-hub-name" in connectionString: "${connectionString}" ` + + `doesn't match with eventHubName: "${eventHubName}".` ); }); it("creates an EventHubClient from a connection string", function(): void { const client = new EventHubClient( - "Endpoint=sb://a;SharedAccessKeyName=b;SharedAccessKey=c;EntityPath=my-event-hub-path" + "Endpoint=sb://a;SharedAccessKeyName=b;SharedAccessKey=c;EntityPath=my-event-hub-name" ); client.should.be.an.instanceof(EventHubClient); - should.equal(client.eventHubName, "my-event-hub-path"); + should.equal(client.eventHubName, "my-event-hub-name"); }); - it("creates an EventHubClient from a connection string and an Event Hub path", function(): void { + it("creates an EventHubClient from a connection string and an Event Hub name", function(): void { const client = new EventHubClient( "Endpoint=sb://a;SharedAccessKeyName=b;SharedAccessKey=c", - "my-event-hub-path" + "my-event-hub-name" ); client.should.be.an.instanceof(EventHubClient); - should.equal(client.eventHubName, "my-event-hub-path"); + should.equal(client.eventHubName, "my-event-hub-name"); }); it("creates an EventHubClient from a custom TokenCredential", function(): void { @@ -61,9 +75,9 @@ describe("Create EventHubClient #RunnableInBrowser", function(): void { }; } }; - const client = new EventHubClient("abc", "my-event-hub-path", dummyCredential); + const client = new EventHubClient("abc","my-event-hub-name", dummyCredential); client.should.be.an.instanceof(EventHubClient); - should.equal(client.eventHubName, "my-event-hub-path"); + should.equal(client.eventHubName, "my-event-hub-name"); }); it("creates an EventHubClient from an Azure.Identity credential", async function(): Promise<