-
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.
* support urlencoded form data * remove prettierrc
- Loading branch information
Showing
5 changed files
with
85 additions
and
49 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
This file was deleted.
Oops, something went wrong.
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
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,46 +1,76 @@ | ||
import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test'; | ||
import { describe, it, expect } from 'vitest'; | ||
import worker from '../src'; | ||
import { | ||
env, | ||
createExecutionContext, | ||
waitOnExecutionContext, | ||
SELF, | ||
} from "cloudflare:test"; | ||
import { describe, it, expect } from "vitest"; | ||
import worker from "../src"; | ||
|
||
const makeRequest = async (request) => { | ||
const send = 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', { | ||
method: 'POST', | ||
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', | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
const response = await makeRequest(request); | ||
const response = await send(request); | ||
expect(await response.json()).toEqual({ | ||
errors: ["missing required parameter 'subject'", "missing required parameter 'to'"], | ||
errors: [ | ||
"missing required parameter 'subject'", | ||
"missing required parameter 'to'", | ||
], | ||
}); | ||
expect(response.status).toEqual(400); | ||
}); | ||
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', | ||
it("responds to application/json and application/x-www-form-urlencoded", async () => { | ||
const jsonResponse = await send( | ||
new Request("http://example.com/api/notify", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
to: "[email protected]", | ||
subject: "hello world", | ||
}), | ||
}), | ||
); | ||
expect(await jsonResponse.json()).toEqual({ | ||
to: "[email protected]", | ||
subject: "hello world", | ||
}); | ||
const response = await makeRequest(request); | ||
expect(await response.json()).toEqual(expect.not.objectContaining({ errors: [] })); | ||
expect(response.status).toEqual(200); | ||
expect(jsonResponse.status).toEqual(200); | ||
|
||
const formResponse = await send( | ||
new Request("http://example.com/api/notify", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}, | ||
body: "[email protected]&subject=hello world", | ||
}), | ||
); | ||
expect(await formResponse.json()).toEqual({ | ||
to: "[email protected]", | ||
subject: "hello world", | ||
}); | ||
expect(formResponse.status).toEqual(200); | ||
|
||
expect.assertions(4); | ||
}); | ||
it('404s for undefined routes', async () => { | ||
const request = new Request('http://example.com/undefined-route'); | ||
const response = await makeRequest(request); | ||
|
||
it("404s for undefined routes", async () => { | ||
const request = new Request("http://example.com/undefined-route"); | ||
const response = await send(request); | ||
expect(response.status).toEqual(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