|
1 | | -import { describe, it, expect } from 'vitest'; |
2 | | -import { env, createExecutionContext, waitOnExecutionContext } from 'cloudflare:test'; |
3 | | -import worker from '../worker.js'; |
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { |
| 3 | + env, |
| 4 | + createExecutionContext, |
| 5 | + waitOnExecutionContext, |
| 6 | +} from "cloudflare:test"; |
| 7 | +import worker from "../worker.js"; |
4 | 8 |
|
5 | 9 | /** |
6 | 10 | * Helper to make a request to the worker. |
7 | 11 | */ |
8 | | -async function fetchWorker(path, origin = 'https://cambeerfestival.app') { |
9 | | - const request = new Request(`https://worker.example.com${path}`, { |
10 | | - headers: { Origin: origin }, |
11 | | - }); |
12 | | - const ctx = createExecutionContext(); |
13 | | - const response = await worker.fetch(request, env, ctx); |
14 | | - await waitOnExecutionContext(ctx); |
15 | | - return response; |
| 12 | +async function fetchWorker(path, origin = "https://cambeerfestival.app") { |
| 13 | + const request = new Request(`https://worker.example.com${path}`, { |
| 14 | + headers: { Origin: origin }, |
| 15 | + }); |
| 16 | + const ctx = createExecutionContext(); |
| 17 | + const response = await worker.fetch(request, env, ctx); |
| 18 | + await waitOnExecutionContext(ctx); |
| 19 | + return response; |
16 | 20 | } |
17 | 21 |
|
18 | | -describe('festivals.json endpoint', () => { |
19 | | - it('returns 200 for /festivals.json', async () => { |
20 | | - const response = await fetchWorker('/festivals.json'); |
21 | | - expect(response.status).toBe(200); |
22 | | - }); |
| 22 | +describe("festivals.json endpoint", () => { |
| 23 | + it("returns 200 for /festivals.json", async () => { |
| 24 | + const response = await fetchWorker("/festivals.json"); |
| 25 | + expect(response.status).toBe(200); |
| 26 | + }); |
23 | 27 |
|
24 | | - it('returns 200 for /festivals (alias)', async () => { |
25 | | - const response = await fetchWorker('/festivals'); |
26 | | - expect(response.status).toBe(200); |
27 | | - }); |
| 28 | + it("returns 200 for /festivals (alias)", async () => { |
| 29 | + const response = await fetchWorker("/festivals"); |
| 30 | + expect(response.status).toBe(200); |
| 31 | + }); |
28 | 32 |
|
29 | | - it('returns valid JSON', async () => { |
30 | | - const response = await fetchWorker('/festivals.json'); |
31 | | - const data = await response.json(); |
32 | | - expect(data).toBeDefined(); |
33 | | - expect(data.festivals).toBeInstanceOf(Array); |
34 | | - expect(data.festivals.length).toBeGreaterThan(0); |
35 | | - }); |
| 33 | + it("returns valid JSON", async () => { |
| 34 | + const response = await fetchWorker("/festivals.json"); |
| 35 | + const data = await response.json(); |
| 36 | + expect(data).toBeDefined(); |
| 37 | + expect(data.festivals).toBeInstanceOf(Array); |
| 38 | + expect(data.festivals.length).toBeGreaterThan(0); |
| 39 | + }); |
36 | 40 |
|
37 | | - it('contains required festival fields', async () => { |
38 | | - const response = await fetchWorker('/festivals.json'); |
39 | | - const data = await response.json(); |
40 | | - const festival = data.festivals[0]; |
| 41 | + it("contains required festival fields", async () => { |
| 42 | + const response = await fetchWorker("/festivals.json"); |
| 43 | + const data = await response.json(); |
| 44 | + const festival = data.festivals[0]; |
41 | 45 |
|
42 | | - expect(festival.id).toBeDefined(); |
43 | | - expect(festival.name).toBeDefined(); |
44 | | - expect(festival.start_date).toBeDefined(); |
45 | | - expect(festival.end_date).toBeDefined(); |
46 | | - expect(festival.data_base_url).toBeDefined(); |
47 | | - }); |
| 46 | + expect(festival.id).toBeDefined(); |
| 47 | + expect(festival.name).toBeDefined(); |
| 48 | + expect(festival.start_date).toBeDefined(); |
| 49 | + expect(festival.end_date).toBeDefined(); |
| 50 | + expect(festival.data_base_url).toBeDefined(); |
| 51 | + }); |
48 | 52 |
|
49 | | - it('contains default_festival_id', async () => { |
50 | | - const response = await fetchWorker('/festivals.json'); |
51 | | - const data = await response.json(); |
52 | | - expect(data.default_festival_id).toBeDefined(); |
53 | | - expect(typeof data.default_festival_id).toBe('string'); |
54 | | - }); |
| 53 | + it("contains default_festival_id", async () => { |
| 54 | + const response = await fetchWorker("/festivals.json"); |
| 55 | + const data = await response.json(); |
| 56 | + expect(data.default_festival_id).toBeDefined(); |
| 57 | + expect(typeof data.default_festival_id).toBe("string"); |
| 58 | + }); |
55 | 59 |
|
56 | | - it('default_festival_id references an existing festival', async () => { |
57 | | - const response = await fetchWorker('/festivals.json'); |
58 | | - const data = await response.json(); |
59 | | - const ids = data.festivals.map((f) => f.id); |
60 | | - expect(ids).toContain(data.default_festival_id); |
61 | | - }); |
| 60 | + it("default_festival_id references an existing festival", async () => { |
| 61 | + const response = await fetchWorker("/festivals.json"); |
| 62 | + const data = await response.json(); |
| 63 | + const ids = data.festivals.map((f) => f.id); |
| 64 | + expect(ids).toContain(data.default_festival_id); |
| 65 | + }); |
62 | 66 |
|
63 | | - it('sets Content-Type to application/json with charset', async () => { |
64 | | - const response = await fetchWorker('/festivals.json'); |
65 | | - expect(response.headers.get('Content-Type')) |
66 | | - .toBe('application/json; charset=utf-8'); |
67 | | - }); |
| 67 | + it("sets Content-Type to application/json with charset", async () => { |
| 68 | + const response = await fetchWorker("/festivals.json"); |
| 69 | + expect(response.headers.get("Content-Type")).toBe( |
| 70 | + "application/json; charset=utf-8", |
| 71 | + ); |
| 72 | + }); |
68 | 73 |
|
69 | | - it('sets Cache-Control to no-cache', async () => { |
70 | | - const response = await fetchWorker('/festivals.json'); |
71 | | - expect(response.headers.get('Cache-Control')) |
72 | | - .toBe('no-cache, must-revalidate'); |
73 | | - }); |
| 74 | + it("sets Cache-Control to no-cache", async () => { |
| 75 | + const response = await fetchWorker("/festivals.json"); |
| 76 | + expect(response.headers.get("Cache-Control")).toBe( |
| 77 | + "no-cache, must-revalidate", |
| 78 | + ); |
| 79 | + }); |
74 | 80 |
|
75 | | - it('includes CORS headers', async () => { |
76 | | - const response = await fetchWorker('/festivals.json'); |
77 | | - expect(response.headers.get('Access-Control-Allow-Origin')) |
78 | | - .toBe('https://cambeerfestival.app'); |
79 | | - }); |
| 81 | + it("includes CORS headers", async () => { |
| 82 | + const response = await fetchWorker("/festivals.json"); |
| 83 | + expect(response.headers.get("Access-Control-Allow-Origin")).toBe( |
| 84 | + "https://cambeerfestival.app", |
| 85 | + ); |
| 86 | + }); |
80 | 87 |
|
81 | | - it('/festivals and /festivals.json return the same data', async () => { |
82 | | - const response1 = await fetchWorker('/festivals.json'); |
83 | | - const response2 = await fetchWorker('/festivals'); |
84 | | - const data1 = await response1.json(); |
85 | | - const data2 = await response2.json(); |
86 | | - expect(data1).toEqual(data2); |
87 | | - }); |
| 88 | + it("/festivals and /festivals.json return the same data", async () => { |
| 89 | + const response1 = await fetchWorker("/festivals.json"); |
| 90 | + const response2 = await fetchWorker("/festivals"); |
| 91 | + const data1 = await response1.json(); |
| 92 | + const data2 = await response2.json(); |
| 93 | + expect(data1).toEqual(data2); |
| 94 | + }); |
88 | 95 | }); |
0 commit comments