@@ -596,10 +596,7 @@ public void ProcessorBlobContainerNameDefaultsCorrectly()
596596 new KeyValuePair < string , string ? > ( "Aspire:Azure:Messaging:EventHubs:EventProcessorClient:EventHubName" , "MyHub" ) ,
597597 ] ) ;
598598
599- var mockTransport = new MockTransport (
600- CreateResponse ( """{}""" ) ) ;
601- var blobClient = new BlobServiceClient ( new Uri ( BlobsConnectionString ) , new BlobClientOptions ( ) { Transport = mockTransport } ) ;
602- builder . Services . AddSingleton ( blobClient ) ;
599+ var mockTransport = InjectMockBlobClient ( builder ) ;
603600
604601 builder . AddAzureEventProcessorClient ( "eh1" ) ;
605602
@@ -613,6 +610,15 @@ public void ProcessorBlobContainerNameDefaultsCorrectly()
613610 Assert . Equal ( "https://fake.blob.core.windows.net/127-0-0-1-MyHub-default?restype=container" , mockTransport . Requests [ 0 ] . Uri . ToString ( ) ) ;
614611 }
615612
613+ private static MockTransport InjectMockBlobClient ( HostApplicationBuilder builder )
614+ {
615+ var mockTransport = new MockTransport (
616+ CreateResponse ( """{}""" ) ) ;
617+ var blobClient = new BlobServiceClient ( new Uri ( BlobsConnectionString ) , new BlobClientOptions ( ) { Transport = mockTransport } ) ;
618+ builder . Services . AddSingleton ( blobClient ) ;
619+ return mockTransport ;
620+ }
621+
616622 private static MockResponse CreateResponse ( string content )
617623 {
618624 var buffer = Encoding . UTF8 . GetBytes ( content ) ;
@@ -626,4 +632,137 @@ private static MockResponse CreateResponse(string content)
626632
627633 return response ;
628634 }
635+
636+ [ Theory ]
637+ [ MemberData ( nameof ( ConnectionString_MemberData ) ) ]
638+ public void AddAzureCosmosClient_EnsuresConnectionStringIsCorrect ( EventHubTestConnectionInfo testInfo )
639+ {
640+ var builder = Host . CreateEmptyApplicationBuilder ( null ) ;
641+
642+ builder . Configuration . AddInMemoryCollection ( [
643+ new KeyValuePair < string , string ? > ( "ConnectionStrings:eh1" , testInfo . TestConnectionString ) ,
644+ new KeyValuePair < string , string ? > ( "Aspire:Azure:Messaging:EventHubs:EventHubProducerClient:EventHubName" , "NotInConnectionInfo" ) ,
645+ new KeyValuePair < string , string ? > ( "Aspire:Azure:Messaging:EventHubs:EventHubConsumerClient:EventHubName" , "NotInConnectionInfo" ) ,
646+ new KeyValuePair < string , string ? > ( "Aspire:Azure:Messaging:EventHubs:EventProcessorClient:EventHubName" , "NotInConnectionInfo" ) ,
647+ new KeyValuePair < string , string ? > ( "Aspire:Azure:Messaging:EventHubs:PartitionReceiver:EventHubName" , "NotInConnectionInfo" ) ,
648+ new KeyValuePair < string , string ? > ( "Aspire:Azure:Messaging:EventHubs:PartitionReceiver:PartitionId" , "foo" ) ,
649+ new KeyValuePair < string , string ? > ( "Aspire:Azure:Messaging:EventHubs:EventHubBufferedProducerClient:EventHubName" , "NotInConnectionInfo" ) ,
650+ ] ) ;
651+
652+ var expectedEventHubName = testInfo . EventHubName ?? "NotInConnectionInfo" ;
653+
654+ var settingsCalled = 0 ;
655+ void VerifySettings ( AzureMessagingEventHubsSettings settings )
656+ {
657+ settingsCalled ++ ;
658+
659+ Assert . Equal ( testInfo . ConnectionString , settings . ConnectionString ) ;
660+ Assert . Equal ( testInfo . FullyQualifiedNamespace , settings . FullyQualifiedNamespace ) ;
661+ Assert . Equal ( expectedEventHubName , settings . EventHubName ) ;
662+
663+ var consumerGroupProperty = settings . GetType ( ) . GetProperty ( "ConsumerGroup" ) ;
664+ if ( consumerGroupProperty != null )
665+ {
666+ Assert . Equal ( testInfo . ConsumerGroup , consumerGroupProperty . GetValue ( settings ) ) ;
667+ }
668+ }
669+
670+ InjectMockBlobClient ( builder ) ;
671+
672+ builder . AddAzureEventHubProducerClient ( "eh1" , VerifySettings ) ;
673+ builder . AddAzureEventHubConsumerClient ( "eh1" , VerifySettings ) ;
674+ builder . AddAzureEventProcessorClient ( "eh1" , VerifySettings ) ;
675+ builder . AddAzurePartitionReceiverClient ( "eh1" , VerifySettings ) ;
676+ builder . AddAzureEventHubBufferedProducerClient ( "eh1" , VerifySettings ) ;
677+
678+ Assert . Equal ( 5 , settingsCalled ) ;
679+
680+ using var app = builder . Build ( ) ;
681+
682+ var producerClient = app . Services . GetRequiredService < EventHubProducerClient > ( ) ;
683+ Assert . Equal ( testInfo . ClientFullyQualifiedNamespace , producerClient . FullyQualifiedNamespace ) ;
684+ Assert . Equal ( expectedEventHubName , producerClient . EventHubName ) ;
685+
686+ var consumerClient = app . Services . GetRequiredService < EventHubConsumerClient > ( ) ;
687+ Assert . Equal ( testInfo . ClientFullyQualifiedNamespace , consumerClient . FullyQualifiedNamespace ) ;
688+ Assert . Equal ( expectedEventHubName , consumerClient . EventHubName ) ;
689+
690+ var processorClient = app . Services . GetRequiredService < EventProcessorClient > ( ) ;
691+ Assert . Equal ( testInfo . ClientFullyQualifiedNamespace , processorClient . FullyQualifiedNamespace ) ;
692+ Assert . Equal ( expectedEventHubName , processorClient . EventHubName ) ;
693+
694+ var partitionReceiver = app . Services . GetRequiredService < PartitionReceiver > ( ) ;
695+ Assert . Equal ( testInfo . ClientFullyQualifiedNamespace , partitionReceiver . FullyQualifiedNamespace ) ;
696+ Assert . Equal ( expectedEventHubName , partitionReceiver . EventHubName ) ;
697+
698+ var bufferedProducerClient = app . Services . GetRequiredService < EventHubBufferedProducerClient > ( ) ;
699+ Assert . Equal ( testInfo . ClientFullyQualifiedNamespace , bufferedProducerClient . FullyQualifiedNamespace ) ;
700+ Assert . Equal ( expectedEventHubName , bufferedProducerClient . EventHubName ) ;
701+ }
702+
703+ public static TheoryData < EventHubTestConnectionInfo > ConnectionString_MemberData ( )
704+ {
705+ return new ( )
706+ {
707+ new EventHubTestConnectionInfo ( )
708+ {
709+ TestConnectionString = "Endpoint=sb://localhost:55184;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;" ,
710+ ConnectionString = "Endpoint=sb://localhost:55184;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;" ,
711+ ClientFullyQualifiedNamespace = "localhost"
712+ } ,
713+ new EventHubTestConnectionInfo ( )
714+ {
715+ TestConnectionString = "Endpoint=sb://localhost:55184;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;EntityPath=myhub" ,
716+ ConnectionString = "Endpoint=sb://localhost:55184;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;EntityPath=myhub" ,
717+ EventHubName = "myhub" ,
718+ ClientFullyQualifiedNamespace = "localhost"
719+ } ,
720+ new EventHubTestConnectionInfo ( )
721+ {
722+ TestConnectionString = "Endpoint=sb://localhost:55184;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;ConsumerGroup=mygroup;EntityPath=myhub" ,
723+ ConnectionString = "Endpoint=sb://localhost:55184;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;ConsumerGroup=mygroup;EntityPath=myhub" ,
724+ EventHubName = "myhub" ,
725+ ConsumerGroup = "mygroup" ,
726+ ClientFullyQualifiedNamespace = "localhost"
727+ } ,
728+ new EventHubTestConnectionInfo ( )
729+ {
730+ TestConnectionString = "Endpoint=https://eventhubns-cetg3lr.servicebus.windows.net:443/;EntityPath=myhub;ConsumerGroup=mygroup" ,
731+ FullyQualifiedNamespace = "https://eventhubns-cetg3lr.servicebus.windows.net:443/" ,
732+ EventHubName = "myhub" ,
733+ ConsumerGroup = "mygroup" ,
734+ ClientFullyQualifiedNamespace = "eventhubns-cetg3lr.servicebus.windows.net"
735+ } ,
736+ new EventHubTestConnectionInfo ( )
737+ {
738+ TestConnectionString = "Endpoint=https://eventhubns-cetg3lr.servicebus.windows.net:443/;EntityPath=myhub" ,
739+ FullyQualifiedNamespace = "https://eventhubns-cetg3lr.servicebus.windows.net:443/" ,
740+ EventHubName = "myhub" ,
741+ ClientFullyQualifiedNamespace = "eventhubns-cetg3lr.servicebus.windows.net"
742+ } ,
743+ new EventHubTestConnectionInfo ( )
744+ {
745+ TestConnectionString = "Endpoint=https://eventhubns-cetg3lr.servicebus.windows.net:443/;ConsumerGroup=mygroup" ,
746+ FullyQualifiedNamespace = "https://eventhubns-cetg3lr.servicebus.windows.net:443/" ,
747+ ConsumerGroup = "mygroup" ,
748+ ClientFullyQualifiedNamespace = "eventhubns-cetg3lr.servicebus.windows.net"
749+ } ,
750+ new EventHubTestConnectionInfo ( )
751+ {
752+ TestConnectionString = "https://eventhubns-cetg3lr.servicebus.windows.net:443/" ,
753+ FullyQualifiedNamespace = "https://eventhubns-cetg3lr.servicebus.windows.net:443/" ,
754+ ClientFullyQualifiedNamespace = "eventhubns-cetg3lr.servicebus.windows.net"
755+ }
756+ } ;
757+ }
758+
759+ public record EventHubTestConnectionInfo
760+ {
761+ public required string TestConnectionString { get ; set ; }
762+ public string ? FullyQualifiedNamespace { get ; set ; }
763+ public string ? ConnectionString { get ; set ; }
764+ public string ? EventHubName { get ; set ; }
765+ public string ? ConsumerGroup { get ; set ; }
766+ public string ? ClientFullyQualifiedNamespace { get ; set ; }
767+ }
629768}
0 commit comments