Skip to content

Commit

Permalink
chore: add tests for useProxyCache middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Aug 14, 2023
1 parent 6d8f1b1 commit 1642b90
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,14 @@ export async function get(key) {
return false;
}
}

export async function remove(key) {
try {
return await client.deleteObject({
Bucket: process.env.AWS_BUCKET_NAME,
Key: `public/${dir}/${key}`
});
} catch (e: any) {
return false;
}
}
10 changes: 9 additions & 1 deletion src/middlewares/useProxyCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ import { capture } from '@snapshot-labs/snapshot-sentry';
import { sha256, MAX } from '../utils';
import { get, set } from '../aws';

export function cacheKey(key: string) {
return sha256(key);
}

/**
* This middleware serves a cache if it exists, else it will process the controller
* and caches its results if it's less than 1MB
*/
export default async function useProxyCache(req, res, next) {
const key = sha256(req.originalUrl);
const key = cacheKey(req.originalUrl);

const cache = await get(`cache/${key}`);
if (cache) {
Expand Down
38 changes: 32 additions & 6 deletions test/e2e/proxy.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
import request from 'supertest';
import { cacheKey } from '../../src/middlewares/useProxyCache';
import { set, get, remove } from '../../src/aws';

const HOST = `http://localhost:${process.env.PORT || 3003}`;

describe('GET /ipfs/*', () => {
describe('when the IPFS cid exists', () => {
it('returns the JSON file', async () => {
const response = await request(HOST).get(
'/ipfs/bafkreib5epjzumf3omr7rth5mtcsz4ugcoh3ut4d46hx5xhwm4b3pqr2vi'
);
const path = '/ipfs/bafkreib5epjzumf3omr7rth5mtcsz4ugcoh3ut4d46hx5xhwm4b3pqr2vi';
const content = { status: 'OK' };
const key = `cache/${cacheKey(path)}`;

expect(response.statusCode).toBe(200);
expect(response.body).toEqual({ status: 'OK' });
afterEach(async () => {
await remove(key);
});

describe('when the file is cached', () => {
const cachedContent = { status: 'CACHED' };

it('returns the cache file', async () => {
await set(key, cachedContent);
const response = await request(HOST).get(path);

expect(response.body).toEqual(cachedContent);
expect(response.statusCode).toBe(200);
expect(response.headers['content-type']).toBe('application/json; charset=utf-8');
expect(await get(key)).toEqual(cachedContent);
});
});

describe('when the file is not cached', () => {
it('returns the file and caches it', async () => {
const response = await request(HOST).get(path);

expect(response.body).toEqual(content);
expect(response.statusCode).toBe(200);
expect(response.headers['content-type']).toBe('application/json; charset=utf-8');
expect(await get(key)).toEqual(response.body);
});
});
});

Expand Down

0 comments on commit 1642b90

Please sign in to comment.