-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,37 @@ | ||
/** | ||
* Welcome to Cloudflare Workers! This is your first worker. | ||
* | ||
* - Run `npm run dev` in your terminal to start a development server | ||
* - Open a browser tab at http://localhost:8787/ to see your worker in action | ||
* - Run `npm run deploy` to publish your worker | ||
* | ||
* Learn more at https://developers.cloudflare.com/workers/ | ||
*/ | ||
const validate = async (request) => { | ||
const required = ['subject', 'to']; | ||
let data; | ||
try { | ||
data = (await request.json()) ?? {}; | ||
} catch (e) { | ||
data = {}; | ||
} | ||
|
||
data.errors = required.reduce((acc, el) => { | ||
if (!data[el]) { | ||
return [...acc, `missing required parameter '${el}'`]; | ||
} | ||
return acc; | ||
}, []); | ||
|
||
return data; | ||
}; | ||
|
||
export default { | ||
async fetch(request, env, ctx) { | ||
return new Response('Hello World!!'); | ||
const { pathname } = new URL(request.url); | ||
if (pathname === '/api/notify') { | ||
const { subject, to, errors } = await validate(request); | ||
const body = { | ||
subject, | ||
to, | ||
}; | ||
const response = { | ||
...body, | ||
...(errors.length && { errors }), | ||
}; | ||
return new Response(JSON.stringify(response), { status: errors.length ? 400 : 200 }); | ||
} | ||
return new Response(null, { status: 404 }); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,22 +2,37 @@ import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloud | |
import { describe, it, expect } from 'vitest'; | ||
import worker from '../src'; | ||
|
||
describe('Hello World worker', () => { | ||
it('responds with Hello World! (unit style)', async () => { | ||
const request = new Request('http://example.com'); | ||
// Create an empty context to pass to `worker.fetch()`. | ||
describe('API', () => { | ||
it('errors for missing required parameters', async () => { | ||
const request = new Request('http://example.com/api/notify', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
const ctx = createExecutionContext(); | ||
const response = await worker.fetch(request, env, ctx); | ||
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions | ||
await waitOnExecutionContext(ctx); | ||
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!!"`); | ||
expect(await response.json()).toEqual({ | ||
errors: ["missing required parameter 'subject'", "missing required parameter 'to'"], | ||
}); | ||
expect(response.status).toEqual(400); | ||
}); | ||
|
||
it('responds with Hello World! (integration style)', async () => { | ||
const request = new Request('http://example.com'); | ||
const env = {}; | ||
const ctx = {}; | ||
const response = await SELF.fetch(request, env, ctx); | ||
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!!"`); | ||
it('does not error if the required parameters are present', async () => { | ||
const request = new Request('http://example.com/api/notify', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
to: '[email protected]', | ||
subject: 'hello world', | ||
}), | ||
}); | ||
const ctx = createExecutionContext(); | ||
const response = await worker.fetch(request, env, ctx); | ||
await waitOnExecutionContext(ctx); | ||
expect(await response.json()).toEqual(expect.not.objectContaining({ errors: [] })); | ||
expect(response.status).toEqual(200); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters