@@ -110,42 +110,48 @@ spec:
110110 }
111111
112112 Context ("Consumer plugins" , func () {
113- var keyAuthConsumer = `apiVersion: gateway.apisix.io/v1alpha1
113+ var limitCountConsumer = `
114+ apiVersion: gateway.apisix.io/v1alpha1
114115kind: Consumer
115116metadata:
116117 name: consumer-sample
117118spec:
118119 gatewayRef:
119120 name: api7ee
120- plugins:
121- - name: key-auth
121+ credentials:
122+ - type: key-auth
123+ name: key-auth-sample
122124 config:
123125 key: sample-key
126+ plugins:
127+ - name: limit-count
128+ config:
129+ count: 2
130+ time_window: 60
131+ rejected_code: 503
132+ key: remote_addr
124133`
125- var basicAuthConsumer = `apiVersion: gateway.apisix.io/v1alpha1
134+
135+ var unlimitConsumer = `
136+ apiVersion: gateway.apisix.io/v1alpha1
126137kind: Consumer
127138metadata:
128- name: consumer-sample
139+ name: consumer-sample2
129140spec:
130141 gatewayRef:
131142 name: api7ee
132- plugins:
133- - name: basic-auth
143+ credentials:
144+ - type: key-auth
145+ name: key-auth-sample
134146 config:
135- username: sample-user
136- password: sample-password
147+ key: sample-key2
137148`
138149
139150 BeforeEach (beforeEachHTTP )
140151
141- It ("key-auth" , func () {
142- s .ResourceApplied ("Consumer" , "consumer-sample" , keyAuthConsumer , 1 )
143-
144- s .NewAPISIXClient ().
145- GET ("/get" ).
146- WithHost ("httpbin.org" ).
147- Expect ().
148- Status (401 )
152+ It ("limit-count plugin" , func () {
153+ s .ResourceApplied ("Consumer" , "consumer-sample" , limitCountConsumer , 1 )
154+ s .ResourceApplied ("Consumer" , "consumer-sample2" , unlimitConsumer , 1 )
149155
150156 s .NewAPISIXClient ().
151157 GET ("/get" ).
@@ -154,46 +160,29 @@ spec:
154160 Expect ().
155161 Status (200 )
156162
157- By ("delete Consumer" )
158- err := s .DeleteResourceFromString (keyAuthConsumer )
159- Expect (err ).NotTo (HaveOccurred (), "deleting Consumer" )
160- time .Sleep (5 * time .Second )
161-
162163 s .NewAPISIXClient ().
163164 GET ("/get" ).
164165 WithHeader ("apikey" , "sample-key" ).
165166 WithHost ("httpbin.org" ).
166167 Expect ().
167- Status (401 )
168- })
169-
170- It ("basic-auth" , func () {
171- s .ResourceApplied ("Consumer" , "consumer-sample" , basicAuthConsumer , 1 )
172-
173- s .NewAPISIXClient ().
174- GET ("/get" ).
175- WithHost ("httpbin.org" ).
176- Expect ().
177- Status (401 )
178-
179- s .NewAPISIXClient ().
180- GET ("/get" ).
181- WithBasicAuth ("sample-user" , "sample-password" ).
182- WithHost ("httpbin.org" ).
183- Expect ().
184168 Status (200 )
185169
186- By ("delete Consumer" )
187- err := s .DeleteResourceFromString (basicAuthConsumer )
188- Expect (err ).NotTo (HaveOccurred (), "deleting Consumer" )
189- time .Sleep (5 * time .Second )
190-
170+ By ("trigger limit-count" )
191171 s .NewAPISIXClient ().
192172 GET ("/get" ).
193- WithBasicAuth ( "sample-user " , "sample-password " ).
173+ WithHeader ( "apikey " , "sample-key " ).
194174 WithHost ("httpbin.org" ).
195175 Expect ().
196- Status (401 )
176+ Status (503 )
177+
178+ for i := 0 ; i < 10 ; i ++ {
179+ s .NewAPISIXClient ().
180+ GET ("/get" ).
181+ WithHeader ("apikey" , "sample-key2" ).
182+ WithHost ("httpbin.org" ).
183+ Expect ().
184+ Status (200 )
185+ }
197186 })
198187 })
199188
@@ -309,6 +298,87 @@ spec:
309298 })
310299 })
311300
312- PContext ("SecretRef" , func () {
301+ Context ("SecretRef" , func () {
302+ var keyAuthSecret = `
303+ apiVersion: v1
304+ kind: Secret
305+ metadata:
306+ name: key-auth-secret
307+ data:
308+ key: c2FtcGxlLWtleQ==
309+ `
310+ var basicAuthSecret = `
311+ apiVersion: v1
312+ kind: Secret
313+ metadata:
314+ name: basic-auth-secret
315+ data:
316+ username: c2FtcGxlLXVzZXI=
317+ password: c2FtcGxlLXBhc3N3b3Jk
318+ `
319+ var defaultConsumer = `
320+ apiVersion: gateway.apisix.io/v1alpha1
321+ kind: Consumer
322+ metadata:
323+ name: consumer-sample
324+ spec:
325+ gatewayRef:
326+ name: api7ee
327+ credentials:
328+ - type: basic-auth
329+ name: basic-auth-sample
330+ secretRef:
331+ name: basic-auth-secret
332+ - type: key-auth
333+ name: key-auth-sample
334+ secretRef:
335+ name: key-auth-secret
336+ - type: key-auth
337+ name: key-auth-sample2
338+ config:
339+ key: sample-key2
340+ `
341+ BeforeEach (beforeEachHTTP )
342+
343+ It ("Create/Update/Delete" , func () {
344+ err := s .CreateResourceFromString (keyAuthSecret )
345+ Expect (err ).NotTo (HaveOccurred (), "creating key-auth secret" )
346+ err = s .CreateResourceFromString (basicAuthSecret )
347+ Expect (err ).NotTo (HaveOccurred (), "creating basic-auth secret" )
348+ s .ResourceApplied ("Consumer" , "consumer-sample" , defaultConsumer , 1 )
349+
350+ s .NewAPISIXClient ().
351+ GET ("/get" ).
352+ WithHeader ("apikey" , "sample-key" ).
353+ WithHost ("httpbin.org" ).
354+ Expect ().
355+ Status (200 )
356+
357+ s .NewAPISIXClient ().
358+ GET ("/get" ).
359+ WithBasicAuth ("sample-user" , "sample-password" ).
360+ WithHost ("httpbin.org" ).
361+ Expect ().
362+ Status (200 )
363+
364+ By ("delete consumer" )
365+ err = s .DeleteResourceFromString (defaultConsumer )
366+ Expect (err ).NotTo (HaveOccurred (), "deleting consumer" )
367+ time .Sleep (5 * time .Second )
368+
369+ s .NewAPISIXClient ().
370+ GET ("/get" ).
371+ WithHeader ("apikey" , "sample-key" ).
372+ WithHost ("httpbin.org" ).
373+ Expect ().
374+ Status (401 )
375+
376+ s .NewAPISIXClient ().
377+ GET ("/get" ).
378+ WithBasicAuth ("sample-user" , "sample-password" ).
379+ WithHost ("httpbin.org" ).
380+ Expect ().
381+ Status (401 )
382+ })
313383 })
314384})
0 commit comments