cloudworker-proxy
Version:
An api gateway for cloudflare workers
79 lines (63 loc) • 1.81 kB
JavaScript
const { expect } = require('chai');
const kvStorageFactory = require('../../src/handlers/kv-storage');
const helpers = require('../helpers');
const fetchMock = require('fetch-mock');
function mockCall(key) {
fetchMock.mock(
`https://api.cloudflare.com/client/v4/accounts/accountId/storage/kv/namespaces/namespace/values/${key}`,
'OK',
);
}
describe('kvStorage', () => {
let handler;
beforeEach(() => {
handler = kvStorageFactory({
kvAccountId: 'accountId',
kvNamespace: 'namespace',
kvAuthEmail: 'authEmail',
kvAuthKey: 'authKey',
});
});
afterEach(() => {
fetchMock.restore();
});
it('should fetch a file from kv', async () => {
mockCall('index.html');
const ctx = helpers.getCtx();
ctx.request.path = '/index.html';
ctx.params = {
file: 'index.html',
};
await handler(ctx, []);
expect(ctx.status).to.equal(200);
});
it('should return a 404 if a file is not found', async () => {
const ctx = helpers.getCtx();
ctx.request.path = '/index.html';
ctx.params = {
file: 'index.html',
};
await handler(ctx, []);
expect(ctx.status).to.equal(404);
});
it('apply a default file type to a file fetched for kv', async () => {
mockCall('index.html');
const ctx = helpers.getCtx();
ctx.request.path = '/index';
ctx.params = {
file: 'index',
};
await handler(ctx, []);
expect(ctx.status).to.equal(200);
});
it('apply a default file type to a file in a nested folder', async () => {
mockCall('nested/folder/index.html');
const ctx = helpers.getCtx();
ctx.request.path = '/nested/folder/index';
ctx.params = {
file: 'nested/folder/index',
};
await handler(ctx, []);
expect(ctx.status).to.equal(200);
});
});