1
1
const { Apigw } = require ( '../src' ) ;
2
2
3
+ const deepClone = ( obj ) => {
4
+ return JSON . parse ( JSON . stringify ( obj ) ) ;
5
+ } ;
6
+
3
7
describe ( 'apigw' , ( ) => {
4
8
const credentials = {
5
9
SecretId : process . env . TENCENT_SECRET_ID ,
@@ -25,6 +29,15 @@ describe('apigw', () => {
25
29
// protocols: ['http', 'https'],
26
30
// },
27
31
// ],
32
+ usagePlan : {
33
+ usagePlanId : 'usagePlan-8bbr8pup' ,
34
+ usagePlanName : 'slscmp' ,
35
+ usagePlanDesc : 'sls create' ,
36
+ maxRequestNum : 1000 ,
37
+ } ,
38
+ auth : {
39
+ secretName : 'authName' ,
40
+ } ,
28
41
endpoints : [
29
42
{
30
43
apiId : 'api-i84p7rla' ,
@@ -35,17 +48,6 @@ describe('apigw', () => {
35
48
function : {
36
49
functionName : 'egg-function' ,
37
50
} ,
38
- usagePlan : {
39
- usagePlanId : 'usagePlan-8bbr8pup' ,
40
- usagePlanName : 'slscmp' ,
41
- usagePlanDesc : 'sls create' ,
42
- maxRequestNum : 1000 ,
43
- } ,
44
- auth : {
45
- serviceTimeout : 15 ,
46
- secretName : 'authName' ,
47
- secretIds : [ 'xxx' ] ,
48
- } ,
49
51
} ,
50
52
{
51
53
path : '/mo' ,
@@ -97,19 +99,99 @@ describe('apigw', () => {
97
99
const apigw = new Apigw ( credentials , process . env . REGION ) ;
98
100
let outputs ;
99
101
100
- test ( 'should deploy a apigw success' , async ( ) => {
101
- outputs = await apigw . deploy ( inputs ) ;
102
+ test ( '[Environment UsagePlan] should deploy a apigw success' , async ( ) => {
103
+ const apigwInputs = deepClone ( inputs ) ;
104
+ outputs = await apigw . deploy ( apigwInputs ) ;
105
+ expect ( outputs ) . toEqual ( {
106
+ created : true ,
107
+ serviceId : expect . stringContaining ( 'service-' ) ,
108
+ serviceName : 'serverless_test' ,
109
+ subDomain : expect . stringContaining ( '.apigw.tencentcs.com' ) ,
110
+ protocols : 'http&https' ,
111
+ environment : 'release' ,
112
+ usagePlan : {
113
+ created : true ,
114
+ secrets : {
115
+ created : true ,
116
+ secretIds : expect . any ( Array ) ,
117
+ } ,
118
+ usagePlanId : expect . stringContaining ( 'usagePlan-' ) ,
119
+ } ,
120
+ apiList : [
121
+ {
122
+ path : '/' ,
123
+ internalDomain : null ,
124
+ method : 'GET' ,
125
+ apiName : 'index' ,
126
+ apiId : expect . stringContaining ( 'api-' ) ,
127
+ created : true ,
128
+ } ,
129
+ {
130
+ path : '/mo' ,
131
+ method : 'GET' ,
132
+ apiName : 'mo' ,
133
+ internalDomain : null ,
134
+ apiId : expect . stringContaining ( 'api-' ) ,
135
+ created : true ,
136
+ } ,
137
+ {
138
+ path : '/auto' ,
139
+ method : 'GET' ,
140
+ apiName : 'auto-http' ,
141
+ internalDomain : null ,
142
+ apiId : expect . stringContaining ( 'api-' ) ,
143
+ created : true ,
144
+ } ,
145
+ {
146
+ path : '/ws' ,
147
+ method : 'GET' ,
148
+ apiName : 'ws-test' ,
149
+ internalDomain : null ,
150
+ apiId : expect . stringContaining ( 'api-' ) ,
151
+ created : true ,
152
+ } ,
153
+ {
154
+ path : '/wsf' ,
155
+ method : 'GET' ,
156
+ apiName : 'ws-scf' ,
157
+ internalDomain : expect . stringContaining (
158
+ 'http://set-websocket.cb-common.apigateway.tencentyun.com' ,
159
+ ) ,
160
+ apiId : expect . stringContaining ( 'api-' ) ,
161
+ created : true ,
162
+ } ,
163
+ ] ,
164
+ } ) ;
165
+ } ) ;
166
+
167
+ test ( '[Environment UsagePlan] should remove apigw success' , async ( ) => {
168
+ await apigw . remove ( outputs ) ;
169
+ const detail = await apigw . request ( {
170
+ Action : 'DescribeService' ,
171
+ ServiceId : outputs . serviceId ,
172
+ } ) ;
173
+
174
+ expect ( detail ) . toBeNull ( ) ;
175
+ } ) ;
176
+
177
+ test ( '[Api UsagePlan] should deploy a apigw success' , async ( ) => {
178
+ const apigwInputs = deepClone ( inputs ) ;
179
+ apigwInputs . endpoints [ 0 ] . usagePlan = apigwInputs . usagePlan ;
180
+ apigwInputs . endpoints [ 0 ] . auth = apigwInputs . auth ;
181
+ delete apigwInputs . usagePlan ;
182
+ delete apigwInputs . auth ;
183
+
184
+ outputs = await apigw . deploy ( apigwInputs ) ;
102
185
expect ( outputs ) . toEqual ( {
103
186
created : true ,
104
187
serviceId : expect . stringContaining ( 'service-' ) ,
105
188
serviceName : 'serverless_test' ,
106
189
subDomain : expect . stringContaining ( '.apigw.tencentcs.com' ) ,
107
- protocols : inputs . protocols ,
190
+ protocols : 'http&https' ,
108
191
environment : 'release' ,
109
192
apiList : [
110
193
{
111
194
path : '/' ,
112
- bindType : 'API' ,
113
195
internalDomain : null ,
114
196
method : 'GET' ,
115
197
apiName : 'index' ,
@@ -118,8 +200,8 @@ describe('apigw', () => {
118
200
usagePlan : {
119
201
created : true ,
120
202
secrets : {
121
- created : false ,
122
- secretIds : [ ] ,
203
+ created : true ,
204
+ secretIds : expect . any ( Array ) ,
123
205
} ,
124
206
usagePlanId : expect . stringContaining ( 'usagePlan-' ) ,
125
207
} ,
@@ -162,7 +244,7 @@ describe('apigw', () => {
162
244
} ) ;
163
245
} ) ;
164
246
165
- test ( 'should remove apigw success' , async ( ) => {
247
+ test ( '[Api UsagePlan] should remove apigw success' , async ( ) => {
166
248
await apigw . remove ( outputs ) ;
167
249
const detail = await apigw . request ( {
168
250
Action : 'DescribeService' ,
0 commit comments