|
| 1 | +import { ApigwDeployInputs, ApigwDeployOutputs } from '../src/modules/apigw/interface'; |
| 2 | +import { Apigw } from '../src'; |
| 3 | +import { deepClone } from '../src/utils'; |
| 4 | + |
| 5 | +describe('apigw', () => { |
| 6 | + const domains = [`test-1.${Date.now()}.com`, `test-2.${Date.now()}.com`]; |
| 7 | + const credentials = { |
| 8 | + SecretId: process.env.TENCENT_SECRET_ID, |
| 9 | + SecretKey: process.env.TENCENT_SECRET_KEY, |
| 10 | + }; |
| 11 | + const inputs: ApigwDeployInputs = { |
| 12 | + protocols: ['http', 'https'], |
| 13 | + serviceName: 'serverless_test', |
| 14 | + environment: 'release', |
| 15 | + netTypes: ['OUTER'], |
| 16 | + usagePlan: { |
| 17 | + usagePlanId: 'usagePlan-8bbr8pup', |
| 18 | + usagePlanName: 'slscmp', |
| 19 | + usagePlanDesc: 'sls create', |
| 20 | + maxRequestNum: 1000, |
| 21 | + }, |
| 22 | + auth: { |
| 23 | + secretName: 'authName', |
| 24 | + }, |
| 25 | + endpoints: [ |
| 26 | + { |
| 27 | + apiId: 'api-i84p7rla', |
| 28 | + path: '/', |
| 29 | + protocol: 'HTTP', |
| 30 | + method: 'GET', |
| 31 | + apiName: 'index', |
| 32 | + function: { |
| 33 | + functionName: 'serverless-unit-test', |
| 34 | + }, |
| 35 | + isBase64Encoded: true, |
| 36 | + isBase64Trigger: true, |
| 37 | + base64EncodedTriggerRules: [ |
| 38 | + { |
| 39 | + name: 'Accept', |
| 40 | + value: ['application/x-vpeg005', 'application/xhtml+xml'], |
| 41 | + }, |
| 42 | + { |
| 43 | + name: 'Content_Type', |
| 44 | + value: ['application/x-vpeg005', 'application/xhtml+xml'], |
| 45 | + }, |
| 46 | + ], |
| 47 | + }, |
| 48 | + { |
| 49 | + path: '/mo', |
| 50 | + protocol: 'HTTP', |
| 51 | + method: 'GET', |
| 52 | + apiName: 'mo', |
| 53 | + serviceType: 'MOCK', |
| 54 | + serviceMockReturnMessage: 'test mock response', |
| 55 | + }, |
| 56 | + { |
| 57 | + path: '/auto', |
| 58 | + protocol: 'HTTP', |
| 59 | + apiName: 'auto-http', |
| 60 | + method: 'GET', |
| 61 | + serviceType: 'HTTP', |
| 62 | + serviceConfig: { |
| 63 | + url: 'http://www.baidu.com', |
| 64 | + path: '/test', |
| 65 | + method: 'GET', |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + path: '/ws', |
| 70 | + protocol: 'WEBSOCKET', |
| 71 | + apiName: 'ws-test', |
| 72 | + method: 'GET', |
| 73 | + serviceType: 'WEBSOCKET', |
| 74 | + serviceConfig: { |
| 75 | + url: 'ws://yugasun.com', |
| 76 | + path: '/', |
| 77 | + method: 'GET', |
| 78 | + }, |
| 79 | + }, |
| 80 | + { |
| 81 | + path: '/wsf', |
| 82 | + protocol: 'WEBSOCKET', |
| 83 | + apiName: 'ws-scf', |
| 84 | + method: 'GET', |
| 85 | + serviceType: 'SCF', |
| 86 | + function: { |
| 87 | + functionNamespace: 'default', |
| 88 | + functionQualifier: '$DEFAULT', |
| 89 | + transportFunctionName: 'serverless-unit-test', |
| 90 | + registerFunctionName: 'serverless-unit-test', |
| 91 | + }, |
| 92 | + }, |
| 93 | + // below two api is for oauth2.0 test |
| 94 | + { |
| 95 | + path: '/oauth', |
| 96 | + protocol: 'HTTP', |
| 97 | + method: 'GET', |
| 98 | + apiName: 'oauthapi', |
| 99 | + authType: 'OAUTH', |
| 100 | + businessType: 'OAUTH', |
| 101 | + serviceType: 'HTTP', |
| 102 | + serviceConfig: { |
| 103 | + method: 'GET', |
| 104 | + path: '/check', |
| 105 | + url: 'http://10.64.47.103:9090', |
| 106 | + }, |
| 107 | + oauthConfig: { |
| 108 | + loginRedirectUrl: 'http://10.64.47.103:9090/code', |
| 109 | + publicKey: process.env.API_PUBLIC_KEY, |
| 110 | + tokenLocation: 'method.req.header.authorization', |
| 111 | + // tokenLocation: 'method.req.header.cookie', |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + path: '/oauthwork', |
| 116 | + protocol: 'HTTP', |
| 117 | + method: 'GET', |
| 118 | + apiName: 'business', |
| 119 | + authType: 'OAUTH', |
| 120 | + businessType: 'NORMAL', |
| 121 | + authRelationApi: { |
| 122 | + path: '/oauth', |
| 123 | + method: 'GET', |
| 124 | + }, |
| 125 | + serviceType: 'MOCK', |
| 126 | + serviceMockReturnMessage: 'helloworld', |
| 127 | + }, |
| 128 | + ], |
| 129 | + }; |
| 130 | + const apigw = new Apigw(credentials, process.env.REGION); |
| 131 | + let outputs: ApigwDeployOutputs; |
| 132 | + |
| 133 | + // 由于自定义域名必须 ICP 备案,所以这里测试域名不会通过,具体测试请使用 |
| 134 | + test('[Apigw CustomDomain] bind CustomDomain success', async () => { |
| 135 | + const apigwInputs = deepClone(inputs); |
| 136 | + |
| 137 | + apigwInputs.usagePlan = undefined; |
| 138 | + apigwInputs.customDomains = [ |
| 139 | + { |
| 140 | + domain: domains[0], |
| 141 | + // certificateId: 'cWOJJjax', |
| 142 | + isDefaultMapping: false, |
| 143 | + pathMappingSet: [ |
| 144 | + { |
| 145 | + path: '/', |
| 146 | + environment: 'release', |
| 147 | + }, |
| 148 | + ], |
| 149 | + protocols: ['http'], |
| 150 | + }, |
| 151 | + { |
| 152 | + domain: domains[1], |
| 153 | + // certificateId: 'cWOJJjax', |
| 154 | + isDefaultMapping: false, |
| 155 | + pathMappingSet: [ |
| 156 | + { |
| 157 | + path: '/', |
| 158 | + environment: 'release', |
| 159 | + }, |
| 160 | + ], |
| 161 | + protocols: ['http'], |
| 162 | + }, |
| 163 | + ]; |
| 164 | + outputs = await apigw.deploy(apigwInputs); |
| 165 | + |
| 166 | + expect(outputs.customDomains).toEqual([ |
| 167 | + { |
| 168 | + isBinded: true, |
| 169 | + created: true, |
| 170 | + subDomain: domains[0], |
| 171 | + cname: expect.any(String), |
| 172 | + url: `http://${domains[0]}`, |
| 173 | + }, |
| 174 | + { |
| 175 | + isBinded: true, |
| 176 | + created: true, |
| 177 | + subDomain: domains[1], |
| 178 | + cname: expect.any(String), |
| 179 | + url: `http://${domains[1]}`, |
| 180 | + }, |
| 181 | + ]); |
| 182 | + |
| 183 | + const d = await apigw.customDomain.getCurrentDict(outputs.serviceId); |
| 184 | + expect(d[domains[0]]).toBeDefined(); |
| 185 | + expect(d[domains[1]]).toBeDefined(); |
| 186 | + }); |
| 187 | + |
| 188 | + test('[Apigw CustomDomain] rebind customDomain success (skipped)', async () => { |
| 189 | + const apigwInputs = deepClone(inputs); |
| 190 | + apigwInputs.oldState = outputs; |
| 191 | + |
| 192 | + apigwInputs.usagePlan = undefined; |
| 193 | + apigwInputs.serviceId = outputs.serviceId; |
| 194 | + apigwInputs.customDomains = [ |
| 195 | + { |
| 196 | + domain: domains[0], |
| 197 | + // certificateId: 'cWOJJjax', |
| 198 | + isDefaultMapping: false, |
| 199 | + pathMappingSet: [ |
| 200 | + { |
| 201 | + path: '/', |
| 202 | + environment: 'release', |
| 203 | + }, |
| 204 | + ], |
| 205 | + protocols: ['http'], |
| 206 | + }, |
| 207 | + { |
| 208 | + domain: domains[1], |
| 209 | + // certificateId: 'cWOJJjax', |
| 210 | + isDefaultMapping: false, |
| 211 | + pathMappingSet: [ |
| 212 | + { |
| 213 | + path: '/', |
| 214 | + environment: 'release', |
| 215 | + }, |
| 216 | + ], |
| 217 | + protocols: ['http'], |
| 218 | + }, |
| 219 | + ]; |
| 220 | + |
| 221 | + outputs = await apigw.deploy(apigwInputs); |
| 222 | + |
| 223 | + expect(outputs.customDomains).toEqual([ |
| 224 | + { |
| 225 | + isBinded: true, |
| 226 | + created: true, |
| 227 | + subDomain: domains[0], |
| 228 | + cname: expect.any(String), |
| 229 | + url: `http://${domains[0]}`, |
| 230 | + }, |
| 231 | + { |
| 232 | + isBinded: true, |
| 233 | + created: true, |
| 234 | + subDomain: domains[1], |
| 235 | + cname: expect.any(String), |
| 236 | + url: `http://${domains[1]}`, |
| 237 | + }, |
| 238 | + ]); |
| 239 | + |
| 240 | + const d = await apigw.customDomain.getCurrentDict(outputs.serviceId); |
| 241 | + expect(d[domains[0]]).toBeDefined(); |
| 242 | + expect(d[domains[1]]).toBeDefined(); |
| 243 | + }); |
| 244 | + |
| 245 | + test('[Apigw CustomDomain] unbind customDomain success', async () => { |
| 246 | + const apigwInputs = deepClone(inputs); |
| 247 | + apigwInputs.oldState = outputs; |
| 248 | + |
| 249 | + apigwInputs.serviceId = outputs.serviceId; |
| 250 | + apigwInputs.usagePlan = undefined; |
| 251 | + apigwInputs.customDomains = undefined; |
| 252 | + |
| 253 | + outputs = await apigw.deploy(apigwInputs); |
| 254 | + |
| 255 | + expect(outputs.customDomains).toBeUndefined(); |
| 256 | + |
| 257 | + const d = await apigw.customDomain.getCurrentDict(outputs.serviceId); |
| 258 | + |
| 259 | + expect(d[domains[0]]).toBeUndefined(); |
| 260 | + expect(d[domains[1]]).toBeUndefined(); |
| 261 | + }); |
| 262 | + |
| 263 | + test('[Apigw CustomDomain] should remove apigw success', async () => { |
| 264 | + // FIXME: 手动修改为 created |
| 265 | + outputs.customDomains?.forEach((v) => { |
| 266 | + v.created = true; |
| 267 | + }); |
| 268 | + outputs.apiList?.forEach((v) => { |
| 269 | + v.created = true; |
| 270 | + if (v.usagePlan) { |
| 271 | + v.usagePlan.created = true; |
| 272 | + } |
| 273 | + }); |
| 274 | + outputs.created = true; |
| 275 | + if (outputs.usagePlan) { |
| 276 | + outputs.usagePlan.created = true; |
| 277 | + } |
| 278 | + |
| 279 | + await apigw.remove(outputs); |
| 280 | + const detail = await apigw.request({ |
| 281 | + Action: 'DescribeService', |
| 282 | + ServiceId: outputs.serviceId, |
| 283 | + }); |
| 284 | + expect(detail).toBeNull(); |
| 285 | + }); |
| 286 | +}); |
0 commit comments