Skip to content
206 changes: 149 additions & 57 deletions __tests__/apigw.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { ApigwDeployInputs, ApigwDeployOutputs } from './../src/modules/apigw/interface';
import { Apigw } from '../src';

function deepClone<T>(obj: T): T {
return JSON.parse(JSON.stringify(obj));
}
import { deepClone } from '../src/utils';

describe('apigw', () => {
const domains = [`test-1.${Date.now()}.com`, `test-2.${Date.now()}.com`];
const credentials = {
SecretId: process.env.TENCENT_SECRET_ID,
SecretKey: process.env.TENCENT_SECRET_KEY,
Expand All @@ -15,22 +13,6 @@ describe('apigw', () => {
serviceName: 'serverless_test',
environment: 'release',
netTypes: ['OUTER'],
// customDomains: [
// {
// domain: 'test.yuga.chat',
// // TODO: change to your certId
// certificateId: 'cWOJJjax',
// isDefaultMapping: false,
// pathMappingSet: [
// {
// path: '/',
// environment: 'release',
// },
// ],
// protocols: ['http', 'https'],
// isForcedHttps: true
// },
// ],
usagePlan: {
usagePlanId: 'usagePlan-8bbr8pup',
usagePlanName: 'slscmp',
Expand Down Expand Up @@ -375,46 +357,156 @@ describe('apigw', () => {
Action: 'DescribeService',
ServiceId: outputs.serviceId,
});

expect(detail).toBeNull();
});

// FIXME: remove custom domain test (not complete)
// test.only('[Apigw] Bind CustomDomain success', async () => {
// const apigwInputs = deepClone(inputs);
// apigwInputs.customDomains = [
// {
// domain: 'test-1.sls.plus',
// // certificateId: 'cWOJJjax',
// isDefaultMapping: false,
// pathMappingSet: [
// {
// path: '/',
// environment: 'release',
// },
// ],
// protocols: ['http'],
// isForcedHttps: true,
// },
// {
// domain: 'test-2.sls.plus',
// // certificateId: 'cWOJJjax',
// isDefaultMapping: false,
// pathMappingSet: [
// {
// path: '/',
// environment: 'release',
// },
// ],
// protocols: ['http'],
// isForcedHttps: true,
// },
// ];
// const deployOutputs = await apigw.deploy(inputs);
test('[Apigw CustomDomain] Bind CustomDomain success', async () => {
const apigwInputs = deepClone(inputs);

apigwInputs.usagePlan = undefined;
apigwInputs.customDomains = [
{
domain: domains[0],
// certificateId: 'cWOJJjax',
isDefaultMapping: false,
pathMappingSet: [
{
path: '/',
environment: 'release',
},
],
protocols: ['http'],
},
{
domain: domains[1],
// certificateId: 'cWOJJjax',
isDefaultMapping: false,
pathMappingSet: [
{
path: '/',
environment: 'release',
},
],
protocols: ['http'],
},
];
outputs = await apigw.deploy(apigwInputs);
expect(outputs.customDomains).toEqual([
{
isBinded: true,
created: true,
subDomain: domains[0],
cname: expect.any(String),
url: `http://${domains[0]}`,
},
{
isBinded: true,
created: true,
subDomain: domains[1],
cname: expect.any(String),
url: `http://${domains[1]}`,
},
]);

const d = await apigw.getCurrentCustomDomainsDict(outputs.serviceId);
expect(d[domains[0]]).toBeDefined();
expect(d[domains[1]]).toBeDefined();
});

let oldState: ApigwDeployOutputs;

test('[Apigw CustomDomain] rebind customDomain success (skipped)', async () => {
const apigwInputs = deepClone(inputs);
apigwInputs.usagePlan = undefined;
apigwInputs.serviceId = outputs.serviceId;
apigwInputs.customDomains = [
{
domain: domains[0],
// certificateId: 'cWOJJjax',
isDefaultMapping: false,
pathMappingSet: [
{
path: '/',
environment: 'release',
},
],
protocols: ['http'],
},
{
domain: domains[1],
// certificateId: 'cWOJJjax',
isDefaultMapping: false,
pathMappingSet: [
{
path: '/',
environment: 'release',
},
],
protocols: ['http'],
},
];

outputs = await apigw.deploy(apigwInputs);
oldState = outputs;
expect(outputs.customDomains).toEqual([
{
isBinded: true,
created: true,
subDomain: domains[0],
cname: expect.any(String),
url: `http://${domains[0]}`,
},
{
isBinded: true,
created: true,
subDomain: domains[1],
cname: expect.any(String),
url: `http://${domains[1]}`,
},
]);

const d = await apigw.getCurrentCustomDomainsDict(outputs.serviceId);
expect(d[domains[0]]).toBeDefined();
expect(d[domains[1]]).toBeDefined();
});

test('[Apigw CustomDomain] unbind customDomain success', async () => {
const apigwInputs = deepClone(inputs);

apigwInputs.usagePlan = undefined;
apigwInputs.serviceId = outputs.serviceId;
apigwInputs.customDomains = undefined;
apigwInputs.oldState = oldState;

outputs = await apigw.deploy(apigwInputs);
expect(outputs.customDomains).toBeUndefined();

// const deployOutputsAgain = await apigw.deploy(inputs);
const d = await apigw.getCurrentCustomDomainsDict(outputs.serviceId);
expect(d[domains[0]]).toBeUndefined();
expect(d[domains[1]]).toBeUndefined();
});

test('[Apigw CustomDomain] should remove apigw success', async () => {
// FIXME: 手动修改为 created
outputs.customDomains?.forEach((v) => {
v.created = true;
});
outputs.apiList?.forEach((v) => {
v.created = true;
if (v.usagePlan) {
v.usagePlan.created = true;
}
});
outputs.created = true;
if (outputs.usagePlan) {
outputs.usagePlan.created = true;
}

// console.log({ deployOutputs, deployOutputsAgain });
// await apigw.remove(deployOutputs);
// });
await apigw.remove(outputs);
const detail = await apigw.request({
Action: 'DescribeService',
ServiceId: outputs.serviceId,
});
expect(detail).toBeNull();
});
});
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@
"@tencent-sdk/capi": "^1.1.8",
"@tencent-sdk/cls": "^0.1.7",
"@types/jest": "^26.0.20",
"@types/lodash": "^4.14.167",
"@types/node": "^14.14.20",
"@types/node": "^14.14.31",
"@ygkit/request": "^0.1.8",
"cos-nodejs-sdk-v5": "2.8.6",
"lodash": "^4.17.20",
"moment": "^2.29.1",
"tencent-cloud-sdk": "^1.0.5",
"type-fest": "^0.20.2"
Expand Down
1 change: 1 addition & 0 deletions src/modules/apigw/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const ACTIONS = [
'BindEnvironment',
'UnBindEnvironment',
'DescribeServiceSubDomains',
'DescribeServiceSubDomainMappings',
'BindSubDomain',
'UnBindSubDomain',
] as const;
Expand Down
Loading