@@ -30,6 +30,8 @@ describe('#validate()', () => {
30
30
conditions : {
31
31
host : 'example.com' ,
32
32
path : '/hello' ,
33
+ method : 'GET' ,
34
+ ip : [ '192.168.0.1/1' , 'fe80:0000:0000:0000:0204:61ff:fe9d:f156/3' ] ,
33
35
} ,
34
36
} ,
35
37
} ,
@@ -45,6 +47,10 @@ describe('#validate()', () => {
45
47
priority : 2 ,
46
48
conditions : {
47
49
path : '/world' ,
50
+ method : [ 'POST' , 'GET' ] ,
51
+ query : {
52
+ foo : 'bar' ,
53
+ } ,
48
54
} ,
49
55
} ,
50
56
} ,
@@ -62,8 +68,10 @@ describe('#validate()', () => {
62
68
+ '50dc6c495c0c9188/f2f7dc8efc522ab2' ,
63
69
priority : 1 ,
64
70
conditions : {
65
- host : 'example.com' ,
66
- path : '/hello' ,
71
+ host : [ 'example.com' ] ,
72
+ path : [ '/hello' ] ,
73
+ method : [ 'GET' ] ,
74
+ ip : [ '192.168.0.1/1' , 'fe80:0000:0000:0000:0204:61ff:fe9d:f156/3' ] ,
67
75
} ,
68
76
} ,
69
77
{
@@ -73,9 +81,132 @@ describe('#validate()', () => {
73
81
+ '50dc6c495c0c9188/f2f7dc8efc522ab2' ,
74
82
priority : 2 ,
75
83
conditions : {
76
- path : '/world' ,
84
+ path : [ '/world' ] ,
85
+ method : [ 'POST' , 'GET' ] ,
86
+ query : {
87
+ foo : 'bar' ,
88
+ } ,
77
89
} ,
78
90
} ,
79
91
] ) ;
80
92
} ) ;
93
+
94
+ it ( 'should throw when given an invalid query condition' , ( ) => {
95
+ awsCompileAlbEvents . serverless . service . functions = {
96
+ first : {
97
+ events : [
98
+ {
99
+ alb : {
100
+ listenerArn : 'arn:aws:elasticloadbalancing:'
101
+ + 'us-east-1:123456789012:listener/app/my-load-balancer/'
102
+ + '50dc6c495c0c9188/f2f7dc8efc522ab2' ,
103
+ priority : 1 ,
104
+ conditions : {
105
+ path : '/hello' ,
106
+ query : 'ss' ,
107
+ } ,
108
+ } ,
109
+ } ,
110
+ ] ,
111
+ } ,
112
+ } ;
113
+
114
+ expect ( ( ) => awsCompileAlbEvents . validate ( ) ) . to . throw ( Error ) ;
115
+ } ) ;
116
+
117
+ it ( 'should throw when given an invalid ip condition' , ( ) => {
118
+ awsCompileAlbEvents . serverless . service . functions = {
119
+ first : {
120
+ events : [
121
+ {
122
+ alb : {
123
+ listenerArn : 'arn:aws:elasticloadbalancing:'
124
+ + 'us-east-1:123456789012:listener/app/my-load-balancer/'
125
+ + '50dc6c495c0c9188/f2f7dc8efc522ab2' ,
126
+ priority : 1 ,
127
+ conditions : {
128
+ path : '/hello' ,
129
+ ip : '1.1.1.1' ,
130
+ } ,
131
+ } ,
132
+ } ,
133
+ ] ,
134
+ } ,
135
+ } ;
136
+
137
+ expect ( ( ) => awsCompileAlbEvents . validate ( ) ) . to . throw ( Error ) ;
138
+ } ) ;
139
+
140
+ it ( 'should throw when given an invalid header condition' , ( ) => {
141
+ awsCompileAlbEvents . serverless . service . functions = {
142
+ first : {
143
+ events : [
144
+ {
145
+ alb : {
146
+ listenerArn : 'arn:aws:elasticloadbalancing:'
147
+ + 'us-east-1:123456789012:listener/app/my-load-balancer/'
148
+ + '50dc6c495c0c9188/f2f7dc8efc522ab2' ,
149
+ priority : 1 ,
150
+ conditions : {
151
+ path : '/hello' ,
152
+ header : [ 'foo' ] ,
153
+ } ,
154
+ } ,
155
+ } ,
156
+ ] ,
157
+ } ,
158
+ } ;
159
+
160
+ expect ( ( ) => awsCompileAlbEvents . validate ( ) ) . to . throw ( Error ) ;
161
+ } ) ;
162
+
163
+ describe ( '#validateIpCondition()' , ( ) => {
164
+ it ( 'should throw if ip is not a valid ipv6 or ipv4 cidr block' , ( ) => {
165
+ const event = { alb : { conditions : { ip : 'fe80:0000:0000:0000:0204:61ff:fe9d:f156/' } } } ;
166
+ expect ( ( ) => awsCompileAlbEvents . validateIpCondition ( event , '' ) ) . to . throw ( Error ) ;
167
+ } ) ;
168
+
169
+ it ( 'should return the value as array if it is a valid ipv6 cidr block' , ( ) => {
170
+ const event = { alb : { conditions : { ip : 'fe80:0000:0000:0000:0204:61ff:fe9d:f156/127' } } } ;
171
+ expect ( awsCompileAlbEvents . validateIpCondition ( event , '' ) )
172
+ . to . deep . equal ( [ 'fe80:0000:0000:0000:0204:61ff:fe9d:f156/127' ] ) ;
173
+ } ) ;
174
+
175
+ it ( 'should return the value as array if it is a valid ipv4 cidr block' , ( ) => {
176
+ const event = { alb : { conditions : { ip : '192.168.0.1/21' } } } ;
177
+ expect ( awsCompileAlbEvents . validateIpCondition ( event , '' ) )
178
+ . to . deep . equal ( [ '192.168.0.1/21' ] ) ;
179
+ } ) ;
180
+ } ) ;
181
+
182
+ describe ( '#validateQueryCondition()' , ( ) => {
183
+ it ( 'should throw if query is not an object' , ( ) => {
184
+ const event = { alb : { conditions : { query : 'foo' } } } ;
185
+ expect ( ( ) => awsCompileAlbEvents . validateQueryCondition ( event , '' ) ) . to . throw ( Error ) ;
186
+ } ) ;
187
+
188
+ it ( 'should return the value if it is an object' , ( ) => {
189
+ const event = { alb : { conditions : { query : { foo : 'bar' } } } } ;
190
+ expect ( awsCompileAlbEvents . validateQueryCondition ( event , '' ) )
191
+ . to . deep . equal ( { foo : 'bar' } ) ;
192
+ } ) ;
193
+ } ) ;
194
+
195
+ describe ( '#validateHeaderCondition()' , ( ) => {
196
+ it ( 'should throw if header does not have the required properties' , ( ) => {
197
+ const event = { alb : { conditions : { header : { name : 'foo' , value : 'bar' } } } } ;
198
+ expect ( ( ) => awsCompileAlbEvents . validateHeaderCondition ( event , '' ) ) . to . throw ( Error ) ;
199
+ } ) ;
200
+
201
+ it ( 'should throw if header.values is not an array' , ( ) => {
202
+ const event = { alb : { conditions : { header : { name : 'foo' , values : 'bar' } } } } ;
203
+ expect ( ( ) => awsCompileAlbEvents . validateHeaderCondition ( event , '' ) ) . to . throw ( Error ) ;
204
+ } ) ;
205
+
206
+ it ( 'should return the value if it is valid' , ( ) => {
207
+ const event = { alb : { conditions : { header : { name : 'foo' , values : [ 'bar' ] } } } } ;
208
+ expect ( awsCompileAlbEvents . validateHeaderCondition ( event , '' ) )
209
+ . to . deep . equal ( { name : 'foo' , values : [ 'bar' ] } ) ;
210
+ } ) ;
211
+ } ) ;
81
212
} ) ;
0 commit comments