|
| 1 | +const { Apigw } = require('../src'); |
| 2 | + |
| 3 | +describe('apigw', () => { |
| 4 | + const credentials = { |
| 5 | + SecretId: process.env.TENCENT_SECRET_ID, |
| 6 | + SecretKey: process.env.TENCENT_SECRET_KEY, |
| 7 | + }; |
| 8 | + const inputs = { |
| 9 | + protocols: ['http', 'https'], |
| 10 | + serviceName: 'serverless_test', |
| 11 | + environment: 'release', |
| 12 | + netTypes: ['OUTER'], |
| 13 | + // customDomains: [ |
| 14 | + // { |
| 15 | + // domain: 'test.yugasun.com', |
| 16 | + // // TODO: change to your certId |
| 17 | + // certificateId: 'cWOJJjax', |
| 18 | + // isDefaultMapping: false, |
| 19 | + // pathMappingSet: [ |
| 20 | + // { |
| 21 | + // path: '/', |
| 22 | + // environment: 'release', |
| 23 | + // }, |
| 24 | + // ], |
| 25 | + // protocols: ['http', 'https'], |
| 26 | + // }, |
| 27 | + // ], |
| 28 | + endpoints: [ |
| 29 | + { |
| 30 | + apiId: 'api-i84p7rla', |
| 31 | + path: '/', |
| 32 | + protocol: 'HTTP', |
| 33 | + method: 'GET', |
| 34 | + apiName: 'index', |
| 35 | + function: { |
| 36 | + functionName: 'egg-function', |
| 37 | + }, |
| 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 | + }, |
| 50 | + { |
| 51 | + path: '/mo', |
| 52 | + protocol: 'HTTP', |
| 53 | + method: 'GET', |
| 54 | + apiName: 'mo', |
| 55 | + serviceType: 'MOCK', |
| 56 | + serviceMockReturnMessage: 'test mock response', |
| 57 | + }, |
| 58 | + { |
| 59 | + path: '/auto', |
| 60 | + protocol: 'HTTP', |
| 61 | + apiName: 'auto-http', |
| 62 | + method: 'GET', |
| 63 | + serviceType: 'HTTP', |
| 64 | + serviceConfig: { |
| 65 | + url: 'http://www.baidu.com', |
| 66 | + path: '/test', |
| 67 | + method: 'GET', |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + path: '/ws', |
| 72 | + protocol: 'WEBSOCKET', |
| 73 | + apiName: 'ws-test', |
| 74 | + method: 'GET', |
| 75 | + serviceType: 'WEBSOCKET', |
| 76 | + serviceConfig: { |
| 77 | + url: 'ws://yugasun.com', |
| 78 | + path: '/', |
| 79 | + method: 'GET', |
| 80 | + }, |
| 81 | + }, |
| 82 | + { |
| 83 | + path: '/wsf', |
| 84 | + protocol: 'WEBSOCKET', |
| 85 | + apiName: 'ws-scf', |
| 86 | + method: 'GET', |
| 87 | + serviceType: 'SCF', |
| 88 | + function: { |
| 89 | + functionNamespace: 'default', |
| 90 | + functionQualifier: '$DEFAULT', |
| 91 | + transportFunctionName: 'fullstack-api', |
| 92 | + registerFunctionName: 'myRestAPI', |
| 93 | + }, |
| 94 | + }, |
| 95 | + ], |
| 96 | + }; |
| 97 | + const apigw = new Apigw(credentials, process.env.REGION); |
| 98 | + let outputs; |
| 99 | + |
| 100 | + test('should deploy a apigw success', async () => { |
| 101 | + outputs = await apigw.deploy(inputs); |
| 102 | + expect(outputs).toEqual({ |
| 103 | + created: true, |
| 104 | + serviceId: expect.stringContaining('service-'), |
| 105 | + serviceName: 'serverless_test', |
| 106 | + subDomain: expect.stringContaining('.apigw.tencentcs.com'), |
| 107 | + protocols: inputs.protocols, |
| 108 | + environment: 'release', |
| 109 | + apiList: [{ |
| 110 | + path: '/', |
| 111 | + bindType: 'API', |
| 112 | + internalDomain: null, |
| 113 | + method: 'GET', |
| 114 | + apiName: 'index', |
| 115 | + apiId: expect.stringContaining('api-'), |
| 116 | + created: true, |
| 117 | + usagePlan: { |
| 118 | + created: true, |
| 119 | + secrets: { |
| 120 | + 'created': false, |
| 121 | + 'secretIds': [], |
| 122 | + }, |
| 123 | + usagePlanId: expect.stringContaining('usagePlan-'), |
| 124 | + }, |
| 125 | + },{ |
| 126 | + path: '/mo', |
| 127 | + method: 'GET', |
| 128 | + apiName: 'mo', |
| 129 | + internalDomain: null, |
| 130 | + apiId: expect.stringContaining('api-'), |
| 131 | + created: true, |
| 132 | + },{ |
| 133 | + path: '/auto', |
| 134 | + method: 'GET', |
| 135 | + apiName: 'auto-http', |
| 136 | + internalDomain: null, |
| 137 | + apiId: expect.stringContaining('api-'), |
| 138 | + created: true, |
| 139 | + },{ |
| 140 | + path: '/ws', |
| 141 | + method: 'GET', |
| 142 | + apiName: 'ws-test', |
| 143 | + internalDomain: null, |
| 144 | + apiId: expect.stringContaining('api-'), |
| 145 | + created: true, |
| 146 | + },{ |
| 147 | + path: '/wsf', |
| 148 | + method: 'GET', |
| 149 | + apiName: 'ws-scf', |
| 150 | + internalDomain: expect.stringContaining('http://set-websocket.cb-common.apigateway.tencentyun.com'), |
| 151 | + apiId: expect.stringContaining('api-'), |
| 152 | + created: true, |
| 153 | + }], |
| 154 | + }); |
| 155 | + |
| 156 | + }); |
| 157 | + |
| 158 | + test('should remove apigw success', async () => { |
| 159 | + await apigw.remove(outputs); |
| 160 | + const detail = await apigw.request({ |
| 161 | + Action: 'DescribeService', |
| 162 | + ServiceId: outputs.serviceId, |
| 163 | + }); |
| 164 | + |
| 165 | + expect(detail).toBeNull(); |
| 166 | + }); |
| 167 | +}); |
| 168 | + |
0 commit comments