@@ -707,6 +707,85 @@ describe('tests', () => {
707
707
} ) ;
708
708
} ) ;
709
709
710
+ test ( 'Can add actions to an imported listener' , ( ) => {
711
+ // GIVEN
712
+ const stack = new cdk . Stack ( ) ;
713
+ const stack2 = new cdk . Stack ( ) ;
714
+ const vpc = new ec2 . Vpc ( stack , 'VPC' ) ;
715
+ const lb = new elbv2 . ApplicationLoadBalancer ( stack , 'LoadBalancer' , {
716
+ vpc,
717
+ } ) ;
718
+ const listener = lb . addListener ( 'Listener' , {
719
+ port : 80 ,
720
+ } ) ;
721
+
722
+ // WHEN
723
+ listener . addAction ( 'Default' , {
724
+ action : elbv2 . ListenerAction . fixedResponse ( 404 , {
725
+ contentType : 'text/plain' ,
726
+ messageBody : 'Not Found' ,
727
+ } ) ,
728
+ } ) ;
729
+
730
+ const importedListener = elbv2 . ApplicationListener . fromApplicationListenerAttributes ( stack2 , 'listener' , {
731
+ listenerArn : 'listener-arn' ,
732
+ defaultPort : 443 ,
733
+ securityGroup : ec2 . SecurityGroup . fromSecurityGroupId ( stack2 , 'SG' , 'security-group-id' , {
734
+ allowAllOutbound : false ,
735
+ } ) ,
736
+ } ) ;
737
+ importedListener . addAction ( 'Hello' , {
738
+ action : elbv2 . ListenerAction . fixedResponse ( 503 ) ,
739
+ conditions : [ elbv2 . ListenerCondition . pathPatterns ( [ '/hello' ] ) ] ,
740
+ priority : 10 ,
741
+ } ) ;
742
+
743
+ // THEN
744
+ Template . fromStack ( stack ) . hasResourceProperties ( 'AWS::ElasticLoadBalancingV2::Listener' , {
745
+ DefaultActions : [
746
+ {
747
+ FixedResponseConfig : {
748
+ ContentType : 'text/plain' ,
749
+ MessageBody : 'Not Found' ,
750
+ StatusCode : '404' ,
751
+ } ,
752
+ Type : 'fixed-response' ,
753
+ } ,
754
+ ] ,
755
+ } ) ;
756
+
757
+ Template . fromStack ( stack2 ) . hasResourceProperties ( 'AWS::ElasticLoadBalancingV2::ListenerRule' , {
758
+ ListenerArn : 'listener-arn' ,
759
+ Priority : 10 ,
760
+ Actions : [
761
+ {
762
+ FixedResponseConfig : {
763
+ StatusCode : '503' ,
764
+ } ,
765
+ Type : 'fixed-response' ,
766
+ } ,
767
+ ] ,
768
+ } ) ;
769
+ } ) ;
770
+
771
+ test ( 'actions added to an imported listener must have a priority' , ( ) => {
772
+ // GIVEN
773
+ const stack = new cdk . Stack ( ) ;
774
+
775
+ const importedListener = elbv2 . ApplicationListener . fromApplicationListenerAttributes ( stack , 'listener' , {
776
+ listenerArn : 'listener-arn' ,
777
+ defaultPort : 443 ,
778
+ securityGroup : ec2 . SecurityGroup . fromSecurityGroupId ( stack , 'SG' , 'security-group-id' , {
779
+ allowAllOutbound : false ,
780
+ } ) ,
781
+ } ) ;
782
+ expect ( ( ) => {
783
+ importedListener . addAction ( 'Hello' , {
784
+ action : elbv2 . ListenerAction . fixedResponse ( 503 ) ,
785
+ } ) ;
786
+ } ) . toThrow ( / p r i o r i t y m u s t b e s e t f o r a c t i o n s a d d e d t o a n i m p o r t e d l i s t e n e r / ) ;
787
+ } ) ;
788
+
710
789
testDeprecated ( 'Can add redirect responses' , ( ) => {
711
790
// GIVEN
712
791
const stack = new cdk . Stack ( ) ;
0 commit comments