From 1c1c73d0180f53f27c776e40551b52f21cfdbfd4 Mon Sep 17 00:00:00 2001 From: Henry Dollman Date: Sun, 28 Jan 2024 17:11:14 -0500 Subject: [PATCH] add tests --- test/index.test.ts | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 test/index.test.ts diff --git a/test/index.test.ts b/test/index.test.ts new file mode 100644 index 0000000..a9616e8 --- /dev/null +++ b/test/index.test.ts @@ -0,0 +1,49 @@ +import { app } from '../src/index' +import { describe, expect, it } from 'bun:test' + +describe('General', () => { + it('home route redirects to github', async () => { + const response = await app.handle(new Request('http://localhost/')) + expect(response.headers.get('Location')).toBe('https://github.com/henrygd/ncaa-api') + }) + it('invalid route returns 400', async () => { + const response = await app.handle(new Request('http://localhost/invalid')) + expect(response.status).toBe(400) + }) + it('invalid page param returns 400', async () => { + const response = await app.handle(new Request('http://localhost/stats/test?page=invalid')) + expect(response.status).toBe(400) + }) + it('valid route returns good data', async () => { + const response = await app.handle( + new Request('http://localhost/rankings/football/fbs/associated-press') + ) + expect(response.status).toBe(200) + const { data } = await response.json() + expect(data.length).toBeGreaterThan(0) + expect(data[0]).toContainKeys(['RANK', 'SCHOOL']) + }) +}) + +describe('Header validation', () => { + // Custom header tests (must be last due to env var) + it('valid custom header returns 200', async () => { + Bun.env.NCAA_HEADER_KEY = 'valid' + const response = await app.handle( + new Request('http://localhost/rankings/football/fbs/associated-press', { + headers: { 'x-ncaa-key': 'valid' }, + }) + ) + expect(response.status).toBe(200) + }) + it('invalid custom header returns 401', async () => { + const response = await app.handle( + new Request('http://localhost/test', { headers: { 'x-ncaa-key': 'invalid' } }) + ) + expect(response.status).toBe(401) + }) + it('lack of custom header returns 401', async () => { + const response = await app.handle(new Request('http://localhost/stats/test')) + expect(response.status).toBe(401) + }) +})