Skip to content

Commit 7157d2c

Browse files
committed
fix(cos): support disableErrorStatus config for website
1 parent 03876ac commit 7157d2c

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

__tests__/cos.test.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { Cos } = require('../src');
22
const path = require('path');
3-
const request = require('request-promise-native');
3+
const axios = require('axios');
44
const { sleep } = require('@ygkit/request');
55

66
describe('Cos', () => {
@@ -55,6 +55,8 @@ describe('Cos', () => {
5555
const websiteInputs = {
5656
code: {
5757
src: staticPath,
58+
index: 'index.html',
59+
error: 'index.html',
5860
},
5961
bucket: bucket,
6062
src: staticPath,
@@ -71,19 +73,37 @@ describe('Cos', () => {
7173
const res = await cos.deploy(inputs);
7274
await sleep(1000);
7375
const reqUrl = `https://${bucket}.cos.${process.env.REGION}.myqcloud.com/index.html`;
74-
const content = await request.get(reqUrl);
76+
const { data } = await axios.get(reqUrl);
7577
expect(res).toEqual(inputs);
76-
expect(content).toMatch(/Serverless\sFramework/gi);
78+
expect(data).toMatch(/Serverless\sFramework/gi);
7779
});
7880

7981
test('should deploy website success', async () => {
8082
const res = await cos.website(websiteInputs);
8183
await sleep(1000);
8284
const websiteUrl = `${inputs.bucket}.cos-website.${process.env.REGION}.myqcloud.com`;
8385
const reqUrl = `${websiteInputs.protocol}://${websiteUrl}`;
84-
const content = await request.get(reqUrl);
86+
const { data } = await axios.get(reqUrl);
87+
try {
88+
await axios.get(`${reqUrl}/error.html`);
89+
} catch (e) {
90+
expect(e.response.status).toBe(404);
91+
expect(e.response.data).toMatch(/Serverless\sFramework/gi);
92+
}
93+
expect(res).toBe(websiteUrl);
94+
expect(data).toMatch(/Serverless\sFramework/gi);
95+
});
96+
97+
test('should deploy website and error code with 200', async () => {
98+
websiteInputs.disableErrorStatus = true;
99+
const res = await cos.website(websiteInputs);
100+
await sleep(1000);
101+
const websiteUrl = `${inputs.bucket}.cos-website.${process.env.REGION}.myqcloud.com`;
102+
const reqUrl = `${websiteInputs.protocol}://${websiteUrl}`;
103+
const { data, status } = await axios.get(`${reqUrl}/error.html`);
85104
expect(res).toBe(websiteUrl);
86-
expect(content).toMatch(/Serverless\sFramework/gi);
105+
expect(data).toMatch(/Serverless\sFramework/gi);
106+
expect(status).toBe(200);
87107
});
88108

89109
test('should deploy Cos success with policy', async () => {
@@ -92,9 +112,9 @@ describe('Cos', () => {
92112
const res = await cos.deploy(inputs);
93113
await sleep(1000);
94114
const reqUrl = `https://${bucket}.cos.${process.env.REGION}.myqcloud.com/index.html`;
95-
const content = await request.get(reqUrl);
115+
const { data } = await axios.get(reqUrl);
96116
expect(res).toEqual(inputs);
97-
expect(content).toMatch(/Serverless\sFramework/gi);
117+
expect(data).toMatch(/Serverless\sFramework/gi);
98118
});
99119

100120
test('should deploy website success with policy', async () => {
@@ -104,9 +124,9 @@ describe('Cos', () => {
104124
await sleep(1000);
105125
const websiteUrl = `${inputs.bucket}.cos-website.${process.env.REGION}.myqcloud.com`;
106126
const reqUrl = `${websiteInputs.protocol}://${websiteUrl}`;
107-
const content = await request.get(reqUrl);
127+
const { data } = await axios.get(reqUrl);
108128
expect(res).toBe(websiteUrl);
109-
expect(content).toMatch(/Serverless\sFramework/gi);
129+
expect(data).toMatch(/Serverless\sFramework/gi);
110130
});
111131

112132
test('should remove Cos success', async () => {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"@semantic-release/npm": "^7.0.4",
6262
"@semantic-release/release-notes-generator": "^9.0.1",
6363
"@ygkit/secure": "^0.0.3",
64+
"axios": "^0.21.0",
6465
"babel-eslint": "^10.1.0",
6566
"dotenv": "^8.2.0",
6667
"eslint": "^6.8.0",

src/modules/cos/index.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const COS = require('cos-nodejs-sdk-v5');
2-
const util = require('util');
32
const path = require('path');
43
const fs = require('fs');
5-
const exec = util.promisify(require('child_process').exec);
64
const { traverseDirSync } = require('../../utils');
75
const { TypeError, ApiError } = require('../../utils/error');
86

@@ -438,6 +436,7 @@ class Cos {
438436
},
439437
ErrorDocument: {
440438
Key: inputs.code.error || 'error.html',
439+
OriginalHttpStatus: inputs.disableErrorStatus === true ? 'Disabled' : 'Enabled',
441440
},
442441
RedirectAllRequestsTo: {
443442
Protocol: inputs.protocol || 'http',
@@ -664,20 +663,6 @@ class Cos {
664663
}
665664
}
666665

667-
// If a hook is provided, build the website
668-
if (inputs.code.hook) {
669-
const options = { cwd: inputs.code.root };
670-
try {
671-
await exec(inputs.code.hook, options);
672-
} catch (err) {
673-
throw new TypeError(
674-
`DEPLOY_COS_EXEC_HOOK`,
675-
`Failed building website via "${inputs.code.hook}" due to the following error: "${err.stderr}"`,
676-
err.stack,
677-
);
678-
}
679-
}
680-
681666
// upload
682667
const dirToUploadPath = inputs.code.src || inputs.code.root;
683668
const uploadDict = {

0 commit comments

Comments
 (0)