Skip to content

Commit e8848eb

Browse files
committed
adding test for building redirects
1 parent f6d384b commit e8848eb

File tree

3 files changed

+152
-7
lines changed

3 files changed

+152
-7
lines changed

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ module.exports = {
4242
const functionTemplatesPath = path.join(__dirname, 'templates/functions');
4343
const functionTemplates = await fs.readdir(functionTemplatesPath);
4444

45+
if ( !Array.isArray(functionTemplates) || functionTemplates.length == 0 ) {
46+
throw new Error(`Failed to generate templates: can not find function templates in ${functionTemplatesPath}`);
47+
}
48+
4549
try {
4650
await Promise.all(functionTemplates.map(async templateFileName => {
4751
const bundle = await ncc(path.join(functionTemplatesPath, templateFileName));
@@ -53,8 +57,7 @@ module.exports = {
5357
await fs.writeFile(filePath, bundle.code, 'utf8');
5458
}));
5559
} catch(e) {
56-
console.log('Failed to generate templates:', e);
57-
throw e;
60+
throw new Error(`Failed to generate templates: ${e}`);
5861
}
5962

6063
// Configure reference parameters for Cloudinary delivery to attach to redirect

tests/lib/util.test.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
const { isRemoteUrl, determineRemoteUrl } = require('../../src/lib/util');
1+
const { isRemoteUrl, determineRemoteUrl, getQueryParams } = require('../../src/lib/util');
22

33
describe('lib/util', () => {
44

55
describe('isRemoteUrl', () => {
6-
6+
77
test('should be a remote URL', () => {
88
expect(isRemoteUrl('https://cloudinary.com')).toEqual(true);
99
expect(isRemoteUrl('http://cloudinary.com')).toEqual(true);
1010
});
11-
11+
1212
test('should not be a remote URL', () => {
1313
expect(isRemoteUrl('/images/cloud.jpg')).toEqual(false);
1414
expect(isRemoteUrl('images/cloud.jpg')).toEqual(false);
@@ -17,15 +17,15 @@ describe('lib/util', () => {
1717
});
1818

1919
describe('determineRemoteUrl', () => {
20-
20+
2121
test('should return original value when remote URL', () => {
2222
const secure = determineRemoteUrl('https://cloudinary.com', 'https://cloudinary.netlify.app');
2323
expect(secure).toEqual('https://cloudinary.com');
2424

2525
const notSecure = determineRemoteUrl('http://cloudinary.com', 'https://cloudinary.netlify.app');
2626
expect(notSecure).toEqual('http://cloudinary.com');
2727
});
28-
28+
2929
test('should prepend DEPLOY_PRIME_URL when local path', () => {
3030
const withStartingSlash = determineRemoteUrl('/images/cloud.jpg', 'https://cloudinary.netlify.app');
3131
expect(withStartingSlash).toEqual('https://cloudinary.netlify.app/images/cloud.jpg');
@@ -36,4 +36,19 @@ describe('lib/util', () => {
3636

3737
});
3838

39+
describe('getQueryParams', () => {
40+
41+
test('should get query parameters by URL with a single param', () => {
42+
const { myparam } = getQueryParams('https://cloudinary.com?myparam=myvalue')
43+
expect(myparam).toEqual('myvalue');
44+
});
45+
46+
test('should get query parameters by URL with a multiple params', () => {
47+
const { myparam, otherparam } = getQueryParams('https://cloudinary.com?myparam=myvalue&otherparam=othervalue')
48+
expect(myparam).toEqual('myvalue');
49+
expect(otherparam).toEqual('othervalue');
50+
});
51+
52+
});
53+
3954
});

tests/on-build.test.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
const fs = require('fs-extra');
2+
const ncc = require('@vercel/ncc');
3+
const { onBuild } = require('../src/');
4+
5+
jest.mock('fs-extra', () => ({
6+
readdir: jest.fn(),
7+
ensureDir: jest.fn(),
8+
writeFile: jest.fn(),
9+
}));
10+
11+
jest.mock('@vercel/ncc', () => jest.fn());
12+
13+
describe('onBuild', () => {
14+
15+
describe('Redirects', () => {
16+
17+
test('should create redirects with defaut fetch-based configuration', async () => {
18+
const imagesFunctionName = 'cld_images';
19+
20+
fs.readdir.mockResolvedValue([imagesFunctionName]);
21+
ncc.mockResolvedValue('exports.handler = async function (event, context) {}');
22+
23+
process.env.SITE_NAME = 'cool-site';
24+
process.env.CLOUDINARY_CLOUD_NAME = 'testcloud';
25+
26+
const deliveryType = 'fetch';
27+
28+
const defaultRedirect = {
29+
from: '/path',
30+
to: '/other-path',
31+
status: 200
32+
}
33+
34+
const redirects = [defaultRedirect];
35+
36+
const netlifyConfig = {
37+
redirects
38+
};
39+
40+
await onBuild({
41+
netlifyConfig,
42+
constants: {
43+
INTERNAL_FUNCTIONS_SRC: '.netlify/functions-internal'
44+
},
45+
inputs: {
46+
deliveryType
47+
}
48+
});
49+
50+
expect(redirects[0]).toEqual({
51+
from: '/images/*',
52+
to: `/.netlify/functions/${imagesFunctionName}?path=/images/:splat&deliveryType=${deliveryType}&cloudName=${process.env.CLOUDINARY_CLOUD_NAME}&folder=${process.env.SITE_NAME}`,
53+
status: 302,
54+
force: true
55+
});
56+
57+
expect(redirects[1]).toEqual({
58+
from: 'cld-assets/images/*',
59+
to: '/images/:splat',
60+
status: 200,
61+
force: true
62+
});
63+
64+
expect(redirects[2]).toEqual(defaultRedirect);
65+
});
66+
67+
68+
test('should create redirects with upload delivery type and custom inputs', async () => {
69+
const imagesFunctionName = 'cld_images';
70+
71+
fs.readdir.mockResolvedValue([imagesFunctionName]);
72+
ncc.mockResolvedValue('exports.handler = async function (event, context) {}');
73+
74+
process.env.SITE_NAME = 'cool-site';
75+
76+
const cloudName = 'othercloud';
77+
const deliveryType = 'upload';
78+
const imagesPath = '/awesome';
79+
const folder = 'test-folder';
80+
const uploadPreset = 'my-upload-preset';
81+
82+
const defaultRedirect = {
83+
from: '/path',
84+
to: '/other-path',
85+
status: 200
86+
}
87+
88+
const redirects = [defaultRedirect];
89+
90+
const netlifyConfig = {
91+
redirects
92+
};
93+
94+
await onBuild({
95+
netlifyConfig,
96+
constants: {
97+
INTERNAL_FUNCTIONS_SRC: '.netlify/functions-internal'
98+
},
99+
inputs: {
100+
cloudName,
101+
deliveryType,
102+
imagesPath,
103+
folder,
104+
uploadPreset
105+
}
106+
});
107+
108+
expect(redirects[0]).toEqual({
109+
from: `${imagesPath}/*`,
110+
to: `/.netlify/functions/${imagesFunctionName}?path=${imagesPath}/:splat&uploadPreset=${uploadPreset}&deliveryType=${deliveryType}&cloudName=${process.env.CLOUDINARY_CLOUD_NAME}&folder=${folder}`,
111+
status: 302,
112+
force: true
113+
});
114+
115+
expect(redirects[1]).toEqual({
116+
from: `cld-assets${imagesPath}/*`,
117+
to: `${imagesPath}/:splat`,
118+
status: 200,
119+
force: true
120+
});
121+
122+
expect(redirects[2]).toEqual(defaultRedirect);
123+
});
124+
125+
});
126+
127+
});

0 commit comments

Comments
 (0)