Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor #3

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ export default {
};
return new Response(JSON.stringify(response), { status: errors.length ? 400 : 200 });
}
return new Response(null, { status: 404 });
return new Response(
JSON.stringify(
{
message: 'Not Found',
documentation_url: 'https://www.cinotify.cc/docs',
},
null,
2,
),
{ status: 404 },
);
},
};
20 changes: 14 additions & 6 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloud
import { describe, it, expect } from 'vitest';
import worker from '../src';

const makeRequest = async (request) => {
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
return response;
};

describe('API', () => {
it('errors for missing required parameters', async () => {
const request = new Request('http://example.com/api/notify', {
Expand All @@ -10,9 +17,7 @@ describe('API', () => {
'Content-Type': 'application/json',
},
});
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
const response = await makeRequest(request);
expect(await response.json()).toEqual({
errors: ["missing required parameter 'subject'", "missing required parameter 'to'"],
});
Expand All @@ -29,10 +34,13 @@ describe('API', () => {
subject: 'hello world',
}),
});
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
const response = await makeRequest(request);
expect(await response.json()).toEqual(expect.not.objectContaining({ errors: [] }));
expect(response.status).toEqual(200);
});
it('404s for undefined routes', async () => {
const request = new Request('http://example.com/undefined-route');
const response = await makeRequest(request);
expect(response.status).toEqual(404);
});
});
Loading